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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <random>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const int MN = 501 * 1000 * 1000;

ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}
ll ext_gcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b) {
    d = ext_gcd(b, a % b, y, x);
    y -= a / b * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

bitset<MN> mp;

int w, d, n, m, t, u, h;
int x;
void init() {
  m = t - m % t;
  u = gcd(t, w + d), h = gcd(t, m);
  ll xx, y;
  ext_gcd(m, t, xx, y);
  x = ((xx % t) + t) % t;
}
int solve() {
  init();
  int res = 0, c = 0;
  int l = 0;

  int tt = t / 2;
  int s = 0;
  int i, j;
  int th = t / h;
  for (i = 0; i < tt; i += h) {
    if (s < n) {
      l = (l + u - 1) / u * u;
      res += max(0, (i - l - w + u) / u);
      l = i + 1;
    }
    s += x;
    s %= th;
  }

  for (; i < t; i += h) {
    if (s < n) {
      l = (l + u - 1) / u * u;
      res += max(0, (i - l - w + u) / u);
      l = i + 1;
    }
    s += x;
    s %= th;
  }
  l = (l + u - 1) / u * u;
  res += max(0, (t - l - w + u) / u);
  return res;
}

int main() {
  cin >> w >> d >> n >> m >> t;
  w++;
  d--;
  printf("%.20f\\n", 1.0 * solve() / (t / u));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <random>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const int MN = 501 * 1000 * 1000;

ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}
ll ext_gcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b) {
    d = ext_gcd(b, a % b, y, x);
    y -= a / b * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

bitset<MN> mp;

int w, d, n, m, t, u, h;
int x;
void init() {
  m = t - m % t;
  u = gcd(t, w + d), h = gcd(t, m);
  ll xx, y;
  ext_gcd(m, t, xx, y);
  x = ((xx % t) + t) % t;
}
int solve() {
  init();
  int res = 0, c = 0;
  int l = 0;

  int tt = t / 2;
  int s = 0;
  int i, j;
  int th = t / h;
  for (j = 0; j < tt; j += h) {
    mp[j] = (s < n);
    s += x;
    s %= th;
  }
  for (i = 0; i < tt; i += h) {
    if (mp[i]) {
      l = (l + u - 1) / u * u;
      res += max(0, (i - l - w + u) / u);
      l = i + 1;
    }
  }

  mp.reset();
  for (; j < t; j += h) {
    if (s < n) {
      mp[j - tt] = true;
    }
    s += x;
    s %= t / h;
  }
  for (; i < t; i += h) {
    if (mp[i - tt]) {
      l = (l + u - 1) / u * u;
      res += max(0, (i - l - w + u) / u);
      l = i + 1;
    }
  }
  l = (l + u - 1) / u * u;
  res += max(0, (t - l - w + u) / u);
  return res;
}

int main() {
  cin >> w >> d >> n >> m >> t;
  w++;
  d--;
  printf("%.20f\\n", 1.0 * solve() / (t / u));
  return 0;
}
Problem p01504 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#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 show(x) cout << #x << " " << x << endl
struct edge {
  int from, to, cost;
};
bool operator<(const edge &x, const edge &y) { return x.cost > y.cost; }
int par[10000], id[10000];
void init(int n) {
  rep(i, n) par[i] = i;
  rep(i, n) id[i] = -1;
}
int find(int x) {
  if (x == par[x])
    return x;
  else
    return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x), y = find(y);
  if (id[x] >= 0)
    par[y] = x;
  else
    par[x] = y;
}
vector<edge> es;
vector<int> costs;
int main() {
  int tt = 0;
  while (true) {
    tt++;
    int n, t, k;
    cin >> n >> t >> k;
    if (n == 0)
      break;
    es.clear();
    init(n);
    rep(i, n - 1) {
      int a, b, c;
      cin >> a >> b >> c;
      es.pb(edge{a - 1, b - 1, c});
    }
    rep(i, t) {
      int x;
      cin >> x;
      id[x - 1] = x - 1;
    }
    sort(all(es));
    costs.clear();
    rep(i, n - 1) {
      int a = es[i].from, b = es[i].to, c = es[i].cost;
      a = find(a), b = find(b);
      if (id[a] >= 0 && id[b] >= 0)
        costs.pb(c);
      else
        unite(a, b);
    }
    sort(all(costs));
    //		show(costs.size());
    int sum = 0;
    rep(i, k) sum += costs[i];
    printf("Case %d: %d\\n", tt, sum);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#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 fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
struct edge {
  int from, to, cost;
};
bool operator<(const edge &x, const edge &y) { return x.cost > y.cost; }
int par[10000], id[10000];
void init(int n) {
  rep(i, n) par[i] = i;
  rep(i, n) id[i] = -1;
}
int find(int x) {
  if (x == par[x])
    return x;
  else
    par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x), y = find(y);
  if (id[x] >= 0)
    par[y] = x;
  else
    par[x] = y;
}
vector<edge> es;
int main() {
  int tt = 0;
  while (true) {
    tt++;
    int n, t, k;
    cin >> n >> t >> k;
    if (n == 0)
      break;
    es.clear();
    init(n);
    rep(i, n - 1) {
      int a, b, c;
      cin >> a >> b >> c;
      es.pb(edge{a - 1, b - 1, c});
    }
    rep(i, t) {
      int x;
      cin >> x;
      id[x - 1] = x - 1;
    }
    sort(all(es));
    vector<int> costs;
    rep(i, n - 1) {
      int a = es[i].from, b = es[i].to, c = es[i].cost;
      a = find(a), b = find(b);
      if (id[a] >= 0 && id[b] >= 0)
        costs.pb(c);
      else
        unite(a, b);
    }
    sort(all(costs));
    //		show(costs.size());
    int sum = 0;
    rep(i, k) sum += costs[i];
    printf("Case %d: %d\\n", tt, sum);
  }
}
Problem p01504 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

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

  int n, t, k, case_num = 0;
  while (cin >> n >> t >> k, n) {
    case_num++;

    map<int, int> costs;
    vector<vector<pii>> tree(n);

    for (int i = 0; i < n - 1; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      tree[a].push_back(pii(c, b));
      tree[b].push_back(pii(c, a));
      costs[c]++;
    }

    vector<int> base(t), use(n, 0);
    for (int i = 0; i < t; i++) {
      cin >> base[i];
      base[i]--;
      use[base[i]] = 1;
    }

    priority_queue<pii> q;
    for (int v : base) {
      for (pii u : tree[v]) {
        if (!use[u.second])
          q.push(u);
      }
    }

    while (q.size()) {
      pii p = q.top();
      q.pop();
      if (use[p.second])
        continue;
      use[p.second] = 1;
      costs[p.first]--;

      for (pii u : tree[p.second]) {
        if (!use[u.second])
          q.push(u);
      }
    }

    long long res = 0;
    auto it = costs.begin();
    while (k) {
      while (k && it->second) {
        it->second--;
        k--;
        res += it->first;
      }
      it++;
    }
    cout << "Case " << case_num << ": " << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

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

  int n, t, k, case_num = 0;
  while (cin >> n >> t >> k, n) {
    case_num++;
    assert(case_num <= 60);

    set<int> costs;
    vector<vector<pii>> tree(n);

    for (int i = 0; i < n - 1; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      tree[a].push_back(pii(c, b));
      tree[b].push_back(pii(c, a));
      costs.insert(c);
    }

    vector<int> base(t), use(n, 0);
    for (int i = 0; i < t; i++) {
      cin >> base[i];
      base[i]--;
      use[base[i]] = 1;
    }

    priority_queue<pii> q;
    for (int v : base) {
      for (pii u : tree[v]) {
        if (!use[u.second])
          q.push(u);
      }
    }

    while (q.size()) {
      pii p = q.top();
      q.pop();
      if (use[p.second])
        continue;
      use[p.second] = 1;
      costs.erase(p.first);

      for (pii u : tree[p.second]) {
        if (use[u.second])
          continue;
        q.push(u);
      }
    }

    long long res = 0;
    auto it = costs.begin();
    for (int i = 0; i < k; i++) {
      res += *it;
      it++;
    }
    cout << "Case " << case_num << ": " << res << endl;
  }
}
Problem p01511 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

typedef vector<vector<int>> Mat;

long long W, H, N;

Mat operator*(const Mat &a, const Mat &b) {
  Mat m(W, vector<int>(W));
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = 0; k < W; k++) {
        m[i][j] = (m[i][j] + a[i][k] * 1LL * b[k][j]) % 1000000009;
      }
    }
  }
  return m;
}

