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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
#include <vector>
#define SIZE 20005

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

ll A[SIZE];
P pos[SIZE];
int n;

bool check(int x, int k) // x 桁目
{
  ll rt = 1;
  for (int i = 0; i < x; i++)
    rt *= 10LL;
  int sz = 0;
  pos[sz++] = P(rt * (ll)k, rt * (ll)(k + 1) - 1);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < sz; j++) {
      if (pos[j].first <= A[i] % (rt * 10LL) &&
          A[i] % (rt * 10LL) <= pos[j].second) {
        return true;
      }
    }
    int ns = sz;
    for (int j = 0; j < sz; j++) {
      P p = pos[j];
      p.first -= A[i] % (rt * 10LL);
      p.second -= A[i] % (rt * 10LL);
      if (p.first < 0)
        p.first += rt * 10LL;
      if (p.second < 0)
        p.second += rt * 10LL;
      if (p.first <= p.second)
        pos[ns++] = p;
      else {
        pos[ns++] = P(p.first, rt * 10LL - 1);
        pos[ns++] = P(0, p.second);
      }
    }
    sort(pos, pos + ns);
    sz = 0;
    for (int j = 0; j < ns;) {
      P p = pos[j];
      for (; j < ns && pos[j].first <= p.second; j++)
        p.second = max(p.second, pos[j].second);
      pos[sz++] = p;
    }
  }
  return false;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &A[i]);
  int ret = 0;
  for (int i = 0; i < 20; i++) {
    int nm = 0;
    for (int j = 0; j < 10; j++)
      if (check(i, j))
        nm = j;
    ret += nm;
  }
  printf("%d\n", ret);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
#include <vector>
#define SIZE 20005

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

ll A[SIZE];
P pos[SIZE];
int n;

bool check(int x, int k) // x 桁目
{
  ll rt = 1;
  for (int i = 0; i < x; i++)
    rt *= 10LL;
  int sz = 0;
  pos[sz++] = P(rt * (ll)k, rt * (ll)(k + 1) - 1);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < sz; j++) {
      if (pos[j].first <= A[i] % (rt * 10LL) &&
          A[i] % (rt * 10LL) <= pos[j].second) {
        return true;
      }
    }
    int ns = sz;
    for (int j = 0; j < sz; j++) {
      P p = pos[j];
      p.first -= A[i] % (rt * 10LL);
      p.second -= A[i] % (rt * 10LL);
      if (p.first < 0)
        p.first += rt * 10LL;
      if (p.second < 0)
        p.second += rt * 10LL;
      if (p.first <= p.second)
        pos[ns++] = p;
      else {
        pos[ns++] = P(p.first, rt * 10LL - 1);
        pos[ns++] = P(0, p.second);
      }
    }
    sort(pos, pos + ns);
    sz = 0;
    for (int j = 0; j < ns;) {
      P p = pos[j];
      for (; j < ns && pos[j].first <= p.second; j++)
        p.second = max(p.second, pos[j].second);
      pos[sz++] = p;
    }
  }
  return false;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &A[i]);
  int ret = 0;
  for (int i = 0; i < 17; i++) {
    int nm = 0;
    for (int j = 0; j < 10; j++)
      if (check(i, j))
        nm = j;
    ret += nm;
  }
  printf("%d\n", ret);
  return 0;
}
Problem p03908 (C++)

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

using namespace std;

int main() {
  long long int N;
  cin >> N;
  if (N < 10) {
    return 1;
  }
  vector<long long int> A(N);
  for (long long int i = 0; i < N; i++) {
    cin >> A[i];
  }
  // cout << "!" << endl;
  long long int x = 1LL, ans = 0LL;
  for (long long int i = 0; i < 20; i++) {
    vector<long long int> DP(10, LLONG_MIN / 6);
    DP[0] = 0;
    long long int maxx = 0;
    for (long long int k = 0; k < N; k++) {
      auto dp = DP;
      DP[(A[k] / x) % 10LL] = max(A[k] % x, DP[(A[k] / x) % 10LL]);
      maxx = max((A[k] / x) % 10LL, maxx);
      for (long long int j = 0; j < 10; j++) {
        if (dp[j] >= 0LL) {
          long long int next = (dp[j] + A[k] % (x * 10LL) + x * j) % (x * 10LL);
          long long int nextidx = (next / x) % 10LL;
          DP[nextidx] = max(dp[nextidx], next % x);
          maxx = max(nextidx, maxx);
        }
      }
    }
    ans += maxx;

    // for( size_t i = 0; i < 10; i++ ) {
    //	cout << DP[i] << " ";
    // }
    // cout << endl;

    // cout << maxx << endl;
    x *= 10LL;
  }
  cout << ans << endl;
}

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

