prithivMLmods/Coder-Stat huggingface dataset:
Problem p00904 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

signed main() {
  int D;
  cin >> D;
  while (D--) {
    int m, n;
    cin >> m >> n;

    int cnt = 0;
    rep(p, -1500, 1500) rep(q, -1500, 1500) {
      if (p == 0 && q == 0)
        continue;
      int sum = p * p + q * q;
      if ((m * p + n * q) % sum)
        continue;
      if ((n * p - m * q) % sum)
        continue;
      cnt++;
    }
    cout << (cnt == 8 ? "P" : "C") << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

signed main() {
  int D;
  cin >> D;
  while (D--) {
    int m, n;
    cin >> m >> n;

    int cnt = 0;
    rep(p, -300, 300) rep(q, -300, 300) {
      if (p == 0 && q == 0)
        continue;
      int sum = p * p + q * q;
      if ((m * p + n * q) % sum)
        continue;
      if ((n * p - m * q) % sum)
        continue;
      cnt++;
    }
    cout << (cnt == 8 ? "P" : "C") << endl;
  }
  return 0;
}
Problem p00904 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int main(void) {
  int T;
  cin >> T;
  while (T--) {
    int p, q;
    cin >> p >> q;
    int P = p * p + q * q;
    int cnt = 0;
    int ma = 0;
    for (int i = 0; i <= P; i++)
      ma = i;
    for (int m = -ma; m <= ma; m++) {
      for (int n = -ma; n <= ma; n++) {
        int tmp = m * m + n * n;
        if (tmp == 0)
          continue;
        if (P % tmp == 0) {
          int mp = m * p + n * q;
          int mq = m * q - n * p;
          if (mp % tmp == 0 && mq % tmp == 0)
            cnt++;
        }
      }
    }
    if (cnt == 8)
      cout << "P" << endl;
    else
      cout << "C" << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int main(void) {
  int T;
  cin >> T;
  while (T--) {
    int p, q;
    cin >> p >> q;
    int P = p * p + q * q;
    int cnt = 0;
    int ma = 0;
    for (int i = 0; i * i <= P; i++)
      ma = i;
    for (int m = -ma; m <= ma; m++) {
      for (int n = -ma; n <= ma; n++) {
        int tmp = m * m + n * n;
        if (tmp == 0)
          continue;
        if (P % tmp == 0) {
          int mp = m * p + n * q;
          int mq = m * q - n * p;
          if (mp % tmp == 0 && mq % tmp == 0)
            cnt++;
        }
      }
    }
    if (cnt == 8)
      cout << "P" << endl;
    else
      cout << "C" << endl;
  }
  return 0;
}
Problem p00904 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int cnt[221212];
int X[221212];
int main() {
  int i, j;
  for (i = -400; i <= 400; i++) {
    for (j = i; j <= 400; j++) {
      if (i == 0 || j == 0)
        cnt[i * i + j * j] += 2;
      else
        cnt[i * i + j * j]++;
    }
  }
  for (i = 0; i <= 20000; i++) {
    for (j = 0; i * j <= 20000 && j <= 20000; j++) {
      if (cnt[i] && cnt[j])
        X[i * j] += cnt[i];
    }
  }
  int t, tc;
  scanf("%d", &t);
  for (tc = 0; tc < t; tc++) {
    int a, b;
    scanf("%d%d", &a, &b);
    puts(X[a * a + b * b] != 8 ? "C" : "P");
  }
  return 0;
}


Fixed submission:
#include <stdio.h>

int cnt[221212];
int X[221212];
int main() {
  int i, j;
  for (i = -200; i <= 200; i++) {
    for (j = i; j <= 200; j++) {
      if (i == 0 || j == 0)
        cnt[i * i + j * j] += 2;
      else
        cnt[i * i + j * j]++;
    }
  }
  for (i = 0; i <= 20000; i++) {
    for (j = 0; i * j <= 20000 && j <= 20000; j++) {
      if (cnt[i] && cnt[j])
        X[i * j] += cnt[i];
    }
  }
  int t, tc;
  scanf("%d", &t);
  for (tc = 0; tc < t; tc++) {
    int a, b;
    scanf("%d%d", &a, &b);
    puts(X[a * a + b * b] != 8 ? "C" : "P");
  }
  return 0;
}
Problem p00904 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
#define f first
#define s second

using namespace std;

typedef pair<int, int> P;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main(void) {

  int t;
  cin >> t;

  while (t--) {
    int p, q;
    cin >> p >> q;
    int tmp = p * p + q * q, cnt = 0;

    for (int n = 0; n * n <= tmp; n++) {
      for (int m = 0; m <= tmp; m++) {
        if (m * m + n * n < 1)
          continue;
        int res = gcd(m * p + n * q, m * q - n * p);
        if ((m * p + n * q) % (m * m + n * n) == 0 &&
            (m * q - n * p) % (m * m + n * n) == 0)
          cnt++;
      }
    }

    if (cnt * 2 <= 8)
      cout << "P" << endl;
    else
      cout << "C" << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
#define f first
#define s second

using namespace std;

typedef pair<int, int> P;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main(void) {

  int t;
  cin >> t;

  while (t--) {
    int p, q;
    cin >> p >> q;
    int tmp = p * p + q * q, cnt = 0;

    for (int n = 0; n * n <= tmp; n++) {
      for (int m = 0; m * m <= tmp; m++) {
        if (m * m + n * n < 1)
          continue;
        int res = gcd(m * p + n * q, m * q - n * p);
        if ((m * p + n * q) % (m * m + n * n) == 0 &&
            (m * q - n * p) % (m * m + n * n) == 0)
          cnt++;
      }
    }

    if (cnt * 2 <= 8)
      cout << "P" << endl;
    else
      cout << "C" << endl;
  }
  return 0;
}
Problem p00904 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int m, n, t, c = 0;
    cin >> m >> n;
    t = m * m + n * n;
    for (int i = 0; i < 10000; i++)
      for (int j = 0; j < 10000; j++)
        if (i * i + j * j < 1)
          continue;
        else if ((m * i + n * j) % (i * i + j * j) == 0 &&
                 (m * j - n * i) % (i * i + j * j) == 0)
          c++;
    if (c <= 4)
      cout << 'P' << endl;
    else
      cout << 'C' << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int m, n, t, c = 0;
    cin >> m >> n;
    t = m * m + n * n;
    for (int i = 0; i < 2000; i++)
      for (int j = 0; j < 2000; j++)
        if (i * i + j * j < 1)
          continue;
        else if ((m * i + n * j) % (i * i + j * j) == 0 &&
                 (m * j - n * i) % (i * i + j * j) == 0)
          c++;
    if (c <= 4)
      cout << 'P' << endl;
    else
      cout << 'C' << endl;
  }
}
Problem p00907 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int D;
double V[5];

double absd(double x) {

  if (x < 0) {
    return -x;
  }

  return x;
}

double interpolate(int n, int E) {

  double sum = 0.0;

  for (int k = 0; k < D + 3; k++) {

    if (k == n || k == E) {
      continue;
    }

    double p = V[k];

    for (int i = 0; i < D + 3; i++) {

      if (i != k && i != n && i != E) {
        p *= (n - i) / (double)(k - i);
      }
    }

    sum += p;
  }

  return sum;
}

bool outlier(int E) {

  for (int i = 0; i < D + 3; i++) {

    if (i == E) {
      continue;
    }

    double p = interpolate(i, E);

    if (absd(p - V[i]) > 0.1) {
      return false;
    }
  }

  return true;
}

int main() {

  while (cin >> D && D != 0) {

    for (int i = 0; i < D + 3; i++) {
      cin >> V[i];
    }

    for (int i = 0; i < D + 3; i++) {

      if (outlier(i)) {

        cout << i << endl;
        break;
      }
    }
  }
}

Fixed submission:
#include <iostream>
using namespace std;

int D;
double V[100];

double absd(double x) {

  if (x < 0) {
    return -x;
  }

  return x;
}

double interpolate(int n, int E) {

  double sum = 0.0;

  for (int k = 0; k < D + 3; k++) {

    if (k == n || k == E) {
      continue;
    }

    double p = V[k];

    for (int i = 0; i < D + 3; i++) {

      if (i != k && i != n && i != E) {
        p *= (n - i) / (double)(k - i);
      }
    }

    sum += p;
  }

  return sum;
}

bool outlier(int E) {

  for (int i = 0; i < D + 3; i++) {

    if (i == E) {
      continue;
    }

    double p = interpolate(i, E);

    if (absd(p - V[i]) > 0.1) {
      return false;
    }
  }

  return true;
}

int main() {

  while (cin >> D && D != 0) {

    for (int i = 0; i < D + 3; i++) {
      cin >> V[i];
    }

    for (int i = 0; i < D + 3; i++) {

      if (outlier(i)) {

        cout << i << endl;
        break;
      }
    }
  }
}
Problem p00909 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
long long dist[1 << 18];
long long group[1 << 18];
char q;
long long n, Q, a, b, c;
vector<long long> gr[1 << 18];
int main() {
  while (true) {
    for (int i = 0; i < (1 << 18); i++) {
      dist[i] = 0;
      group[i] = i;
      gr[i].clear();
      gr[i].push_back(i);
    }
    cin >> n >> Q;
    if (n == 0 && Q == 0)
      break;
    for (int i = 0; i < Q; i++) {
      cin >> q;
      if (q == '?') {
        cin >> a >> b;
        if (group[a] != group[b])
          cout << "UNKNOWN" << endl;
        else
          cout << dist[b] - dist[a] << endl;
      }
      if (q == '!') {
        cin >> a >> b >> c;
        long long L = group[a], M = group[b];
        if (group[a] != group[b] && gr[L].size() <= gr[M].size()) {
          long long K = dist[a] + c - dist[b];
          for (int j = 0; j < gr[M].size(); j++) {
            group[gr[M][j]] = L;
            gr[L].push_back(gr[M][j]);
            dist[gr[M][j]] += K;
          }
          gr[M].clear();
        } else if (group[a] != group[b]) {
          long long K = dist[b] - c - dist[a];
          for (int j = 0; j < gr[L].size(); j++) {
            group[gr[L][j]] = M;
            gr[M].push_back(gr[L][j]);
            dist[gr[L][j]] += K;
          }
          gr[L].clear();
        }
      }
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
long long dist[1 << 18];
long long group[1 << 18];
char q;
long long n, Q, a, b, c;
vector<long long> gr[1 << 18];
int main() {
  while (true) {
    for (int i = 0; i < (1 << 18); i++) {
      dist[i] = 0;
      group[i] = i;
      gr[i].clear();
      gr[i].push_back(i);
    }
    cin >> n >> Q;
    if (n == 0 && Q == 0)
      break;
    for (int i = 0; i < Q; i++) {
      cin >> q;
      if (q == '?') {
        cin >> a >> b;
        if (group[a] != group[b])
          cout << "UNKNOWN" << endl;
        else
          cout << dist[b] - dist[a] << endl;
      }
      if (q == '!') {
        cin >> a >> b >> c;
        long long L = group[a], M = group[b];
        if (group[a] != group[b] && gr[L].size() >= gr[M].size()) {
          long long K = dist[a] + c - dist[b];
          for (int j = 0; j < gr[M].size(); j++) {
            group[gr[M][j]] = L;
            gr[L].push_back(gr[M][j]);
            dist[gr[M][j]] += K;
          }
          gr[M].clear();
        } else if (group[a] != group[b]) {
          long long K = dist[b] - c - dist[a];
          for (int j = 0; j < gr[L].size(); j++) {
            group[gr[L][j]] = M;
            gr[M].push_back(gr[L][j]);
            dist[gr[L][j]] += K;
          }
          gr[L].clear();
        }
      }
    }
  }
  return 0;
}
Problem p00909 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX_N 100005
using namespace std;
typedef pair<int, int> P;
P par[MAX_N];

void init(int n) {
  for (int i = 0; i < n; i++)
    par[i] = P(i, 0);
}

P find(int x) {
  if (par[x].first == x)
    return P(x, 0);
  P r = find(par[x].first);
  return par[x] = P(r.first, par[x].second + r.second);
}

void unite(int x, int y, int cost) {
  P r1 = find(x);
  P r2 = find(y);
  if (r1.first == r2.first)
    return;
  par[r1.first] = P(r2.first, r2.second - r1.second + cost);
}

bool same(int x, int y) { return find(x).first == find(y).first; }

int n, m, a, b, cost;
char c;

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    init(n);
    rep(i, m) {
      scanf("%c%d%d", &c, &a, &b);
      if (c == '!') {
        scanf("%d", &cost);
        unite(b - 1, a - 1, cost);
      } else {
        if (!same(a - 1, b - 1))
          printf("UNKNOWN\n");
        else
          printf("%d\n", find(b - 1).second - find(a - 1).second);
      }
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX_N 100005
using namespace std;
typedef pair<int, int> P;
P par[MAX_N];

void init(int n) {
  for (int i = 0; i < n; i++)
    par[i] = P(i, 0);
}

P find(int x) {
  if (par[x].first == x)
    return P(x, 0);
  P r = find(par[x].first);
  return par[x] = P(r.first, par[x].second + r.second);
}

void unite(int x, int y, int cost) {
  P r1 = find(x);
  P r2 = find(y);
  if (r1.first == r2.first)
    return;
  par[r1.first] = P(r2.first, r2.second - r1.second + cost);
}

bool same(int x, int y) { return find(x).first == find(y).first; }

int n, m, a, b, cost;
char c;

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    init(n);
    rep(i, m) {
      scanf("%c", &c);
      scanf("%c%d%d", &c, &a, &b);
      if (c == '!') {
        scanf("%d", &cost);
        unite(b - 1, a - 1, cost);
      } else {
        if (!same(a - 1, b - 1))
          printf("UNKNOWN\n");
        else
          printf("%d\n", find(b - 1).second - find(a - 1).second);
      }
    }
  }
  return 0;
}
Problem p00912 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (int W, N; cin >> W >> N, W | N;) {
    int x[52345];
    for (int i = 0; i < N; i++) {
      cin >> x[i];
    }
    int l = 0, h = 1e9;
    while (h - l > 1) {
      int m = (h + l) / 2;
      vector<pair<int, int>> v[52345];
      bool p[52345] = {};
      v[0].emplace_back(0, 1);
      for (int i = 0; i < N; i++) {
        bool f = false;
        for (auto e : v[i]) {
          p[i] |= e.first + x[i] <= W;
          int nl = e.first + x[i] + 1;
          if (nl < W) {
            int nh = min(W, e.second + x[i] + m);
            if (!v[i + 1].empty() && v[i + 1].back().second >= nl) {
              v[i + 1].back().second = nh;
            } else {
              v[i + 1].emplace_back(nl, nh);
            }
          }
          f |= e.first <= W - x[i] && W - x[i] < e.second;
        }
        if (f) {
          v[i + 1].insert(v[i + 1].begin(), make_pair(0, 1));
        }
      }
      (!p[N - 1] ? l : h) = m;
    }
    cout << h << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (int W, N; cin >> W >> N, W | N;) {
    int x[52345];
    for (int i = 0; i < N; i++) {
      cin >> x[i];
    }
    int l = 0, h = 1e9;
    while (h - l > 1) {
      int m = (h + l) / 2;
      vector<pair<int, int>> v[52345];
      bool p[52345] = {};
      v[0].emplace_back(0, 1);
      for (int i = 0; i < N; i++) {
        v[i + 1].reserve(v[i].size());
        bool f = false;
        for (auto e : v[i]) {
          p[i] |= e.first + x[i] <= W;
          int nl = e.first + x[i] + 1;
          if (nl < W) {
            int nh = min(W, e.second + x[i] + m);
            if (!v[i + 1].empty() && v[i + 1].back().second >= nl) {
              v[i + 1].back().second = nh;
            } else {
              v[i + 1].emplace_back(nl, nh);
            }
          }
          f |= e.first <= W - x[i] && W - x[i] < e.second;
        }
        if (f) {
          v[i + 1].insert(v[i + 1].begin(), make_pair(0, 1));
        }
      }
      (!p[N - 1] ? l : h) = m;
    }
    cout << h << endl;
  }
}
Problem p00914 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstring>
#include <iostream>
using namespace std;
int x[100][1000][250], a, b, c, i, j, k, l, sum;
int main() {
  while (true) {
    sum = 0;
    memset(x, 0, sizeof(x));
    cin >> a >> b >> c;
    if (a == 0) {
      break;
    }
    x[0][0][0] = 1;
    for (i = 1; i <= b; i++) {
      for (j = 0; j <= c; j++) {
        for (l = 0; l < a; l++) {
          for (k = l + 1; k <= a; k++) {
            x[i][j + k][k] += x[i - 1][j][l];
          }
        }
      }
    }
    for (i = 0; i <= a; i++) {
      sum += x[b][c][i];
    }
    cout << sum << endl;
  }
}

Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
int x[100][1000][150], a, b, c, i, j, k, l, sum;
int main() {
  while (true) {
    sum = 0;
    memset(x, 0, sizeof(x));
    cin >> a >> b >> c;
    if (a == 0) {
      break;
    }
    x[0][0][0] = 1;
    for (i = 1; i <= b; i++) {
      for (j = 0; j <= c; j++) {
        for (l = 0; l < a; l++) {
          for (k = l + 1; k <= a; k++) {
            x[i][j + k][k] += x[i - 1][j][l];
          }
        }
      }
    }
    for (i = 0; i <= a; i++) {
      sum += x[b][c][i];
    }
    cout << sum << endl;
  }
}
Problem p00914 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  for (;;) {
    int n, k, s;
    cin >> n >> k >> s;
    if (n == 0 && k == 0 && s == 0)
      break;
    int cnt = 0;
    for (int i = 0; i < 1 << n; i++) {
      int c = 0, ss = 0;
      while (i != 0) {
        int x = i & -i;
        i ^= x;
        ss += __builtin_ffs(x);
        c++;
      }
      if (c == k && s == ss)
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  for (;;) {
    int n, k, s;
    cin >> n >> k >> s;
    if (n == 0 && k == 0 && s == 0)
      break;
    int cnt = 0;
    for (int i = 0; i < 1 << n; i++) {
      int c = 0, ss = 0, ii = i;
      while (ii != 0) {
        int x = ii & -ii;
        ii ^= x;
        ss += __builtin_ffs(x);
        c++;
      }
      if (c == k && s == ss)
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00915 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

#define MP make_pair

int main() {

  int N, L;
  while (cin >> N >> L && (N | L)) {
    vector<pair<int, int>> v;
    rep(i, N) {
      char d;
      int p;
      cin >> d >> p;
      v.push_back(MP(d == 'R' ? +1 : -1, p));
    }

    int time = 0;
    int ans = -1;
    int remain = N;
    while (remain > 0) {
      int ch[2 * L + 2];
      memset(ch, 0, sizeof ch);
      int *check = ch + L + 1;
      rep(i, N) {
        v[i].second += v[i].first;
        check[v[i].second]++;
      }

      rep(i, N) {
        if (check[v[i].second] == 2) {
          v[i].first *= -1;
        }
      }

      bool f = 0;
      rep(i, N) {
        if (v[i].second == 0) {
          remain--;
          f = 1;
          ans = i + 1;
        }
        if (v[i].second == L) {
          remain--;
          if (!f) {
            ans = i + 1;
          }
        }
      }
      time++;
    }
    cout << time << " " << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

#define MP make_pair

int main() {

  int N, L;
  while (cin >> N >> L && (N | L)) {
    vector<pair<int, int>> v;
    rep(i, N) {
      char d;
      int p;
      cin >> d >> p;
      v.push_back(MP(d == 'R' ? +1 : -1, p));
    }

    int time = 0;
    int ans = -1;
    int remain = N;
    while (remain > 0) {
      int ch[2200] = {};
      int *check = ch + 1100;
      rep(i, N) {
        v[i].second += v[i].first;
        check[v[i].second]++;
      }

      rep(i, N) {
        if (check[v[i].second] == 2) {
          v[i].first *= -1;
        }
      }

      bool f = 0;
      rep(i, N) {
        if (v[i].second == 0) {
          remain--;
          f = 1;
          ans = i + 1;
        }
        if (v[i].second == L) {
          remain--;
          if (!f) {
            ans = i + 1;
          }
        }
      }
      time++;
    }
    cout << time << " " << ans << endl;
  }
}
Problem p00926 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int dfs(int n, vector<vector<int>> &adj) {
  if ((int)adj[n].size() == 0)
    return n;
  int ret = 0;
  for (int i = 0; i < (int)adj[n].size(); i++) {
    ret = max(ret, dfs(adj[n][i], adj));
  }
  return ret;
}

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

  vector<vector<int>> rest(n + 1);
  for (int i = 0; i < m; i++) {
    int c, d;
    cin >> c >> d;
    rest[c].push_back(d);
  }
  vector<int> c, d;
  for (int i = 1; i <= n; i++) {
    int top = dfs(i, rest);
    if (top != i) {
      c.push_back(i);
      d.push_back(top);
    }
  }
  c.push_back(n + 1);
  d.push_back(n + 1);

  int ans = 0;
  int s = c[0], e = d[0];
  for (int i = 1; i < (int)c.size(); i++) {
    if (c[i] <= e) {
      e = max(e, d[i]);
    } else {
      ans += e - s;
      s = c[i];
      e = d[i];
    }
  }
  ans = n + 1 + ans * 2;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int dfs(int n, vector<vector<int>> &adj) {
  if ((int)adj[n].size() == 0)
    return n;
  int ret = 0;
  for (int i = 0; i < (int)adj[n].size(); i++) {
    ret = max(ret, dfs(adj[n][i], adj));
  }
  adj[n].clear();
  return ret;
}

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

  vector<vector<int>> rest(n + 1);
  for (int i = 0; i < m; i++) {
    int c, d;
    cin >> c >> d;
    rest[c].push_back(d);
  }
  vector<int> c, d;
  for (int i = 1; i <= n; i++) {
    int top = dfs(i, rest);
    if (top != i) {
      c.push_back(i);
      d.push_back(top);
    }
  }
  c.push_back(n + 1);
  d.push_back(n + 1);

  int ans = 0;
  int s = c[0], e = d[0];
  for (int i = 1; i < (int)c.size(); i++) {
    if (c[i] <= e) {
      e = max(e, d[i]);
    } else {
      ans += e - s;
      s = c[i];
      e = d[i];
    }
  }
  ans = n + 1 + ans * 2;
  cout << ans << endl;
  return 0;
}
Problem p00929 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

struct edge {
  int from, to, cost;
  bool operator<(const edge &p) const { return cost < p.cost; }
};

int pa[1000], ra[1000];
void init() {
  for (int i = 0; i < 1000; i++) {
    pa[i] = i;
    ra[i] = 0;
  }
}

int find(int x) {
  if (x == pa[x])
    return x;
  else
    return pa[x] = find(pa[x]);
}

bool same(int x, int y) { return find(x) == find(y); }

void unite(int x, int y) {
  x = find(x), y = find(y);
  if (ra[x] < ra[y])
    swap(x, y);
  pa[y] = x;
  if (ra[x] == ra[y])
    ra[x]++;
}

int N, M;
int a, b, c;
int flg[1000];
vector<edge> t;

int solve(int id) {
  int sum = 0;
  for (int i = 0; i < (int)t.size(); i++) {
    if (i == id)
      continue;
    edge e = t[i];
    if (same(e.from, e.to))
      continue;
    unite(e.from, e.to);
    sum += e.cost;
    if (id == -1)
      flg[i] = true;
  }
  if (id != -1) {
    edge e = t[id];
    if (same(e.from, e.to))
      return sum;
    else
      return 1e9;
  }
  return sum;
}

int main() {
  cin >> N >> M;
  init();
  t.clear();
  for (int i = 0; i < M; i++) {
    scanf("%d %d %d", &a, &b, &c);
    t.push_back((edge){a, b, c});
  }
  sort(t.begin(), t.end());
  int base = solve(-1);

  int ans = 0, ans2 = 0;
  for (int i = 0; i < M; i++) {
    if (flg[i] == false)
      continue;
    init();
    int b = solve(i);

    if (base == b)
      continue;
    ans++;
    ans2 += t[i].cost;
  }
  cout << ans << ' ' << ans2 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

struct edge {
  int from, to, cost;
  bool operator<(const edge &p) const { return cost < p.cost; }
};

int pa[1000], ra[1000];
void init() {
  for (int i = 0; i < 1000; i++) {
    pa[i] = i;
    ra[i] = 0;
  }
}

int find(int x) {
  if (x == pa[x])
    return x;
  else
    return pa[x] = find(pa[x]);
}

bool same(int x, int y) { return find(x) == find(y); }

void unite(int x, int y) {
  x = find(x), y = find(y);
  if (ra[x] < ra[y])
    swap(x, y);
  pa[y] = x;
  if (ra[x] == ra[y])
    ra[x]++;
}

int N, M;
int a, b, c;
int flg[100000];
vector<edge> t;

int solve(int id) {
  int sum = 0;
  for (int i = 0; i < (int)t.size(); i++) {
    if (i == id)
      continue;
    edge e = t[i];
    if (same(e.from, e.to))
      continue;
    unite(e.from, e.to);
    sum += e.cost;
    if (id == -1)
      flg[i] = true;
  }
  if (id != -1) {
    edge e = t[id];
    if (same(e.from, e.to))
      return sum;
    else
      return 1e9;
  }
  return sum;
}

int main() {
  cin >> N >> M;
  init();
  t.clear();
  for (int i = 0; i < M; i++) {
    scanf("%d %d %d", &a, &b, &c);
    t.push_back((edge){a, b, c});
  }
  sort(t.begin(), t.end());
  int base = solve(-1);

  int ans = 0, ans2 = 0;
  for (int i = 0; i < M; i++) {
    if (flg[i] == false)
      continue;
    init();
    int b = solve(i);

    if (base == b)
      continue;
    ans++;
    ans2 += t[i].cost;
  }
  cout << ans << ' ' << ans2 << endl;
  return 0;
}
Problem p00935 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

bool vis[100005];
int n, x, a[1005];

int main() {
  memset(vis, false, sizeof(vis));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    x = 0;
    for (int j = 0; j <= min(5, n - i); j++) {
      x = x * 10 + a[i + j];
      vis[x] = true;
    }
  }
  for (int i = 0; i <= 100000; i++) {
    if (vis[i] == false) {
      printf("%d\n", i);
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

bool vis[100005];
int n, x, a[1005];

int main() {
  memset(vis, false, sizeof(vis));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    x = 0;
    for (int j = 0; j <= min(4, n - i); j++) {
      x = x * 10 + a[i + j];
      vis[x] = true;
    }
  }
  for (int i = 0; i <= 100000; i++) {
    if (vis[i] == false) {
      printf("%d\n", i);
      break;
    }
  }
  return 0;
}
Problem p00935 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
// typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define DUMP(x) cerr << #x << " = " << (x) << endl

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

int main() {
  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> num(N);
  REP(i, N) cin >> num[i];

  set<string> num_set;
  REP(i, N) {
    string t;
    for (int j = i; j < N; j++) {
      t.push_back(num[j] + '0');
      num_set.insert(t);
    }
  }

  REP(i, 1e6) {
    if (num_set.find(to_string(i)) == num_set.end()) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
// typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define DUMP(x) cerr << #x << " = " << (x) << endl

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

int main() {
  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> num(N);
  REP(i, N) cin >> num[i];

  set<string> num_set;
  REP(i, N) {
    string t;
    for (int j = i; j < N; j++) {
      if (j - i + 1 > 6)
        break;
      t.push_back(num[j] + '0');
      num_set.insert(t);
    }
  }

  REP(i, 1e6) {
    if (num_set.find(to_string(i)) == num_set.end()) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}
Problem p00935 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(n) begin(n), end(n)

int a[1010];
int n;

string solve() {
  set<string> S;
  for (int i = 0; i < n; i++) {
    string cur;
    for (int j = i; j < n; j++) {
      cur += (char)(a[j] + '0');
      S.insert(cur);
    }
  }
  for (int i = 0; i < 1e9; i++) {
    string s = to_string(i);
    if (S.find(s) != S.end())
      continue;
    return s;
  }
  return "";
}

int main() {
  while (cin >> n) {
    rep(i, n) cin >> a[i];
    cout << solve() << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(n) begin(n), end(n)

int a[1010];
int n;

string solve() {
  set<string> S;
  for (int i = 0; i < n; i++) {
    string cur;
    for (int j = i; j < n; j++) {
      cur += (char)(a[j] + '0');
      if (cur.size() > 10)
        continue;
      S.insert(cur);
    }
  }
  for (int i = 0; i < 1e9; i++) {
    string s = to_string(i);
    if (S.find(s) != S.end())
      continue;
    return s;
  }
  return "";
}

int main() {
  while (cin >> n) {
    rep(i, n) cin >> a[i];
    cout << solve() << endl;
  }
}
Problem p00935 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
bool p[1001];
int main() {
  int n, c;
  cin >> n;
  int a[n];
  r(i, n) cin >> a[i];
  r(i, n) p[a[i]] = true;
  if (n > 1)
    r(i, n - 1) {
      int s = a[i] * 10 + a[i + 1];
      p[s] = true;
    }
  if (n > 2)
    r(i, n - 2) {
      int s = a[i] * 100 + a[i + 1] * 10 + a[i + 2];
      p[s] = true;
    }
  if (n > 3)
    r(i, n - 3) {
      int s = a[i] * 1000 + a[i + 1] * 100 + a[i + 2] * 10 + a[i + 3];
      p[s] = true;
    }
  c = 0;
  while (p[c])
    c++;
  cout << c << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
bool p[10000];
int main() {
  int n, c;
  cin >> n;
  int a[n];
  r(i, n) cin >> a[i];
  r(i, n) p[a[i]] = true;
  if (n > 1)
    r(i, n - 1) {
      int s = a[i] * 10 + a[i + 1];
      p[s] = true;
    }
  if (n > 2)
    r(i, n - 2) {
      int s = a[i] * 100 + a[i + 1] * 10 + a[i + 2];
      p[s] = true;
    }
  if (n > 3)
    r(i, n - 3) {
      int s = a[i] * 1000 + a[i + 1] * 100 + a[i + 2] * 10 + a[i + 3];
      p[s] = true;
    }
  c = 0;
  while (p[c])
    c++;
  cout << c << endl;
}
Problem p00936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define DEBUG(x) cerr << #x << " = " << x << endl
#define int long long
signed main() {
  int N;
  cin >> N;
  vector<int> R(N);
  REP(i, N) cin >> R[i];
  // double ans = 0.0;
  // ans += R[0] + R[N - 1];
  // for(int i = 0; i < N - 1; ++i) {
  //   int mx = max(R[i], R[i + 1]);
  //   int mn = min(R[i], R[i + 1]);
  //   ans += sqrt(max<double>(0.0, (R[i] + R[i + 1]) * (R[i] + R[i + 1]) - (mx
  //   - mn) * (mx - mn)));
  // }
  // cout << ans << endl;
  vector<double> pos(N);
  double ans = R[0] * 2;
  pos[0] = R[0];
  for (int i = 1; i < N; ++i) {
    pos[i] = R[i];
    REP(j, i) {
      int mx = max(R[i], R[j]);
      int mn = min(R[i], R[j]);
      double update =
          pos[j] + sqrt(max<double>(0.0, (R[i] + R[j]) * (R[i] + R[j]) -
                                             (mx - mn) * (mx - mn)));
      // DEBUG(update);
      pos[i] = max(pos[i], update);
    }
    ans = max(ans, pos[i] + R[i]);
    cerr << "pos[" << i << "] = " << pos[i] << endl;
  }
  cout.setf(ios::fixed);
  cout.precision(10);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define DEBUG(x) cerr << #x << " = " << x << endl
#define int long long
signed main() {
  int N;
  cin >> N;
  vector<int> R(N);
  REP(i, N) cin >> R[i];
  // double ans = 0.0;
  // ans += R[0] + R[N - 1];
  // for(int i = 0; i < N - 1; ++i) {
  //   int mx = max(R[i], R[i + 1]);
  //   int mn = min(R[i], R[i + 1]);
  //   ans += sqrt(max<double>(0.0, (R[i] + R[i + 1]) * (R[i] + R[i + 1]) - (mx
  //   - mn) * (mx - mn)));
  // }
  // cout << ans << endl;
  vector<double> pos(N);
  double ans = R[0] * 2;
  pos[0] = R[0];
  for (int i = 1; i < N; ++i) {
    pos[i] = R[i];
    REP(j, i) {
      int mx = max(R[i], R[j]);
      int mn = min(R[i], R[j]);
      double update =
          pos[j] + sqrt(max<double>(0.0, (R[i] + R[j]) * (R[i] + R[j]) -
                                             (mx - mn) * (mx - mn)));
      // DEBUG(update);
      pos[i] = max(pos[i], update);
    }
    ans = max(ans, pos[i] + R[i]);
    // cerr << "pos[" << i << "] = " << pos[i] << endl;
  }
  cout.setf(ios::fixed);
  cout.precision(10);
  cout << ans << endl;
}
Problem p00936 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <cmath>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

double r[1010], x[1010];

const double eps = 1e-11;
const double inf = 1e9;

bool cross(int m, double cx, double cy) {
  rep(i, m) {
    double dd = 0.0;
    dd += 1.0 * (x[i] - cx) * (x[i] - cx);
    dd += 1.0 * (r[i] - cy) * (r[i] - cy);
    double rr = 1.0 * (r[i] + r[m]) * (r[i] + r[m]);
    // cerr << rr << " " << dd << endl;
    if (rr > dd + eps)
      return true;
  }
  return false;
}

int main(void) {
  int n;
  cin >> n;
  rep(i, n) cin >> r[i];
  rep(i, n) x[i] = inf;

  rep(i, n) {
    for (int j = -1; j < i; ++j) {
      double cx = r[i];
      if (j >= 0) {
        double d = 1.0 * (r[i] + r[j]) * (r[i] + r[j]);
        d -= 1.0 * (r[i] - r[j]) * (r[i] - r[j]);
        cx = x[j] + sqrt(d);
      }
      if (cx < r[i])
        continue;
      if (i > 0 && x[i - 1] >= cx)
        continue;
      if (cross(i, cx, r[i]))
        continue;
      x[i] = min(x[i], cx);
    }
    // cerr << i << " " << x[i] << " " << r[i] << endl;
  }

  double ans = 0.0;
  rep(i, n) ans = max(ans, x[i] + r[i]);
  cout.precision(20);
  assert(ans < inf);
  cout << fixed << ans << endl;
  return 0;
}

Fixed submission:
#include <cassert>
#include <cmath>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

double r[1010], x[1010];

const double eps = 1e-5;
const double inf = 1e9;

bool cross(int m, double cx, double cy) {
  rep(i, m) {
    double dd = 0.0;
    dd += 1.0 * (x[i] - cx) * (x[i] - cx);
    dd += 1.0 * (r[i] - cy) * (r[i] - cy);
    double rr = 1.0 * (r[i] + r[m]) * (r[i] + r[m]);
    // cerr << rr << " " << dd << endl;
    if (rr > dd + eps)
      return true;
  }
  return false;
}

int main(void) {
  int n;
  cin >> n;
  rep(i, n) cin >> r[i];
  rep(i, n) x[i] = inf;

  rep(i, n) {
    for (int j = -1; j < i; ++j) {
      double cx = r[i];
      if (j >= 0) {
        double d = 1.0 * (r[i] + r[j]) * (r[i] + r[j]);
        d -= 1.0 * (r[i] - r[j]) * (r[i] - r[j]);
        cx = x[j] + sqrt(d);
      }
      if (cx < r[i])
        continue;
      if (i > 0 && x[i - 1] >= cx)
        continue;
      if (cross(i, cx, r[i]))
        continue;
      x[i] = min(x[i], cx);
    }
    // cerr << i << " " << x[i] << " " << r[i] << endl;
  }

  double ans = 0.0;
  rep(i, n) ans = max(ans, x[i] + r[i]);
  cout.precision(20);
  assert(ans < inf);
  cout << fixed << ans << endl;
  return 0;
}
Problem p00937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, m, a[3];
vector<int> rG[50];

int d[50], c[50];
bool dp[101][50][50];

void init(int si) {
  dp[0][si][si] = true;
  for (int i = 0; i <= 100; i++) {
    for (int j = 0; j < n; j++) {
      if (!dp[i][si][j])
        continue;
      for (int k = 0; k < (int)rG[j].size(); k++) {
        int w = rG[j][k];
        dp[i + 1][si][w] = true;
      }
    }
  }
}

void solve() {
  memset(c, 0, sizeof(c));
  memset(d, -1, sizeof(d));
  queue<int> Q;
  Q.push(n - 1);
  d[n - 1] = 0;
  while (!Q.empty()) {
    int pos = Q.front();
    Q.pop();
    if (pos == 0) {
      cout << d[pos] << endl;
      return;
    }
    for (int i = 0; i < 3; i++) {
      for (int to = 0; to < n; to++) {
        if (!dp[a[i]][pos][to])
          continue;
        c[to] |= (1 << i);
        if (c[to] == 7 && d[to] == -1) {
          d[to] = d[pos] + 1;
          Q.push(to);
        }
      }
    }
  }
  cout << "IMPOSSIBLE" << endl;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < 3; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    from--, to--;
    rG[to].push_back(from);
  }
  for (int i = 0; i < n; i++)
    init(i);

  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, a[3];
vector<int> rG[50];

int d[50], c[50];
bool dp[101][50][50];

void init(int si) {
  dp[0][si][si] = true;
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < n; j++) {
      if (!dp[i][si][j])
        continue;
      for (int k = 0; k < (int)rG[j].size(); k++) {
        int w = rG[j][k];
        dp[i + 1][si][w] = true;
      }
    }
  }
}

void solve() {
  memset(c, 0, sizeof(c));
  memset(d, -1, sizeof(d));
  queue<int> Q;
  Q.push(n - 1);
  d[n - 1] = 0;
  while (!Q.empty()) {
    int pos = Q.front();
    Q.pop();
    if (pos == 0) {
      cout << d[pos] << endl;
      return;
    }
    for (int i = 0; i < 3; i++) {
      for (int to = 0; to < n; to++) {
        if (!dp[a[i]][pos][to])
          continue;
        c[to] |= (1 << i);
        if (c[to] == 7 && d[to] == -1) {
          d[to] = d[pos] + 1;
          Q.push(to);
        }
      }
    }
  }
  cout << "IMPOSSIBLE" << endl;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < 3; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    from--, to--;
    rG[to].push_back(from);
  }
  for (int i = 0; i < n; i++)
    init(i);

  solve();
  return 0;
}
Problem p00937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()

vvi matmul(vvi a, vvi b) {
  int n = a.size();
  vvi d(n, vi(n, 0));
  rep(i, n) rep(j, n) rep(k, n) d[i][j] |= a[i][k] & b[k][j];
  return d;
}

static map<int, vvi> memo;
vvi matpow(vvi a, int p) {
  return memo[p] = memo.find(p) != memo.end()
                       ? p % 2 ? matmul(matpow(a, p - 1), a)
                               : matmul(matpow(a, p / 2), matpow(a, p / 2))
                       : memo[p];
}

int main() {
  int n, m, a[3];
  while (cin >> n >> m >> a[0] >> a[1] >> a[2]) {
    vvi z(n, vi(n, 0)), e = z;
    rep(_, m) {
      int p, q;
      cin >> p >> q;
      e[p - 1][q - 1] = 1;
    }
    memo.clear();
    memo[1] = e;

    vector<vvi> d(3);
    rep(p, 3) d[p] = matpow(e, a[p]);

    vi g(n, 0), ng(n, 0);
    g[n - 1] = 1;
    rep(t, 1000) {
      bool next = 0;
      fill(all(ng), 0);
      rep(i, n) if (!g[i]) {
        bool ok = 1, r;
        rep(p, 3)[&] {
          r = 0;
          rep(j, n) r |= (g[j] and d[p][i][j]);
          ok &= r;
        }
        ();
        if (ok)
          ng[i] = next = 1;
      }
      if (!next) {
        cout << "IMPOSSIBLE" << endl;
        break;
      } else if (ng[0]) {
        cout << t + 1 << endl;
        break;
      }
      rep(i, n) g[i] |= ng[i];
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()

vvi matmul(vvi a, vvi b) {
  int n = a.size();
  vvi d(n, vi(n, 0));
  rep(i, n) rep(j, n) rep(k, n) d[i][j] |= a[i][k] & b[k][j];
  return d;
}

static map<int, vvi> memo;
vvi matpow(vvi a, int p) {
  return memo[p] = memo.find(p) == memo.end()
                       ? p % 2 ? matmul(matpow(a, p - 1), a)
                               : matmul(matpow(a, p / 2), matpow(a, p / 2))
                       : memo[p];
}

int main() {
  int n, m, a[3];
  while (cin >> n >> m >> a[0] >> a[1] >> a[2]) {
    vvi z(n, vi(n, 0)), e = z;
    rep(_, m) {
      int p, q;
      cin >> p >> q;
      e[p - 1][q - 1] = 1;
    }
    memo.clear();
    memo[1] = e;

    vector<vvi> d(3);
    rep(p, 3) d[p] = matpow(e, a[p]);

    vi g(n, 0), ng(n, 0);
    g[n - 1] = 1;
    rep(t, 1000) {
      bool next = 0;
      fill(all(ng), 0);
      rep(i, n) if (!g[i]) {
        bool ok = 1, r;
        rep(p, 3)[&] {
          r = 0;
          rep(j, n) r |= (g[j] and d[p][i][j]);
          ok &= r;
        }
        ();
        if (ok)
          ng[i] = next = 1;
      }
      if (!next) {
        cout << "IMPOSSIBLE" << endl;
        break;
      } else if (ng[0]) {
        cout << t + 1 << endl;
        break;
      }
      rep(i, n) g[i] |= ng[i];
    }
  }
  return 0;
}
Problem p00937 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, m, a, b, c;
bool can[101][40][40];
int dp[50];

int main() {
  cin >> n >> m >> a >> b >> c;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    can[0][x][y] = 1;
  }
  rep(it, 100) rep(k, n) rep(i, n) rep(j, n) {
    if (can[it][i][k] && can[0][k][j])
      can[it + 1][i][j] = 1;
  }

  rep(it, 101) {
    rep(i, n - 1) {
      int x = inf, y = inf, z = inf;
      rep(j, n) {
        if (can[a - 1][i][j])
          x = min(x, dp[j] + 1);
        if (can[b - 1][i][j])
          y = min(y, dp[j] + 1);
        if (can[c - 1][i][j])
          z = min(z, dp[j] + 1);
      }
      dp[i] = max({dp[i], x, y, z});
    }
  }
  if (dp[0] >= n)
    cout << "IMPOSSIBLE" << endl;
  else
    cout << dp[0] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, m, a, b, c;
bool can[101][50][50];
int dp[50];

int main() {
  cin >> n >> m >> a >> b >> c;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    can[0][x][y] = 1;
  }
  rep(it, 100) rep(k, n) rep(i, n) rep(j, n) {
    if (can[it][i][k] && can[0][k][j])
      can[it + 1][i][j] = 1;
  }

  rep(it, 101) {
    rep(i, n - 1) {
      int x = inf, y = inf, z = inf;
      rep(j, n) {
        if (can[a - 1][i][j])
          x = min(x, dp[j] + 1);
        if (can[b - 1][i][j])
          y = min(y, dp[j] + 1);
        if (can[c - 1][i][j])
          z = min(z, dp[j] + 1);
      }
      dp[i] = max({dp[i], x, y, z});
    }
  }
  if (dp[0] >= n)
    cout << "IMPOSSIBLE" << endl;
  else
    cout << dp[0] << endl;

  return 0;
}
Problem p00946 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX_N 40
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m;
vector<int> ans;
bool saw[100001];

int main() {
  cin >> n >> m;
  rep(i, m) {
    int x;
    cin >> x;
    ans.push_back(x);
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    if (saw[ans[i]])
      continue;
    cout << ans[i] << endl;
    saw[ans[i]] = true;
  }
  for (int i = 1; i <= n; i++) {
    if (saw[i])
      continue;
    cout << i << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX_N 40
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m;
vector<int> ans;
bool saw[200001];

int main() {
  cin >> n >> m;
  rep(i, m) {
    int x;
    cin >> x;
    ans.push_back(x);
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    if (saw[ans[i]])
      continue;
    cout << ans[i] << endl;
    saw[ans[i]] = true;
  }
  for (int i = 1; i <= n; i++) {
    if (saw[i])
      continue;
    cout << i << endl;
  }
}