Mat bm(const set<int> &s = set<int>()) {
  Mat m(W, vector<int>(W));
  for (int i = 0; i < W; i++) {
    if (s.count(i) == 0) {
      for (int j = -1; j <= 1; j++) {
        int x = i + j;
        if (0 <= x && x < W) {
          m[i][x] = 1;
        }
      }
    }
  }
  return m;
}

Mat pm(const Mat &m, long long p) {
  if (p == 0) {
    Mat m(W, vector<int>(W));
    for (int i = 0; i < W; i++) {
      m[i][i] = 1;
    }
    return m;
  } else {
    auto r = pm(m * m, p >> 1);
    if (p % 2) {
      r = r * m;
    }
    return r;
  }
}

int main() {
  for (int t = 1; cin >> W >> H >> N, W; t++) {
    map<long long, set<int>> m;
    while (N--) {
      long long x, y;
      cin >> x >> y;
      if (y > 1) {
        m[y - 1].insert(x - 1);
      }
    }
    vector<Mat> v;
    long long l = 0;
    for (auto e : m) {
      v.push_back(pm(bm(), e.first - l - 1));
      v.push_back(bm(e.second));
      l = e.first;
    }
    v.push_back(pm(bm(), H - l - 1));
    auto am = pm(bm(), 0);
    for (int i = v.size() - 1; i >= 0; i--) {
      am = am * v[i];
    }
    cout << "Case " << t << ": " << am[W - 1][0] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

typedef vector<vector<int>> Mat;

int W, H, N;

Mat operator*(const Mat &a, const Mat &b) {
  Mat m(W, vector<int>(W));
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = 0; k < W; k++) {
        m[i][j] = (m[i][j] + a[i][k] * 1LL * b[k][j]) % 1000000009;
      }
    }
  }
  return m;
}