using namespace std;

int main() {
  long long int N;
  cin >> N;
  if (N == 5) {
    vector<long long int> A(N);
    for (size_t i = 0; i < N; i++) {
      cin >> A[i];
    }
    long long int x = 1, ans = 0;
    while (true) {
      vector<long long int> DP(10, LLONG_MIN / 6);
      DP[0] = 0;
      long long int maxx = 0;
      for (size_t i = 0; i < N; i++) {
        auto dp = DP;
        for (size_t j = 0; j < 10; j++) {
          if (dp[j] >= 0) {
            long long int next = dp[j] + A[i];
            long long int nextidx = (next / x) % 10;
            DP[nextidx] = max(DP[nextidx], next % (x * 100));
            maxx = max(nextidx, maxx);
          }
        }
      }
      if (maxx == 0) {
        break;
      } else {
        ans += maxx;
      }
      for (size_t i = 0; i < 10; i++) {
        // cout << DP[i] << " ";
      }
      // cout << endl;
      x *= 10;
    }
    cout << ans << endl;
    return 0;
  }
  vector<long long int> A(N);
  for (long long int i = 0; i < N; i++) {
    cin >> A[i];
  }
  // cout << "!" << endl;
  long long int x = 1LL, ans = 0LL;
  for (long long int i = 0; i < 20; i++) {
    vector<long long int> DP(10, LLONG_MIN / 6);
    DP[0] = 0;
    long long int maxx = 0;
    for (long long int k = 0; k < N; k++) {
      auto dp = DP;
      DP[(A[k] / x) % 10LL] = max(A[k] % x, DP[(A[k] / x) % 10LL]);
      maxx = max((A[k] / x) % 10LL, maxx);
      for (long long int j = 0; j < 10; j++) {
        if (dp[j] >= 0LL) {
          long long int next = (dp[j] + A[k] % (x * 10LL) + x * j) % (x * 10LL);
          long long int nextidx = (next / x) % 10LL;
          DP[nextidx] = max(dp[nextidx], next % x);
          maxx = max(nextidx, maxx);
        }
      }
    }
    ans += maxx;

    // for( size_t i = 0; i < 10; i++ ) {
    //	cout << DP[i] << " ";
    // }
    // cout << endl;

    // cout << maxx << endl;
    x *= 10LL;
  }
  cout << ans << endl;
}
Problem p03909 (C++)

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<string>> S(H, vector<string>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      cin >> S.at(i).at(j);
  }
  string alp = "ABCDEFGHIJ";
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (S.at(y).at(x) == "snuke") {
        cout << alp.at(x) << to_string(y + 1) << endl;
        return 0;
      }
    }
  }
}

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<string>> S(H, vector<string>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      cin >> S.at(i).at(j);
  }
  string alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (S.at(y).at(x) == "snuke") {
        cout << alp.at(x) << to_string(y + 1) << endl;
        return 0;
      }
    }
  }
}
Problem p03909 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) (v).begin(), (v).end()

using namespace std;
using ll = long long;

int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      string s;
      cin >> s;
      if (s == "snuke") {
        printf("%s\n", j + 'A', i + 1);
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) (v).begin(), (v).end()

using namespace std;
using ll = long long;

int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      string s;
      cin >> s;
      if (s == "snuke") {
        cout << char(j + 'A') << i + 1 << endl;
      }
    }
  }
}
Problem p03910 (Python)

Buggy submission (status: Time Limit Exceeded):
# 最大値の最小値、ちょうどn/2臭い
# あとは帳尻合わせが効きそう
# 14とかだと7+4+2+1 （7+5+2でも7+4+3でもいい
# かと思いきや 1+2+3+4+5で15いくので, 5+4+3+2でよかった
# 10^7なので毎回足してもどうせ間に合う。これで答えの集合のうち最大値は確定
# あとはそれでnを引いて0になるまで

n = int(input())
ans = []
cur = 0

while cur != n:
    tmp = 0
    for i in range(1, n + 1 - cur):
        if i + tmp >= n - cur:
            tmp = i
            break
        tmp += i
    ans.append(tmp)
    cur += tmp
    # print("tmp = {}, cur = {}".format(tmp, cur))
# print(ans)
for a in ans:
    print(a)


Fixed submission:
# 最大値の最小値、ちょうどn/2臭い
# あとは帳尻合わせが効きそう
# 14とかだと7+4+2+1 （7+5+2でも7+4+3でもいい
# かと思いきや 1+2+3+4+5で15いくので, 5+4+3+2でよかった
# 10^7なので毎回足してもどうせ間に合う。これで答えの集合のうち最大値は確定
# あとはそれでnを引いて0になるまで -> これだと間に合わない

