alexjercan/bugnet huggingface dataset:
Problem p01457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000
using namespace std;
int main() {
  int q;
  cin >> q;
  long long cnt1 = 0, cnt2 = 0;
  rep(i, q) {
    long long p, n;
    char c;
    scanf("%d %c %d", &p, &c, &n);
    // cin >> p >> c >> num;
    if (c == '(')
      cnt1 += n;
    if (c == ')')
      cnt2 += n;
    if (cnt1 == cnt2)
      printf("Yes\\n");
    // cout << "Yes" << endl;
    else
      printf("No\\n");
    // cout << "No" << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000
using namespace std;
int main() {
  int n;
  cin >> n;
  int cnt1 = 0, cnt2 = 0;
  rep(i, n) {
    int p, num;
    char c;
    cin >> p >> c >> num;
    rep(i, num) {
      if (c == '(')
        cnt1++;
      if (c == ')')
        cnt2++;
    }
    if (cnt1 == cnt2)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}
Problem p01457 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int n;
long long ans;

int main() {
  // freopen("1", "r", stdin);
  while (~scanf("%d", &n)) {
    ans = 0;
    while (n--) {
      long long a, b;
      char ch;
      scanf("%lld %c %lld", &a, &ch, &b);
      if (ch == ')') {
        ans += b;
      } else {
        ans -= b;
      }
      if (ans)
        printf("No\\n");
      else
        printf("Yes\\n");
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

vector<int> gra[500050];
long long dp[500050][2];
int dep[500050];
int n;
void dfs(int s) {
  if (s == n)
    return;
  int p, sum = 0;
  for (int i = 0; i < gra[s].size(); i++) {
    dep[p = gra[s][i]] = dep[s] + 1;
    dfs(p);
    dp[s][0] += dp[p][0] + dep[s];
  }
  dp[s][1] = dep[s] + dp[gra[s][0]][1] + dep[s] + dp[gra[s][1]][1];
  dp[s][1] = min(dp[s][1], dep[s] + dp[gra[s][0]][0] + dp[gra[s][1]][1]);
  dp[s][1] = min(dp[s][1], dep[s] + dp[gra[s][1]][0] + dp[gra[s][0]][1]);
}
int main() {
  int x, y;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    gra[i].push_back(x);
    gra[i].push_back(y);
  }
  dp[n][0] = dp[n][1] = 1;
  dep[1] = 0;
  dfs(1);
  printf("%lld\\n", dp[1][1]);
  return 0;
}
Problem p01459 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int d = 2, h, w, n, sx, sy, gx, gy, ans = -1;
bool dp[111][111][4][11][11];
string s[111];
struct T {
  int y, x, d, l, r, cost;
  T(int a, int b, int c, int f, int e, int w) {
    y = a, x = b, d = c, l = f, r = e, cost = w;
  }
};
signed main() {
  cin >> h >> w >> n;
  r(i, h) cin >> s[i];
  r(i, h) r(j, w) {
    if (s[i][j] == 'S')
      sy = i, sx = j;
    if (s[i][j] == 'G')
      gy = i, gx = j;
  }
  deque<T> q;
  q.push_back(T(sy, sx, 2, 0, 0, 0));
  dp[sy][sx][2][0][0] = 1;
  while (!q.empty()) {
    T a = q.front();
    q.pop_front();
    d = a.d;
    if (s[a.y][a.x] == 'G') {
      ans = a.cost;
      break;
    }
    if (d == 0 && s[a.y][a.x] == 'S')
      continue;
    int y = a.y + dy[d];
    int x = a.x + dx[d];
    if (y < 0 || x < 0 || y >= h || x >= w)
      continue;
    if (s[y][x] == '#')
      continue;
    if (1) {
      T t = T(y, x, d, a.l, a.r, a.cost);
      if (dp[y][x][d][a.l][a.r + 1])
        continue;
      dp[y][x][d][a.l][a.r + 1] = 1;
      q.push_front(t);
    }
    if (a.l < n) {
      T t = T(y, x, 3 - d, a.l + 1, a.r, a.cost + 1);
      if (dp[y][x][3 - d][a.l + 1][a.r])
        continue;
      dp[y][x][3 - d][a.l + 1][a.r] = 1;
      q.push_back(t);
    }
    if (a.r < n) {
      T t = T(y, x, d ^ 1, a.l, a.r + 1, a.cost + 1);
      if (dp[y][x][d ^ 1][a.l][a.r + 1])
        continue;
      dp[y][x][d ^ 1][a.l][a.r + 1] = 1;
      q.push_back(t);
    }
  }
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int d = 2, h, w, n, sx, sy, gx, gy, ans = -1;
string s[111];
struct T {
  int y, x, d, l, r, cost;
  T(int a, int b, int c, int f, int e, int w) {
    y = a, x = b, d = c, l = f, r = e, cost = w;
  }
};
signed main() {
  cin >> h >> w >> n;
  r(i, h) cin >> s[i];
  r(i, h) r(j, w) {
    if (s[i][j] == 'S')
      sy = i, sx = j;
    if (s[i][j] == 'G')
      gy = i, gx = j;
  }
  deque<T> q;
  q.push_back(T(sy, sx, 2, 0, 0, 0));
  while (!q.empty()) {
    T a = q.front();
    q.pop_front();
    d = a.d;
    if (s[a.y][a.x] == 'G') {
      ans = a.cost;
      break;
    }
    if (d == 0 && s[a.y][a.x] == 'S')
      continue;
    int y = a.y + dy[d];
    int x = a.x + dx[d];
    if (y < 0 || x < 0 || y >= h || x >= w)
      continue;
    if (s[y][x] == '#')
      continue;
    if (1) {
      q.push_front(T(y, x, d, a.l, a.r, a.cost));
    }
    if (a.l < n) {
      q.push_back(T(y, x, 3 - d, a.l + 1, a.r, a.cost + 1));
    }
    if (a.r < n) {
      q.push_back(T(y, x, d ^ 1, a.l, a.r + 1, a.cost + 1));
    }
  }
  cout << ans << endl;
}
Problem p01461 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int N;
int l[523456], r[523456];
int nc[523456];
long long wos[523456];
long long mem[523456];

long long rec(int x, int d) {
  if (x == N)
    return 0;
  auto lr = rec(l[x], d + 1), rr = rec(r[x], d + 1);
  nc[x] = nc[l[x]] + nc[r[x]];
  wos[x] = wos[l[x]] + nc[l[x]] + wos[r[x]] + nc[r[x]];
  return min({nc[l[x]] + wos[l[x]] + rr + 1, nc[r[x]] + wos[r[x]] + lr + 1,
              lr + rr + 2 + d});
}

int main() {
  cin >> N;
  for (int i = 1; i < N; i++) {
    cin >> l[i] >> r[i];
  }
  nc[N] = 1;
  vector<pair<int, int>> v;
  v.emplace_back(0, 1);
  for (int i = 0; i < v.size(); i++) {
    int x = v[i].second;
    if (l[x] != N) {
      v.emplace_back(v[i].first + 1, l[x]);
    }
    if (r[x] != N) {
      v.emplace_back(v[i].first + 1, r[x]);
    }
  }
  for (int i = v.size() - 1; i >= 0; i--) {
    int x = v[i].second;
    auto lr = mem[l[x]], rr = mem[r[x]];
    nc[x] = nc[l[x]] + nc[r[x]];
    wos[x] = wos[l[x]] + nc[l[x]] + wos[r[x]] + nc[r[x]];
    mem[x] = min({nc[l[x]] + wos[l[x]] + rr + 1, nc[r[x]] + wos[r[x]] + lr + 1,
                  lr + rr + 2 + v[i].first});
  }
  cout << mem[1] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int N;
int l[523456], r[523456];
int nc[523456];
long long wos[523456];

long long rec(int x, int d) {
  if (x == N)
    return 0;
  auto lr = rec(l[x], d + 1), rr = rec(r[x], d + 1);
  nc[x] = nc[l[x]] + nc[r[x]];
  wos[x] = wos[l[x]] + nc[l[x]] + wos[r[x]] + nc[r[x]];
  return min({nc[l[x]] + wos[l[x]] + rr + 1, nc[r[x]] + wos[r[x]] + lr + 1,
              lr + rr + 2 + d});
}

int main() {
  cin >> N;
  for (int i = 1; i < N; i++) {
    cin >> l[i] >> r[i];
  }
  nc[N] = 1;
  cout << rec(1, 0) << endl;
}
Problem p01461 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
const int MAX_V = 500000;
typedef long long ll;
int n, op[MAX_V][2];
ll s[MAX_V], ns[MAX_V], num[MAX_V], d[MAX_V];
void calc(int v) {
  int a = op[v][0], b = op[v][1];
  num[v] = num[a] + num[b];
  ns[v] = ns[a] + ns[b] + num[v];
  s[v] = min(ns[v], 1 + s[a] + d[v] + 1 + s[b]);
  s[v] = min(s[v], ns[a] + num[a] + 1 + s[b]);
  s[v] = min(s[v], ns[b] + num[b] + 1 + s[a]);
}
int main() {
  cin >> n;
  rep(i, n - 1) {
    scanf("%d%d", &op[i][0], &op[i][1]);
    op[i][0]--, op[i][1]--;
    d[op[i][0]] = d[i] + 1;
    d[op[i][1]] = d[i] + 1;
  }
  num[n - 1] = 1;
  s[n - 1] = 0;
  ns[n - 1] = 0;
  for (int i = n - 2; i >= 0; i--)
    calc(i);
  cout << min(s[0], ns[0]) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
int n, op[500001][2], s[500001], ns[500001], num[500001], d[500000];
void dfs(int v) {
  //	show(v);
  if (v < 0 || n <= v)
    show(v);
  int a = op[v][0], b = op[v][1];
  rep(j, 2) {
    if (op[v][j] == n - 1)
      continue;
    d[op[v][j]] = d[v] + 1;
    /*if(op[v][j]==-1){
            cout<<"ji\\n";
            show(v);
    }*/
    dfs(op[v][j]);
  }
  num[v] = num[a] + num[b];
  ns[v] = ns[a] + ns[b] + num[v];
  s[v] = min(ns[v], 1 + s[a] + d[v] + 1 + s[b]);
  s[v] = min(s[v], ns[a] + num[a] + 1 + s[b]);
  s[v] = min(s[v], ns[b] + num[b] + 1 + s[a]);
  /*	show(v);
          show(num[v]);
          show(ns[v]);
          show(s[v]);
          cout<<endl;*/
}
int main() {
  cin >> n;
  rep(i, n - 1) {
    scanf("%d%d", &op[i][0], &op[i][1]);
    op[i][0]--, op[i][1]--;
  }
  num[n - 1] = 1;
  s[n - 1] = 0;
  ns[n - 1] = 0;
  dfs(0);
  cout << min(s[0], ns[0]) << endl;
}
Problem p01461 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;

constexpr int MAX_N = 500000;
int n;
int l[MAX_N], r[MAX_N];

int leafs[MAX_N + 1];
int depth[MAX_N + 1];
long long memo[MAX_N][2];
int in[MAX_N + 1];
int out[MAX_N + 1];

long long dfs(int v, bool used) {
  if (v == n)
    return 0;

  long long &res = memo[v][used];
  if (res != -1)
    return res;

  if (used) {
    res = dfs(l[v], true) + dfs(r[v], true) + depth[v] + 2;
  } else {
    res = min(
        {dfs(l[v], false) + dfs(r[v], true) + 2 - (leafs[r[v]] - 1) * depth[v],
         dfs(l[v], true) + dfs(r[v], false) + 2 - (leafs[l[v]] - 1) * depth[v],
         dfs(l[v], false) + dfs(r[v], false) + 2 + depth[v]});
  }

  return res;
}

void init() {
  stack<tuple<int, int, int>> sta;
  sta.push(make_tuple(1, 0, 0));

  int res;
  while (!sta.empty()) {
    int v, d, cnt;
    tie(v, d, cnt) = sta.top();

    if (v == n) {
      depth[v] = d;
      res = leafs[v] = 1;
      sta.pop();
    } else if (cnt == 0) {
      depth[v] = d;
      ++get<2>(sta.top());
      sta.push(make_tuple(l[v], d + 1, 0));
    } else if (cnt == 1) {
      leafs[v] += res;
      ++get<2>(sta.top());
      sta.push(make_tuple(r[v], d + 1, 0));
    } else {
      res = (leafs[v] += res);
      sta.pop();
    }
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> n;

  for (int i = 1; i < n; ++i) {
    cin >> l[i] >> r[i];
  }

  init();

  memset(memo, -1, sizeof(memo));
  for (int v = n - 1; v > 1; --v) {
    dfs(v, false);
    dfs(v, true);
  }
  cout << dfs(l[1], false) + dfs(r[1], false) + 2 << endl;

  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

constexpr int MAX_N = 500000;
int n;
int l[MAX_N], r[MAX_N];

int leafs[MAX_N + 1];
int depth[MAX_N + 1];
int memo[MAX_N][2];

int dfs(int v, bool used) {
  if (v == n)
    return 0;

  int &res = memo[v][used];
  if (res != -1)
    return res;

  if (used) {
    res = dfs(l[v], true) + dfs(r[v], true) + depth[v] + 2;
  } else {
    res = min(
        {dfs(l[v], false) + dfs(r[v], true) + 2 - (leafs[r[v]] - 1) * depth[v],
         dfs(l[v], true) + dfs(r[v], false) + 2 - (leafs[l[v]] - 1) * depth[v],
         dfs(l[v], false) + dfs(r[v], false) + 2 + depth[v]});
  }

  return res;
}

int init(int v, int d) {
  depth[v] = d;
  return leafs[v] = (v == n ? 1 : init(l[v], d + 1) + init(r[v], d + 1));
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> n;

  for (int i = 1; i < n; ++i) {
    cin >> l[i] >> r[i];
  }

  init(1, 0);

  memset(memo, -1, sizeof(memo));
  for (int v = n - 1; v > 1; --v) {
    dfs(v, false);
    dfs(v, true);
  }
  cout << dfs(l[1], false) + dfs(r[1], false) + 2 << endl;

  return EXIT_SUCCESS;
}
Problem p01461 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#define SE second
#define FI first
#define MP(x, y) make_pair(x, y)
const int MAXN = 500010;
const int MOD = 1000000007;
int to[MAXN][2], dep[MAXN];
long long no[MAXN][3];
inline void cal(int h, int n) {
  int a = to[h][0], b = to[h][1];
  no[h][0] = no[h][1] = no[h][2] = 0;
  if (h == n) {
    no[h][2] = 1;
    return;
  }
  no[h][2] = no[a][2] + no[b][2];
  no[h][1] = no[a][1] + no[b][1] + no[h][2];

  no[h][0] = no[h][1];
  no[h][0] = min(no[h][0], no[a][1] + no[a][2] + no[b][0] + 1);
  no[h][0] = min(no[h][0], no[b][1] + no[b][2] + no[a][0] + 1);
  no[h][0] = min(no[h][0], no[a][0] + 1 + no[b][0] + 1 + dep[h]);
  return;
}
int main() {
  // freopen("/home/moor/Code/output","r",stdin);
  int n;
  while (scanf("%d", &n) == 1) {
    dep[1] = 0;
    for (int i = 1; i <= n - 1; ++i) {
      scanf("%d%d", &to[i][0], &to[i][1]);
      dep[to[i][0]] = dep[i] + 1;
      dep[to[i][1]] = dep[i] + 1;
    }
    for (int i = n; i >= 1; --i)
      cal(i, n);
    cout << no[1][0] << '\\n';
  }
  return 0;
}

Error: 0

Fixed submission:

#pragma comment(linker, "/STACK:10240000,10240000")
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int maxn = 555555;

long long dp[maxn], dep[maxn], dep1[maxn], x[maxn], y[maxn], vis[maxn],
    dep2[maxn];
int n;
long long yyh[maxn];

void dfs(int u, int fa) {
  dep2[u] = 0;
  if (u == n)
    return;
  dfs(x[u], u);
  dfs(y[u], u);
  dep2[u] = dep2[x[u]] + dep2[y[u]];
  dep2[u] += dep1[u];
  return;
}
int main() {
  // freopen("in","r",stdin);
  //    freopen("out","w",stdout);
  while (cin >> n) {
    memset(dep, 0, sizeof(dep));
    for (int i = 1; i <= n - 1; i++) {
      cin >> x[i] >> y[i];
      dep[x[i]] = dep[i] + 1;
      dep[y[i]] = dep[i] + 1;
    }
    memset(dep1, 0, sizeof(dep1));
    dep1[n] = 1;
    for (int i = n - 1; i >= 1; i--) {
      dep1[i] = dep1[x[i]] + dep1[y[i]];
    }
    memset(dep2, 0, sizeof(dep2));
    memset(vis, 0, sizeof(vis));
    dfs(1, 0);
    memset(dp, 0, sizeof(dp));
    for (int i = n - 1; i >= 1; i--) {
      dp[i] = 2 + dp[x[i]] + dp[y[i]] + dep[i];
      //   cout << i << " " << dep[i] << endl;
      dp[i] = min(dp[i], dp[x[i]] + 1 + dep2[y[i]] + dep1[y[i]]);
      dp[i] = min(dp[i], dp[y[i]] + 1 + dep2[x[i]] + dep1[x[i]]);
      //  cout << dp[i] << " ";
    }
    //   cout << endl;
    // for (int i=1; i<=n; i++) {
    //     cout << dep[i]<<" "<<dep1[i] <<" "<<dep2[i]<<" "<<dp[i] << endl;
    // }
    // cout << endl;
    cout << dp[1] << endl;
  }
  return 0;
}
Problem p01461 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#define rep(i, s, t) for (i = s; i <= t; i++)
using namespace std;
typedef pair<int, int> PII;
queue<PII> q;
const int maxn = 1000001;
long long depth[maxn];
int l[maxn], r[maxn];
long long dp[maxn];
int qqq[maxn];
long long ans;
long long cnt[maxn];
int i, j, m, n, k, o, p;
int depp;
int tail;
void dfs(int k, int depp) {}
int main() {
  //	freopen("1.in","r",stdin);
  while (scanf("%d", &n) != EOF) {
    memset(depth, 0, sizeof(depth));
    memset(dp, 0, sizeof(dp));
    memset(cnt, 0, sizeof(cnt));
    ans = 0;
    tail = 0;
    queue<PII> q;
    rep(i, 1, n - 1) scanf("%d%d", &l[i], &r[i]);
    cnt[n] = 1;
    tail = 0;
    q.push(PII(1, 0));
    while (!q.empty()) {
      k = q.front().first;
      depp = q.front().second;
      q.pop();
      if (k == n) {
        ans += depp;
        continue;
      }
      qqq[++tail] = k;
      depth[k] = depp;
      q.push(PII(l[k], depp + 1));
      q.push(PII(r[k], depp + 1));
    }
    for (i = tail; i >= 1; i--) {
      k = qqq[i];
      cnt[k] = cnt[l[k]] + cnt[r[k]];
      dp[k] = max(dp[l[k]] + dp[r[k]], max(dp[l[k]] + depth[k] * cnt[r[k]],
                                           dp[r[k]] + depth[k] * cnt[l[k]]));
    }
    cout << ans - dp[1] << endl;
  }
  fclose(stdin);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define rep(i, s, t) for (i = s; i <= t; i++)
using namespace std;
const int maxn = 1000001;
long long depth[maxn];
int l[maxn], r[maxn];
long long dp[maxn];
long long ans;
long long cnt[maxn];
int i, j, m, n, k, o, p;
int queue[maxn];
int tail;
void dfs(int k, int depp) {
  if (k == n) {
    ans += depp;
    return;
  }
  depth[k] = depp;
  dfs(l[k], depp + 1);
  dfs(r[k], depp + 1);
  cnt[k] = cnt[l[k]] + cnt[r[k]];
  dp[k] = max(dp[l[k]] + dp[r[k]], max(dp[l[k]] + depth[k] * cnt[r[k]],
                                       dp[r[k]] + depth[k] * cnt[l[k]]));
}
int main() {
  //	freopen("1.in","r",stdin);
  while (scanf("%d", &n) != EOF) {
    memset(depth, 0, sizeof(depth));
    memset(dp, 0, sizeof(dp));
    memset(cnt, 0, sizeof(cnt));
    ans = 0;
    rep(i, 1, n - 1) scanf("%d%d", &l[i], &r[i]);
    cnt[n] = 1;
    dfs(1, 0);
    //		rep(i,1,n-1) cout << dp[i] << endl;
    cout << ans - dp[1] << endl;
  }
  fclose(stdin);
  return 0;
}
Problem p01462 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

int main() {
  int n, m;
  double p;
  cin >> n >> m >> p;

  vector<int> u(m), v(m);
  rep(i, m) {
    cin >> u[i] >> v[i];
    --u[i];
    --v[i];
  }

  p /= 100.0;

  vector<double> pw(m + 1);
  pw[0] = 1;
  rep(i, m) pw[i + 1] = pw[i] * p;

  vector<double> dp(1 << n);
  rep(mask, 1 << n) {
    dp[mask] = 1;

    int sub = mask;
    while (1) {
      sub = (sub - 1) & mask;
      if (sub == 0)
        break;
      if (sub % 2 != 1)
        continue;

      int o_mask = mask ^ sub;

      int ct = 0;
      rep(i, m) {
        if (sub >> u[i] & 1) {
          if (o_mask >> v[i] & 1)
            ++ct;
        }
        if (sub >> v[i] & 1) {
          if (o_mask >> u[i] & 1)
            ++ct;
        }
      }

      dp[mask] -= dp[sub] * pw[ct];
    }
  }

  cout << fixed << setprecision(15) << dp[(1 << n) - 1] << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

const int M = 101;

int n, m, p;
int u[M], v[M];

double pp;

map<vector<int>, double> dp[M];
double dfs(int e, const vector<int> &cc) {
  bool ok = true;
  rep(i, n) if (cc[i] != 0) ok = false;
  if (ok)
    return 1;

  if (e == m)
    return 0;

  if (dp[e].count(cc))
    return dp[e][cc];

  double ret = 0;
  ret += pp * dfs(e + 1, cc);

  vector<int> nc(cc);
  if (cc[u[e]] != cc[v[e]]) {
    int val = min(cc[u[e]], cc[v[e]]);
    rep(i, n) {
      if (nc[i] == cc[u[e]] || nc[i] == cc[v[e]])
        nc[i] = val;
    }
  }
  ret += (1 - pp) * dfs(e + 1, nc);

  return dp[e][cc] = ret;
}

int main() {
  cin >> n >> m >> p;
  rep(i, m) {
    cin >> u[i] >> v[i];
    --u[i];
    --v[i];
  }

  pp = p / 100.0;

  vector<int> s(n);
  rep(i, n) s[i] = i;
  printf("%.15f\\n", dfs(0, s));
  return 0;
}
Problem p01462 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
int N, M;
double P;
int A[100];
int B[100];
int hit(int k) { return 1 << k; }

const double None = 1e-100;

double prob(int s, int V);

// sが連結になる確率
double memo[1 << 14];
double calc(int s) {
  double &res = memo[s];
  if (res != None)
    return res;
  res = 1.0;
  int sub = s;
  do {
    // if(sub != s) printf("s = %d sub = %d prob = %f\\n", s, sub, prob(sub, s));
    if (sub != s)
      res -= prob(sub, s);
    // do something here
    sub = (sub - 1) & s;
  } while (sub != s);
  // printf("calc(%d) -> %f\\n", s, res);
  return res;
}

// V(v0) が s になる確率
// map<int, double> memo2[1 << 14];
double prob(int s, int V) {
  if (!(s >> 0 & 1))
    return 0.0;

  // if(memo2[V].count(s)) return memo2[V][s];

  double res = calc(s);

  for (int i = 0; i < M; i++) {
    if (!(V & hit(A[i])))
      continue;
    if (!(V & hit(B[i])))
      continue;
    int fa = (s & hit(A[i]) ? 1 : 0);
    int fb = (s & hit(B[i]) ? 1 : 0);
    if (fa != fb) {
      res *= P;
    }
  }

  return res;
}

int main() {
  while (cin >> N >> M >> P && N > 0) {
    REP(i, 1 << N) memo[i] = None;
    // REP(i, 1 << N) memo2[i].clear();

    P /= 100.0;

    REP(i, M) cin >> A[i] >> B[i];
    REP(i, M) A[i]--;
    REP(i, M) B[i]--;

    printf("%.12f\\n", calc((1 << N) - 1));
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
int N, M;
double P;
int A[100];
int B[100];
int hit(int k) { return 1 << k; }

const double None = 1e-100;

double prob(int s, int V);

// sが連結になる確率
double memo[1 << 14];
double calc(int s) {
  double &res = memo[s];
  if (res != None)
    return res;
  res = 1.0;
  int sub = s;
  do {
    // if(sub != s) printf("s = %d sub = %d prob = %f\\n", s, sub, prob(sub, s));
    if (sub != s)
      res -= prob(sub, s);
    // do something here
    sub = (sub - 1) & s;
  } while (sub != s);
  // printf("calc(%d) -> %f\\n", s, res);
  return res;
}

// V(v0) が s になる確率
map<int, double> memo2[1 << 14];
double prob(int s, int V) {
  if (!(s >> 0 & 1))
    return 0.0;

  if (memo2[V].count(s))
    return memo2[V][s];

  double res = calc(s);

  for (int i = 0; i < M; i++) {
    if (!(V & hit(A[i])))
      continue;
    if (!(V & hit(B[i])))
      continue;
    int fa = (s & hit(A[i]) ? 1 : 0);
    int fb = (s & hit(B[i]) ? 1 : 0);
    if (fa != fb) {
      res *= P;
    }
  }

  return memo2[V][s] = res;
}

int main() {
  while (cin >> N >> M >> P && N > 0) {
    REP(i, 1 << N) memo[i] = None;
    REP(i, 1 << N) memo2[i].clear();

    P /= 100.0;

    REP(i, M) cin >> A[i] >> B[i];
    REP(i, M) A[i]--;
    REP(i, M) B[i]--;

    printf("%.12f\\n", calc((1 << N) - 1));
  }
  return 0;
}