Mat bm(const set<int> &s = set<int>()) {
  Mat m(W, vector<int>(W));
  for (int i = 0; i < W; i++) {
    if (s.count(i) == 0) {
      for (int j = -1; j <= 1; j++) {
        int x = i + j;
        if (0 <= x && x < W) {
          m[i][x] = 1;
        }
      }
    }
  }
  return m;
}

Mat pm(const Mat &m, long long p) {
  if (p == 0) {
    Mat m(W, vector<int>(W));
    for (int i = 0; i < W; i++) {
      m[i][i] = 1;
    }
    return m;
  } else {
    auto r = pm(m * m, p >> 1);
    if (p % 2) {
      r = r * m;
    }
    return r;
  }
}

int main() {
  for (int t = 1; cin >> W >> H >> N, W; t++) {
    map<long long, set<int>> m;
    while (N--) {
      long long x, y;
      cin >> x >> y;
      m[y - 1].insert(x - 1);
    }
    vector<Mat> v;
    long long l = -1;
    for (auto e : m) {
      v.push_back(pm(bm(), e.first - l - 1));
      v.push_back(bm(e.second));
      l = e.first;
    }
    v.push_back(pm(bm(), H - l - 1));
    auto am = pm(bm(), 0);
    for (int i = v.size() - 1; i >= 0; i--) {
      am = am * v[i];
    }
    cout << "Case " << t << ": " << am[W - 1][0] << endl;
  }
}
Problem p01511 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;
const int mod = 1e9 + 9;

typedef vector<int> vec;
typedef vector<vec> mat;