n = int(input())
ans = []
cur = 0

while cur != n:
    tmp = 0
    for i in range(1, n + 1 - cur):
        if i + tmp >= n - cur:
            tmp = i
            break
        tmp += i
    ans.append(tmp)
    cur += tmp
    # print("tmp = {}, cur = {}".format(tmp, cur))
# print(ans)
for a in ans:
    print(a)
Problem p03910 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
typedef long long int LL;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  int N;
  cin >> N;

  int sum = 0;
  vector<int> a;
  int i = 0;
  while (true) {
    i++;
    sum += i;
    a.push_back(i);
    if (sum >= N) {
      a[sum - N - 1] = -1;
      break;
    }
  }
  REP(j, a.size()) {
    if (a[j] != -1) {
      cout << a[j] << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
typedef long long int LL;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  int N;
  cin >> N;

  int sum = 0;
  vector<int> a;
  int i = 0;
  while (true) {
    i++;
    sum += i;
    a.push_back(i);
    if (sum >= N) {
      if (sum > N) {
        a[sum - N - 1] = -1;
      }
      break;
    }
  }
  REP(j, a.size()) {
    if (a[j] != -1) {
      cout << a[j] << endl;
    }
  }
}
Problem p03910 (C++)

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

using namespace std;

int main(int argc, char *argv[]) {
  long double N;
  cin >> N;

  long long X = ceil((-1.0 + sqrt(1.0 + 8.0 * N)) / 2.0);

  vector<long long> ret;
  ret.push_back(X);
  long long cur = N;
  cur -= X;
  while (true) {
    if (cur <= X - 1) {
      ret.push_back(cur);
      break;
    } else {
      X = min(cur - X + 1, X - 1);
      if (X > cur)
        X = cur;
      ret.push_back(X);
      cur -= X;
      if (cur == 0)
        break;
    }
  }

  for (int i = ret.size() - 1; i >= 0; --i) {
    if (ret[i] == 0)
      continue;
    cout << ret[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  long double N;
  cin >> N;

  long long X = ceil((-1.0 + sqrt(1.0 + 8.0 * N)) / 2.0);

  vector<long long> ret;
  ret.push_back(X);
  long long cur = N;
  cur -= X;
  while (true) {
    if (cur <= X - 1) {
      ret.push_back(cur);
      break;
    } else {
      X = X - 1;
      if (X > cur)
        X = cur;
      ret.push_back(X);
      cur -= X;
      if (cur == 0)
        break;
    }
  }

  for (int i = ret.size() - 1; i >= 0; --i) {
    if (ret[i] == 0)
      continue;
    cout << ret[i] << endl;
  }

  return 0;
}
Problem p03910 (C++)

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

int main() {
  cin.tie(0)->sync_with_stdio(false);

  int n;
  cin >> n;

  vector<int> ans;
  if (n == 1)
    ans.push_back(1);
  else if (n == 2)
    ans.push_back(2);
  else {
    while (true) {
      int tmp = 0;
      int cnt = 0;
      REP(i, n) {
        tmp += i;
        if (n <= tmp) {
          cnt = i;
          break;
        }
      }
      n -= cnt;
      ans.push_back(cnt);
      if (n == 1) {
        ans.push_back(n);
        break;
      }
    }
  }

  for (int i = ans.size() - 1; i >= 0; i--)
    cout << ans[i] << '\n';
  return 0;
}

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

int main() {
  cin.tie(0)->sync_with_stdio(false);

  int n;
  cin >> n;

  vector<int> ans;
  if (n == 1)
    ans.push_back(1);
  else if (n == 2)
    ans.push_back(2);
  else {
    while (true) {
      int tmp = 0;
      int cnt = 0;
      REP(i, n) {
        tmp += i;
        if (n <= tmp) {
          cnt = i;
          break;
        }
      }
      n -= cnt;
      ans.push_back(cnt);
      if (n == 1) {
        ans.push_back(n);
        break;
      } else if (n == 2) {
        ans.push_back(n);
        break;
      }
    }
  }

  for (int i = ans.size() - 1; i >= 0; i--)
    cout << ans[i] << '\n';
  return 0;
}
Problem p03910 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;

int main() {
  ll n;
  cin >> n;
  ll sum = 0;
  ll l = 0;
  ll k;
  for (int i = 0; i < n; i++) {
    sum = sum + i;
    if (sum > n) {
      l = sum - n;
      k = i;
      break;
    } else if (sum == n) {
      k = i;
      break;
    }
  }
  for (ll i = 1; i < k + 1; i++) {
    if (i != l)
      cout << i << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;

int main() {
  ll n;
  cin >> n;
  ll sum = 0;
  ll l = 0;
  ll k;
  if (n == 1 || n == 2) {
    cout << n << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    sum = sum + i;
    if (sum > n) {
      l = sum - n;
      k = i;
      break;
    } else if (sum == n) {
      k = i;
      break;
    }
  }
  for (ll i = 1; i < k + 1; i++) {
    if (i != l)
      cout << i << endl;
  }
}
Problem p03910 (C++)

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

using namespace std;

int main() {
  int n = 0;
  cin >> n;
  int point = 0, i = 0;
  for (i = 1; i <= n; i++) {
    point += i;
    if (point >= n) {
      break;
    }
  }
  for (int j = 1; i <= i; j++) {
    if (j == point - n)
      continue;
    cout << j << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n = 0;
  cin >> n;
  int point = 0, i = 0;
  for (i = 1; i <= n; i++) {
    point += i;
    if (point >= n) {
      break;
    }
  }
  for (int j = 1; j <= i; j++) {
    if (j == point - n)
      continue;
    cout << j << endl;
  }
  return 0;
}
Problem p03910 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[100001];
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
inline void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  int n = read(), s = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = i;
    s += i;
    if (s > n)
      break;
  }
  int t = s - n;
  for (int i = 1; i <= n; i++) {
    if (a[i]) {
      if (a[i] != t) {
        write(a[i]);
        printf("\n");
      }
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[10000001];
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
inline void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  int n = read(), s = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = i;
    s += i;
    if (s > n)
      break;
  }
  int t = s - n;
  for (int i = 1; i <= n; i++) {
    if (a[i]) {
      if (a[i] != t) {
        write(a[i]);
        printf("\n");
      }
    }
  }
  return 0;
}
Problem p03910 (C++)

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

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

#define in_range(x, y, w, h)                                                   \
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }

  int tmp = 0;
  int mx = 0;
  for (int i = 1; i < N; i++) {
    tmp += i;
    if (tmp >= N) {
      // DUMP2(i, tmp);
      mx = i;
      break;
    }
  }

  while (N > 0 && mx > 0) {
    if (N >= mx) {
      N -= mx;
      cout << mx << endl;
    }
    mx--;
  }
  assert(N == 0);
}


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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

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

#define in_range(x, y, w, h)                                                   \
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }

  int tmp = 0;
  int mx = 0;
  for (int i = 1;; i++) {
    tmp += i;
    if (tmp >= N) {
      // DUMP2(i, tmp);
      mx = i;
      break;
    }
  }

  while (N > 0 && mx > 0) {
    if (N >= mx) {
      N -= mx;
      cout << mx << endl;
    }
    mx--;
  }
  assert(N == 0);
}
Problem p03910 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define INIT                                                                   \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false);

template <class T> inline bool chmax(T &a, T b) { return a = (a < b) ? b : a; }
template <class T> inline bool chmin(T &a, T b) { return a = (a > b) ? b : a; }

ll const INF = 1LL << 60;
ll const MOD = 1000000007;

int main() {
  INIT;

  ll N;
  cin >> N;

  vector<ll> sum(N, 0);
  sum[0] = 0;
  rep(i, N) { sum[i + 1] = sum[i] + i + 1; }
  // repv(sum) cout << *it << " ";
  // cout << endl;

  ll idx = lower_bound(ALL(sum), N) - sum.begin();
  cout << idx << endl;

  while (1) {
    N -= idx;
    idx = lower_bound(ALL(sum), N) - sum.begin();

    if (idx == 0)
      break;
    cout << idx << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define INIT                                                                   \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false);

template <class T> inline bool chmax(T &a, T b) { return a = (a < b) ? b : a; }
template <class T> inline bool chmin(T &a, T b) { return a = (a > b) ? b : a; }

ll const INF = 1LL << 60;
ll const MOD = 1000000007;

int main() {
  INIT;

  ll N;
  cin >> N;

  vector<ll> sum(N, 0);
  sum[0] = 0;
  rep(i, N - 1) { sum[i + 1] = sum[i] + i + 1; }

  // repv(sum) cout << *it << " ";
  // cout << endl;

  ll idx = lower_bound(ALL(sum), N) - sum.begin();
  cout << idx << endl;

  while (1) {
    N -= idx;
    idx = lower_bound(ALL(sum), N) - sum.begin();

    if (idx == 0)
      break;
    cout << idx << endl;
  }

  return 0;
}
Problem p03911 (C++)

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

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

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

const int MAX = 1e5 + 5;
const int INF = 1001001001;
const int MOD = 1e9 + 7;

vector<int> G[MAX];
vector<bool> used;
void dfs(int n) {
  used[n] = true;
  for (int next : G[n]) {
    if (!used[next])
      dfs(next);
  }
}

int main() {
  int N, M;
  cin >> N >> M;

  rep(i, N) {
    int K;
    cin >> K;

    rep(j, K) {
      int L;
      cin >> L;

      int lang = N + L - 1;
      G[i].push_back(lang);
      G[lang].push_back(i);
    }
  }

  used = vector<bool>(N + M, false);
  dfs(0);

  bool ans = true;
  rep(i, N) ans &= used[i];

  if (ans)
    puts("YES");
  else
    puts("NO");
}

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

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

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

const int MAX = 2 * 1e5 + 5;
const int INF = 1001001001;
const int MOD = 1e9 + 7;

vector<int> G[MAX];
vector<bool> used;
void dfs(int n) {
  used[n] = true;
  for (int next : G[n]) {
    if (!used[next])
      dfs(next);
  }
}

int main() {
  int N, M;
  cin >> N >> M;

  rep(i, N) {
    int K;
    cin >> K;

    rep(j, K) {
      int L;
      cin >> L;

      int lang = N + L - 1;
      G[i].push_back(lang);
      G[lang].push_back(i);
    }
  }

  used = vector<bool>(N + M, false);
  dfs(0);

  bool ans = true;
  rep(i, N) ans &= used[i];

  if (ans)
    puts("YES");
  else
    puts("NO");
}
Problem p03911 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// UnionFind
// coding: https://youtu.be/TdR816rqc3s?t=726
// comment: https://youtu.be/TdR816rqc3s?t=6822
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> lang(M);
  for (int i = 0; i < M; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int l;
      cin >> l;
      --l;
      lang[l].push_back(i);
    }
  }
  UnionFind uf = UnionFind(N);
  for (int i = 0; i < M; i++) {
    if (lang[i].size() > 1) {
      int t = lang[i][0];
      for (int v : lang[i]) {
        uf.unite(t, v);
      }
    }
  }
  if (uf.size(0) == N) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// UnionFind