mat operator*(const mat A, const mat B) {
  mat ret(A.size(), vec(B[0].size(), 0));
  for (int i = 0; i < A.size(); i++) {
    for (int j = 0; j < B[0].size(); j++) {
      for (int k = 0; k < B.size(); k++) {
        (ret[i][j] += 1LL * A[i][k] * B[k][j] % mod) %= mod;
      }
    }
  }
  return (ret);
}

mat operator^(mat A, int64 n) {
  mat ret = mat(A.size(), vec(A.size(), 0));
  for (int i = 0; i < A.size(); i++)
    ret[i][i] = 1;
  while (n > 0) {
    if (n & 1)
      (ret = ret * A);
    A = A * A;
    n >>= 1LL;
  }
  return (ret);
}

int main() {
  int64 W, H, N;
  int cc = 1;
  while (cin >> W >> H >> N, W) {
    map<int64, vector<int>> vs;
    vs.emplace(H, vector<int>());
    for (int i = 0; i < N; i++) {
      int64 x, y;
      cin >> x >> y;
      vs[y].push_back(x);
    }
    mat ma(W, vec(W, 0));
    for (int i = 0; i < W; i++) {
      for (int j = max(0, i - 1); j < min(W, i + 2LL); j++)
        ma[i][j] = 1;
    }
    mat beet(W, vec(1, 0));
    beet[0][0] = 1;
    int64 rev = 1;
    for (auto &p : vs) {
      beet = (ma ^ (p.first - rev)) * beet;
      for (auto &q : p.second)
        beet[q - 1][0] = 0;
      rev = p.first;
    }
    cout << "Case " << cc++ << ": " << beet[W - 1][0] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;
const int mod = 1e9 + 7;

typedef vector<int> vec;
typedef vector<vec> mat;

mat operator*(const mat &a, const mat &b) {
  mat ret(a.size(), vec(b[0].size(), 0));
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b[0].size(); j++) {
      for (int k = 0; k < b.size(); k++) {
        (ret[i][j] += 1LL * a[i][k] * b[k][j] % mod) %= mod;
      }
    }
  }
  return (ret);
}

mat operator^(mat a, int64 n) {
  mat ret = mat(a.size(), vec(a.size(), 0));
  for (int i = 0; i < a.size(); i++)
    ret[i][i] = 1;
  while (n > 0) {
    if (n & 1)
      (ret = ret * a);
    a = a * a;
    n >>= 1LL;
  }
  return (ret);
}

int main() {
  int64 W, H, N;
  int cc = 1;
  while (cin >> W >> H >> N, W) {
    map<int64, vector<int>> vs;
    for (int i = 0; i < N; i++) {
      int x, y;
      cin >> x >> y;
      vs[y].push_back(x);
    }
    vs[H].push_back(1);
    mat ma(W, vec(W, 0));
    for (int i = 0; i < W; i++) {
      for (int j = max(0, i - 1); j < min(W, i + 2LL); j++) {
        ma[i][j] = 1;
      }
    }
    mat beet(W, vec(1, 0));
    beet[0][0] = 1;
    int64 rev = 1;
    for (auto &p : vs) {
      beet = (ma ^ (p.first - rev)) * beet;
      for (auto &q : p.second)
        beet[q - 1][0] = 0;
      rev = p.first;
    }
    cout << "Case " << cc++ << ": " << beet[W - 1][0] << endl;
  }
}
Problem p01520 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