// coding: https://youtu.be/TdR816rqc3s?t=726
// comment: https://youtu.be/TdR816rqc3s?t=6822
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> lang(M);
  for (int i = 0; i < N; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int l;
      cin >> l;
      --l;
      lang[l].push_back(i);
    }
  }
  UnionFind uf = UnionFind(N);
  for (int i = 0; i < M; i++) {
    if (lang[i].size() > 1) {
      int t = lang[i][0];
      for (int v : lang[i]) {
        uf.unite(t, v);
      }
    }
  }
  if (uf.size(0) == N) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}
Problem p03911 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

const int MAX_N = 200010;

int par[MAX_N];
int Rank[MAX_N];
int size;

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

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

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  size--;
  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y]) {
      Rank[x]++;
    }
  }
}
bool same(int x, int y) { return find(x) == find(y); }

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  init(M + N);
  rep(i, N) {
    int K;
    cin >> K;
    rep(j, K) {
      int L;
      cin >> L;
      L--;
      unite(i, N + L);
    }
  }
  rep(i, N) {
    rep(j, N) {
      if (!same(i, j)) {
        cout << "NO" << endl;
        return 0;
      }
    }
  }
  cout << "YES" << endl;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

const int MAX_N = 200010;

int par[MAX_N];
int Rank[MAX_N];
int size;

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

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

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  size--;
  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y]) {
      Rank[x]++;
    }
  }
}
bool same(int x, int y) { return find(x) == find(y); }

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  init(M + N);
  rep(i, N) {
    int K;
    cin >> K;
    rep(j, K) {
      int L;
      cin >> L;
      L--;
      unite(i, N + L);
    }
  }
  rep(i, N) {
    if (!same(0, i)) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;

  return 0;
}
Problem p03911 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/cf16-final/tasks/codefestival_2016_final_c
// https://atcoder.jp/contests/abc073/tasks/abc073_d
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()

struct UnionFind {
  vector<int> par; // 親の番号
  UnionFind(int size) : par(size, -1) {}
  // xとyの木を併合
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (par[y] < par[x])
        swap(x, y);
      par[x] += par[y];
      par[y] = x;
      return true;
    } else {
      return false;
    }
  }
  // 2つのデータx, yが属する木が同じならtrueを返す
  bool same(int x, int y) { return root(x) == root(y); }
  // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
  int root(int x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
  // 頂点 v が属するグループと同じグループに属する頂点数を得る
  int size(int x) { return -par[root(x)]; }
};
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M, K, L;
  cin >> N >> M;
  UnionFind uf(N + M);
  REP(i, N) {
    cin >> K;
    REP(j, K) {
      cin >> L;
      uf.unite(L - 1, M + i);
    }
  }
  bool flag = true;
  FOR(i, 1, N) {
    if (!uf.same(M, M + i + 1)) {
      flag = false;
      break;
    }
  }

  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}


Fixed submission:
// https://atcoder.jp/contests/cf16-final/tasks/codefestival_2016_final_c
// https://atcoder.jp/contests/abc073/tasks/abc073_d
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()

struct UnionFind {
  vector<int> par; // 親の番号
  UnionFind(int size) : par(size, -1) {}
  // xとyの木を併合
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (par[y] < par[x])
        swap(x, y);
      par[x] += par[y];
      par[y] = x;
      return true;
    } else {
      return false;
    }
  }
  // 2つのデータx, yが属する木が同じならtrueを返す
  bool same(int x, int y) { return root(x) == root(y); }
  // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
  int root(int x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
  // 頂点 v が属するグループと同じグループに属する頂点数を得る
  int size(int x) { return -par[root(x)]; }
};
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M, K, L;
  cin >> N >> M;
  UnionFind uf(N + M);
  REP(i, N) {
    cin >> K;
    REP(j, K) {
      cin >> L;
      uf.unite(L - 1, M + i);
    }
  }
  bool flag = true;
  FOR(i, 1, N) {
    if (!uf.same(M, M + i)) {
      flag = false;
      break;
    }
  }

  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}
Problem p03911 (C++)

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

const int N = 100005;

int n, m;
int par[N];

int anc(int p) { return p == par[p] ? p : par[p] = anc(par[p]); }
void join(int p, int q) { par[anc(p)] = anc(q); }

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n + m; ++i)
    par[i] = i;

  for (int i = 1; i <= n; ++i) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int x;
      scanf("%d", &x);
      join(i, x + n);
    }
  }

  int p = anc(1);
  for (int i = 1; i <= n; ++i)
    if (anc(i) != p) {
      cout << "NO\n";
      return 0;
    }
  cout << "YES\n";
}

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

const int N = 100005;

int n, m;
int par[N + N];

int anc(int p) { return p == par[p] ? p : par[p] = anc(par[p]); }
void join(int p, int q) { par[anc(p)] = anc(q); }

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n + m; ++i)
    par[i] = i;

  for (int i = 1; i <= n; ++i) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int x;
      scanf("%d", &x);
      join(i, x + n);
    }
  }

  int p = anc(1);
  for (int i = 1; i <= n; ++i)
    if (anc(i) != p) {
      cout << "NO\n";
      return 0;
    }
  cout << "YES\n";
}
Problem p03911 (C++)

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

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define N 1010
int f[N];
vector<int> A[N];

int fa(int x) {
  if (f[x] == x)
    return x;
  f[x] = fa(f[x]);
  return f[x];
}

int n, m;

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    f[i] = i;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int x;
      cin >> x;
      x--;
      A[x].pb(i);
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 1; j < (int)A[i].size(); j++) {
      f[fa(A[i][j - 1])] = fa(A[i][j]);
    }
  }
  int S = fa(0);
  bool F = true;
  for (int i = 0; i < n; i++)
    if (fa(i) != S)
      F = false;
  puts(F ? "YES" : "NO");
  return 0;
}

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

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define N 101000
int f[N];
vector<int> A[N];

int fa(int x) {
  if (f[x] == x)
    return x;
  f[x] = fa(f[x]);
  return f[x];
}

int n, m;

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    f[i] = i;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int x;
      cin >> x;
      x--;
      A[x].pb(i);
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 1; j < (int)A[i].size(); j++) {
      f[fa(A[i][j - 1])] = fa(A[i][j]);
    }
  }
  int S = fa(0);
  bool F = true;
  for (int i = 0; i < n; i++)
    if (fa(i) != S)
      F = false;
  puts(F ? "YES" : "NO");
  return 0;
}
Problem p03911 (C++)

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

typedef long long ll;
const int N = 100005;
const ll MOD = 1e9 + 7;

int n, m;
vector<int> adj[N];

bool vstd[N];
void dfs(int x) {
  vstd[x] = true;
  for (int y : adj[x]) {
    if (!vstd[y])
      dfs(y);
  }
}

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

  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int x;
      cin >> x;
      adj[i].push_back(x + n);
      adj[x + n].push_back(i);
    }
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    if (!vstd[i]) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES";

  return 0;
}


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