signed main() {
  int n, t, e;
  cin >> n >> t >> e;
  vi x(n);
  rep(i, 0, n) cin >> x[i];
  int ans = -1;
  rep(i, 0, n) {
    for (int j = 1;; j++) {
      if (t - e <= j * x[i] && j * x[i] <= t + e) {
        ans = i + 1;
        i = n;
        break;
      }
      if (j * x[i] > t + e)
        break;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

signed main() {
  int n, t, e;
  cin >> n >> t >> e;
  vi x(n);
  rep(i, 0, n) cin >> x[i];
  int ans = -1;
  rep(i, 0, n) {
    for (int j = 1;; j++) {
      if (t - e <= j * x[i] && j * x[i] <= t + e) {
        ans = i + 1;
        i = n;
      }
      if (j * x[i] > t + e)
        break;
    }
  }
  cout << ans << endl;
}
Problem p01521 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999

#define maru 0
#define batu 1

int dfs(string &data, int player, int left, int right) {
  if (left == right)
    return data[left] - '0';

  bool dfsed = false;
  if (data[left] != '0' + player) {
    dfsed = true;
    if (dfs(data, !player, left + 1, right) == player)
      return player;
  }
  if (data[right] != '0' + player) {
    dfsed = true;
    if (dfs(data, !player, left, right - 1) == player)
      return player;
  }
  if (!dfsed)
    if (dfs(data, !player, left, right) == player)
      return player;
  return !player;
}

int main() {
  string s;
  cin >> s;
  string data = "";
  rep(i, s.size()) if (i == 0 || s[i - 1] != s[i]) if (s[i] == 'o') data += '0';
  else data += '1';

  bool res = dfs(data, maru, 0, data.size() - 1);

  if (res == maru)
    cout << 'o' << endl;
  else
    cout << 'x' << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999

#define maru 0
#define batu 1

char ppp[] = {'o', 'x'};
int dfs(string data, int player) {
  if (data.size() == 1) {
    if (data[0] == 'o')
      return maru;
    return batu;
  }

  bool dfsed = false;
  char ch = ppp[player];
  if (data[0] != ch) {
    dfsed = true;
    if (dfs(data.substr(1, data.size() - 1), !player) == player)
      return player;
  }
  if (data[data.size() - 1] != ch) {
    dfsed = true;
    if (dfs(data.substr(0, data.size() - 1), !player) == player)
      return player;
  }
  if (!dfsed)
    if (dfs(data, !player) == player)
      return player;
  return !player;
}

int main() {
  string s;
  cin >> s;
  string data = "";
  rep(i, s.size()) if (i == 0 || s[i - 1] != s[i]) data += s[i];

  bool res = dfs(data, maru);

  if (res == maru)
    cout << 'o' << endl;
  else
    cout << 'x' << endl;
}
Problem p01521 (C++) — buggy submission, verdict: Time Limit Exceeded

//============================================================================
// Name        : 2407.cpp
// Author      : afterCmidday
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;

int main(void) {
  string str;
  cin >> str;
  if (str[0] == str[str.size() - 1]) {
    cout << str[0] << endl;
  } else {
    cout << 'o' << endl;
  }
}

Error: TLE

Fixed submission:

//============================================================================
// Name        : 2407.cpp
// Author      : afterCmidday
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;

char enemy(char ch) {
  if (ch == 'o') {
    return 'x';
  } else if (ch == 'x') {
    return 'o';
  } else {
    return 'e';
  }
}

char dfs(string cr, char me, int pass) {
  bool puttable = false;
  if (pass == 2) {
    int o_cnt = 0, x_cnt = 0;
    for (int i = 0; i < cr.size(); i++) {
      if (cr[i] == 'o') {
        o_cnt++;
      } else {
        x_cnt++;
      }
    }
    if (o_cnt > x_cnt) {
      return 'o';
    } else if (o_cnt < x_cnt) {
      return 'x';
    }
  }
  if (cr[0] == enemy(me)) {
    for (int i = 1; i < cr.size(); i++) {
      if (cr[i] == me) { // 石が置けるかを判定.
        string tmp = "";
        tmp.append(i + 2, me);
        tmp.append(cr.substr(i + 1, cr.size() - i - 1));
        // cout << cr << " " << tmp << endl;
        if (me == dfs(tmp, enemy(me), 0)) {
          return me;
        }
        puttable = true;
      }
    }
  }
  if (cr[cr.size() - 1] == enemy(me)) {
    for (int i = cr.size() - 2; i >= 0; i--) {
      if (cr[i] == me) { // 石が置けるかを判定.
        string tmp = cr.substr(0, i);
        tmp.append(cr.size() - i, me);
        // cout << cr << " " << tmp << endl;
        if (me == dfs(tmp, enemy(me), 0)) {
          return me;
        }
        puttable = true;
      }
    }
  }
  if (!puttable) {
    return dfs(cr, enemy(me), pass + 1);
  }
  return enemy(me);
}

int main() {
  string cr;
  cin >> cr;
  if (dfs(cr, 'o', 0) == 'o') {
    cout << 'o' << endl;
  } else {
    cout << 'x' << endl;
  }
  return 0;
}
Problem p01522 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <set>

using namespace std;

int main() {
  int n, k, r;
  int m[50], p[5000], q[5000];
  int bunny[50][50];
  int count = 0;
  set<int> all, selected;
  cin >> n >> k;
  for (int i = 0; i < k; ++i) {
    cin >> m[i];
    for (int j = 0; j < m[i]; ++j) {
      cin >> bunny[i][j];
    }
  }
  cin >> r;
  for (int i = 0; i < r; ++i) {
    cin >> p[i] >> q[i];
  }
  for (int i = 0; i < k; ++i) {
    all.clear();
    selected.clear();
    for (int j = 0; j < m[i]; ++j) {
      all.insert(bunny[i][j]);
    }
    for (int j = 0; j < r; ++j) {
      if (all.count(p[j]) * all.count(q[j]) > 0) {
        selected.insert(p[j]);
        selected.insert(q[j]);
      }
    }
    count += selected.size();
  }
  cout << count << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <set>

using namespace std;

int main() {
  int n, k, r;
  int m[50], p[50], q[50];
  int bunny[50][50];
  int count = 0;
  set<int> all, selected;
  cin >> n >> k;
  for (int i = 0; i < k; ++i) {
    cin >> m[i];
    for (int j = 0; j < m[i]; ++j) {
      cin >> bunny[i][j];
    }
  }
  cin >> r;
  for (int i = 0; i < r; ++i) {
    cin >> p[i] >> q[i];
  }
  for (int i = 0; i < k; ++i) {
    all.clear();
    selected.clear();
    for (int j = 0; j < m[i]; ++j) {
      all.insert(bunny[i][j]);
    }
    for (int j = 0; j < r; ++j) {
      if (all.count(p[j]) * all.count(q[j]) > 0) {
        selected.insert(p[j]);
        selected.insert(q[j]);
      }
    }
    count += selected.size();
  }
  cout << count << endl;
  return 0;
}
Problem p01522 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  int n, k, w, p, m, x, y, cnt;
  bool t[51][51], c[51];
  char a;
  cin >> n >> k;

  cnt = 0;
  for (int i = 0; i < 51; i++) {
    c[i] = false;
    for (int j = 0; j < 51; j++)
      t[i][j] = false;
  }
  for (int i = 0; i < k; i++) {
    cin >> m;
    for (int j = 0; j < m; j++) {
      cin >> w;
      t[i][w] = true;
    }
  }
  cin >> p;
  for (int i = 0; i < p; i++) {
    cin >> x >> y;
    for (int j = 0; j < k; j++) {
      if (t[j][x] && t[j][y]) {
        c[x] = c[y] = true;
        break;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (c[i])
      cnt++;
  }
  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  int n, k, w, p, x, y, cnt;
  bool t[51][51], c[51];
  char a;
  cin >> n >> k;

  cnt = 0;
  for (int i = 0; i < 51; i++) {
    c[i] = false;
    for (int j = 0; j < 51; j++)
      t[i][j] = false;
  }
  for (int i = 0; i < k; k++) {
    cin >> w;
    t[i][w] = true;
    scanf("%c", &a);
    if (a != ' ')
      continue;
  }
  cin >> p;
  for (int i = 0; i < p; i++) {
    cin >> x >> y;
    for (int j = 0; j < k; j++) {
      if (t[j][x] && t[j][y]) {
        c[x] = c[y] = true;
        break;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (c[i])
      cnt++;
  }
  cout << cnt << endl;

  return 0;
}