typedef long long ll;
const int N = 200005;
const ll MOD = 1e9 + 7;

int n, m;
vector<int> adj[N];

bool vstd[N];
void dfs(int x) {
  vstd[x] = true;
  for (int y : adj[x]) {
    if (!vstd[y])
      dfs(y);
  }
}

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

  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int x;
      cin >> x;
      adj[i].push_back(x + n);
      adj[x + n].push_back(i);
    }
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    if (!vstd[i]) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES";

  return 0;
}
Problem p03911 (C++)

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

const ll MAX = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;

ll N, M, ans = 0, tmp = 0;
vector<vector<ll>> G, H;
vector<ll> d;

void bfs(ll id) {
  queue<ll> Q;
  Q.push(id);
  while (!Q.empty()) {
    ll num = Q.front();
    Q.pop();
    for (int i = 0; i < G[num].size(); i++) {
      if (d[H[num][i]] == INF) {
        d[H[num][i]] = 0;
        Q.push(H[num][i]);
      }
    }
  }
}

signed main() {
  cin >> N >> M;
  G.resize(N + M);
  H.resize(N);
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    for (int j = 0; j < a; j++) {
      ll l;
      cin >> l;
      l += N - 1;
      G[i].push_back(l);
      G[l].push_back(i);
    }
  }
  d.resize(N + M, INF);
  bfs(0);
  for (int i = 0; i < N; i++) {
    ans = max(ans, d[i]);
  }
  if (ans == INF)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

const ll MAX = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;

ll N, M, ans = 0, tmp = 0;
vector<vector<ll>> G, H;
vector<ll> d;

void bfs(ll id) {
  queue<ll> Q;
  Q.push(id);
  while (!Q.empty()) {
    ll num = Q.front();
    Q.pop();
    for (int i = 0; i < G[num].size(); i++) {
      if (d[G[num][i]] == INF) {
        d[G[num][i]] = 0;
        Q.push(G[num][i]);
      }
    }
  }
}

signed main() {
  cin >> N >> M;
  G.resize(N + M);
  H.resize(N);
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    for (int j = 0; j < a; j++) {
      ll l;
      cin >> l;
      l += N - 1;
      G[i].push_back(l);
      G[l].push_back(i);
    }
  }
  d.resize(N + M, INF);
  bfs(0);
  for (int i = 0; i < N; i++) {
    ans = max(ans, d[i]);
  }
  if (ans == INF)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
  return 0;
}
Problem p03911 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int nmax = 100005;
int par[nmax];
void init(int x) {
  for (int i = 0; i < x; i++) {
    par[i] = i;
  }
}
int root(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}
void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) {
    return;
  } else {
    par[x] = y;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  init(n + m);
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int l;
      cin >> l;
      unite(i, l - 1 + n);
    }
  }
  bool f = true;
  for (int i = 0; i < n - 1; i++) {
    if (root(i) != root(i + 1)) {
      f = false;
    }
  }
  if (f) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int nmax = 100005;
int par[nmax * 2];
void init(int x) {
  for (int i = 0; i < x; i++) {
    par[i] = i;
  }
}
int root(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}
void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) {
    return;
  } else {
    par[x] = y;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  init(n + m);
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int l;
      cin >> l;
      unite(i, l - 1 + n);
    }
  }
  bool f = true;
  for (int i = 0; i < n - 1; i++) {
    if (root(i) != root(i + 1)) {
      f = false;
    }
  }
  if (f) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
Problem p03911 (C++)

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

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

#define RFOR(i, b, a) for (int(i) = (b); (i) >= (a); (i)--)
#define RREP(i, n) FOR((i), (n), 0)

#define pb push_back
#define mp make_pair

using namespace std;

const int INF = INT_MAX / 4;
const int MOD = 1000000007;
const double EPS = 1e-14;

const bool debug_flag = true;

void debug(string str) {
  if (debug_flag) {
    cout << str << endl;
  }
}

void debugs(string str) {
  if (debug_flag) {
    cout << str;
  }
}

vector<int> p;
int k;

int parent(int a) {
  if (p[a] < 0)
    return a;
  return p[a] = parent(p[a]);
}

void myunion(int a, int b) {
  a = parent(a);
  b = parent(b);
  if (a != b) {
    if (p[a] > p[b]) {
      int tmp = a;
      a = b;
      b = tmp;
    }
    p[a] += p[b];
    p[b] = a;
    k--;
  }
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.precision(10);

  int n, m;
  int a, b, l;
  cin >> n >> m;
  p.resize(m);
  vector<bool> used(m);
  k = m;
  REP(i, m) { p[i] = -1; }
  REP(i, m) { used[i] = false; }

  REP(i, n) {
    cin >> l;
    cin >> a;
    used[a - 1] = true;
    if (l != 1) {
      FOR(j, 1, l) {
        cin >> b;
        used[b - 1] = true;
        myunion(a, b);
        a = b;
      }
    }
  }

  int cnt = 0;
  REP(i, m) {
    if (!used[i]) {
      cnt++;
    }
  }
  if (k == cnt + 1) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}


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

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

#define RFOR(i, b, a) for (int(i) = (b); (i) >= (a); (i)--)
#define RREP(i, n) FOR((i), (n), 0)

#define pb push_back
#define mp make_pair

using namespace std;

const int INF = INT_MAX / 4;
const int MOD = 1000000007;
const double EPS = 1e-14;

const bool debug_flag = true;

void debug(string str) {
  if (debug_flag) {
    cout << str << endl;
  }
}

void debugs(string str) {
  if (debug_flag) {
    cout << str;
  }
}

vector<int> p;
int k;

int parent(int a) {
  if (p[a] < 0)
    return a;
  return p[a] = parent(p[a]);
}

void myunion(int a, int b) {
  a = parent(a);
  b = parent(b);
  if (a != b) {
    if (p[a] > p[b]) {
      int tmp = a;
      a = b;
      b = tmp;
    }
    p[a] += p[b];
    p[b] = a;
    k--;
  }
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.precision(10);

  int n, m;
  int a, b, l;
  cin >> n >> m;
  p.resize(m);
  vector<bool> used(m);
  k = m;
  REP(i, m) { p[i] = -1; }
  REP(i, m) { used[i] = false; }

  REP(i, n) {
    cin >> l;
    cin >> a;
    used[a - 1] = true;
    FOR(j, 1, l) {
      cin >> b;
      used[b - 1] = true;
      myunion(a - 1, b - 1);
      a = b;
    }
  }

  int cnt = 0;
  REP(i, m) {
    if (!used[i]) {
      cnt++;
    }
  }
  if (k == cnt + 1) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}
Problem p03911 (C++)

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

int main() {
  int N, M;
  cin >> N >> M;

  vector<vector<int>> edge(N + M);
  for (int i = 0; i < N; i++) {
    int K;
    cin >> K;
    for (int j = 0; j < K; j++) {
      int L;
      cin >> L;
      L--;
      edge[i].push_back(N + L);
      edge[N + L].push_back(i);
    }
  }

  vector<bool> is_visited(N, false);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int from = q.front();
    q.pop();
    if (is_visited[from]) {
      continue;
    } else {
      is_visited[from] = true;
    }

    for (int to : edge[from]) {
      if (!is_visited[to]) {
        q.push(to);
      }
    }
  }

  for (int i = 0; i < N; i++) {
    if (!is_visited[i]) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}


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

int main() {
  int N, M;
  cin >> N >> M;

  vector<vector<int>> edge(N + M);
  for (int i = 0; i < N; i++) {
    int K;
    cin >> K;
    for (int j = 0; j < K; j++) {
      int L;
      cin >> L;
      L--;
      edge[i].push_back(N + L);
      edge[N + L].push_back(i);
    }
  }

  vector<bool> is_visited(N + M, false);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int from = q.front();
    q.pop();
    if (is_visited[from]) {
      continue;
    } else {
      is_visited[from] = true;
    }

    for (int to : edge[from]) {
      if (!is_visited[to]) {
        q.push(to);
      }
    }
  }

  for (int i = 0; i < N; i++) {
    if (!is_visited[i]) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}
Problem p03911 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define pint pair<int, int>
#define pll pair<ll, ll>
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}
  int find(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = find(par[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (par[x] > par[y]) {
      par[y] += par[x];
      par[x] = y;
    } else {
      par[x] += par[y];
      par[y] = x;
    }
    return true;
  }

  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -par[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n + m);
  rep(i, n) {
    int k;
    cin >> k;
    rep(j, k) {
      int x;
      cin >> x;
      x--;
      uf.unite(x, n + i);
    }
  }
  rep(i, n) {
    if (uf.find(0) != uf.find(i)) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define pint pair<int, int>
#define pll pair<ll, ll>
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}
  int find(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = find(par[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (par[x] > par[y]) {
      par[y] += par[x];
      par[x] = y;
    } else {
      par[x] += par[y];
      par[y] = x;
    }
    return true;
  }

  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -par[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n + m);
  rep(i, n) {
    int k;
    cin >> k;
    rep(j, k) {
      int x;
      cin >> x;
      x--;
      uf.unite(n + x, i);
    }
  }
  rep(i, n) {
    if (uf.find(0) != uf.find(i)) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}