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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
int W[200010];
struct edge {
  int a, b, c;
} s[400010];
il bool operator<(const edge &a, const edge &b) { return a.c < b.c; }
int fa[200010], siz[200010];
il int hd(int x) { return fa[x] == x ? x : fa[x] = hd(fa[x]); }
il int Union(int x, int y) {
  x = hd(x), y = hd(y);
  if (x == y)
    return 0;
  if (siz[x] > siz[y])
    siz[x] += siz[y], fa[y] = x;
  else
    siz[y] += siz[x], fa[x] = y;
  return 1;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n = gi(), Q = gi(), a, b, c;
  for (int i = 1; i <= n; ++i)
    W[i] = 2e9;
  int o = 0;
  for (int i = 1; i <= Q; ++i) {
    a = gi() + 1, b = gi() + 1, c = gi();
    s[++o] = (edge){a, b, c};
    W[a] = std::min(W[a], c + 1);
    W[b] = std::min(W[b], c + 2);
  }
  for (int i = 1; i <= n; ++i)
    W[i % n + 1] = std::min(W[i % n + 1], W[i] + 2);
  for (int i = 1; i <= n; ++i)
    W[i % n + 1] = std::min(W[i % n + 1], W[i] + 2);
  for (int i = 1; i <= n; ++i)
    s[++o] = (edge){i, i % n + 1, W[i]};
  std::sort(s + 1, s + o + 1);
  for (int i = 1; i <= n; ++i)
    fa[i] = i, siz[i] = 1;
  ll ans = 0;
  for (int i = 1; i <= o; ++i)
    ans += s[i].c * Union(s[i].a, s[i].b);
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
int W[200010];
struct edge {
  int a, b, c;
} s[400010];
il bool operator<(const edge &a, const edge &b) { return a.c < b.c; }
int fa[200010], siz[200010];
il int hd(int x) { return fa[x] == x ? x : fa[x] = hd(fa[x]); }
il int Union(int x, int y) {
  x = hd(x), y = hd(y);
  if (x == y)
    return 0;
  if (siz[x] > siz[y])
    siz[x] += siz[y], fa[y] = x;
  else
    siz[y] += siz[x], fa[x] = y;
  return 1;
}
int main() {
  int n = gi(), Q = gi(), a, b, c;
  for (int i = 1; i <= n; ++i)
    W[i] = 2e9;
  int o = 0;
  for (int i = 1; i <= Q; ++i) {
    a = gi() + 1, b = gi() + 1, c = gi();
    s[++o] = (edge){a, b, c};
    W[a] = std::min(W[a], c + 1);
    W[b] = std::min(W[b], c + 2);
  }
  for (int i = 1; i <= n; ++i)
    W[i % n + 1] = std::min(W[i % n + 1], W[i] + 2);
  for (int i = 1; i <= n; ++i)
    W[i % n + 1] = std::min(W[i % n + 1], W[i] + 2);
  for (int i = 1; i <= n; ++i)
    s[++o] = (edge){i, i % n + 1, W[i]};
  std::sort(s + 1, s + o + 1);
  for (int i = 1; i <= n; ++i)
    fa[i] = i, siz[i] = 1;
  ll ans = 0;
  for (int i = 1; i <= o; ++i)
    ans += s[i].c * Union(s[i].a, s[i].b);
  printf("%lld\n", ans);
  return 0;
}
Problem p03919 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int A, B;
  cin >> A >> B;
  string str;
  int cou = 0;
  for (int i = 0; i < A * B; i++) {
    cin >> str;
    cou++;
    if (str == "snuke") {
      break;
    }
  }
  int X = cou % A;
  int Y = (cou - X) / A;
  if (X = 0) {
    X = A;
  }
  string alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  char W = alp.at(X - 1);
  cout << W << Y << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int A, B;
  cin >> A >> B;
  string str;
  int cou = 0;
  for (int i = 0; i < A * B; i++) {
    cin >> str;
    cou++;
    if (str == "snuke") {
      break;
    }
  }
  int X = cou % B;
  int Y = (cou - X) / B + 1;
  if (X == 0) {
    X = B;
    Y--;
  }
  string alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  char W = alp.at(X - 1);
  cout << W << Y << endl;
}
Problem p03919 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int m, n;
  cin >> m >> n;

  vector<vector<string>> word(m, vector<string>(n));
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> word[i][j];
    }
  }

  int a;
  char b;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (word[i][j] == "snuke") {
        a = i + 1;
        b = char(j + 'A');
      }
    }
  }

  cout << b << a << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int m, n;
  cin >> m >> n;

  vector<vector<string>> word(m, vector<string>(n));
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> word[i][j];
    }
  }

  int a;
  char b;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (word[i][j] == "snuke") {
        a = i + 1;
        b = char(j + 'A');
      }
    }
  }

  cout << b << a << endl;

  return 0;
}
Problem p03919 (C++)

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

int main() {
  int a = 0, b = 0;
  int w, h;
  cin >> w >> h;
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      string x;
      cin >> x;
      if (x == "snuke") {
        a = i;
        b = j;
      }
    }
  }
  vector<char> line;
  line = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
          'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  cout << line.at(a - 1) << b << endl;
}


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

int main() {
  int a = 0, b = 0;
  int w, h;
  cin >> w >> h;
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      string x;
      cin >> x;
      if (x == "snuke") {
        a = i;
        b = j;
      }
    }
  }
  vector<char> line;
  line = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
          'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  cout << line.at(b) << a + 1 << endl;
}
Problem p03919 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int H, W;
  map<int, char> mp;
  mp[1] = 'A';
  mp[2] = 'B';
  mp[3] = 'C';
  mp[4] = 'D';
  mp[5] = 'E';
  mp[6] = 'F';
  mp[7] = 'G';
  mp[8] = 'H';
  mp[9] = 'I';
  mp[10] = 'J';
  mp[11] = 'K';
  mp[12] = 'L';
  mp[13] = 'M';
  mp[14] = 'N';
  mp[15] = 'O';
  mp[16] = 'P';
  mp[17] = 'Q';
  mp[18] = 'R';
  mp[19] = 'S';
  mp[20] = 'T';
  mp[21] = 'U';
  mp[22] = 'V';
  mp[23] = 'W';
  mp[24] = 'X';
  mp[25] = 'Y';
  mp[26] = 'Z';
  vector<vector<string>> data(26, vector<string>(26));
  cin >> H >> W;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      cin >> data[i][j];
    }
  }

  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (data[i][j] == "snuke") {
        cout << mp[j];
        cout << i << endl;
      }
    }
  }
  return 0;
}


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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int H, W;
  map<int, char> mp;
  mp[1] = 'A';
  mp[2] = 'B';
  mp[3] = 'C';
  mp[4] = 'D';
  mp[5] = 'E';
  mp[6] = 'F';
  mp[7] = 'G';
  mp[8] = 'H';
  mp[9] = 'I';
  mp[10] = 'J';
  mp[11] = 'K';
  mp[12] = 'L';
  mp[13] = 'M';
  mp[14] = 'N';
  mp[15] = 'O';
  mp[16] = 'P';
  mp[17] = 'Q';
  mp[18] = 'R';
  mp[19] = 'S';
  mp[20] = 'T';
  mp[21] = 'U';
  mp[22] = 'V';
  mp[23] = 'W';
  mp[24] = 'X';
  mp[25] = 'Y';
  mp[26] = 'Z';
  vector<vector<string>> data(27, vector<string>(27));
  cin >> H >> W;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      cin >> data[i][j];
    }
  }

  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (data[i][j] == "snuke") {
        cout << mp[j];
        cout << i << endl;
      }
    }
  }
  return 0;
}
Problem p03920 (C++)

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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
const ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
ll n, cnt, a, b, c, d, tmp, m, h, w, x, y, sum, k, q;
ll rui[ohara];
V ans;

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

  cin >> n;
  rui[1] = 1;
  rrep(i, 2, n + 1) { rui[i] += rui[i - 1] + i; }
  ll now = n;
  rrep(i, 1, n + 1) {
    tmp = n - i;
    if (rui[i - 1] >= tmp) {
      ans.p_b(i);
      tmp = i;
      now -= i;
      break;
    }
  }
  rrrep(i, tmp - 1, 1) {
    if (now == 0)
      break;
    if (now >= i) {
      now -= i;
      ans.p_b(i);
    }
  }
  rep(i, Size(ans)) Cout(ans[i]);
  return 0;
}

Fixed submission:
#pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
const ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
ll n, cnt, a, b, c, d, tmp, m, h, w, x, y, sum, k, q;
ll rui[10000000 + 10];
V ans;

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

  cin >> n;
  rui[1] = 1;
  rrep(i, 2, n + 1) { rui[i] += rui[i - 1] + i; }
  ll now = n;
  rrep(i, 1, n + 1) {
    tmp = n - i;
    if (rui[i - 1] >= tmp) {
      ans.p_b(i);
      tmp = i;
      now -= i;
      break;
    }
  }
  rrrep(i, tmp - 1, 1) {
    if (now == 0)
      break;
    if (now >= i) {
      now -= i;
      ans.p_b(i);
    }
  }
  rep(i, Size(ans)) Cout(ans[i]);
  return 0;
}
Problem p03920 (C++)

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

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

  bool f[101010] = {false};

  int sum = 0;
  for (int i = 1; sum < n; ++i) {
    sum += i;
    f[i] = true;
  }

  int d = sum - n;
  f[d] = false;

  for (int i = 0; i <= n; ++i) {
    if (f[i])
      cout << i << endl;
  }
}


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

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

  bool f[101010] = {false};

  int sum = 0;
  for (int i = 1; sum < n; ++i) {
    sum += i;
    f[i] = true;
  }

  int d = sum - n;
  f[d] = false;

  for (int i = 0; i < 101010; ++i) {
    if (f[i])
      cout << i << endl;
  }
}
Problem p03920 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define N 100005
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define pb push_back
#define mp make_pair
#define LL long long
#define fi first
#define se second

set<int> f;
void fun(int n) {
  int i, s, m = sqrt(2 * n);
  while (m * (m + 1) < n * 2)
    ++m;
  while (m * (m + 1) > 2 * n)
    --m;
  ++m;
  s = (m + 1) * m / 2;
  s -= n;
  for (i = 1; i <= m; ++i) {
    if (i == s)
      continue;
    cout << i << ' ';
  }
}
int main() {
  int n;
  while (scanf("%d", &n))
    fun(n);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define N 100005
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define pb push_back
#define mp make_pair
#define LL long long
#define fi first
#define se second

set<int> f;
void fun(int n) {
  int i, s, m = sqrt(2 * n);
  while (m * (m + 1) < n * 2)
    ++m;
  while (m * (m + 1) > 2 * n)
    --m;
  ++m;
  s = (m + 1) * m / 2;
  s -= n;
  for (i = 1; i <= m; ++i) {
    if (i == s)
      continue;
    cout << i << ' ';
  }
}
int main() {
  int n;
  scanf("%d", &n);
  fun(n);
  return 0;
}
Problem p03920 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>

#define For(i, x, y) for (int i = x; i < y; i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

#define dprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;

int IN() {
  int c, f, x;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0');
  while (isdigit(c = getchar()))
    x = (x << 1) + (x << 3) + c - '0';
  return !f ? x : -x;
}

typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> Vi;

const int N = 1e7 + 19;

int A[N], n;

int main() {
  n = IN();
  for (int i = 1, s = 0; !A[n]; i++) {
    for (int j = s + 1; j <= s + i; j++)
      A[j] = i;
    s += i;
  }
  while (n) {
    printf("%d\n", A[n]);
    n -= A[n];
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>

#define For(i, x, y) for (int i = x; i < y; i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

#define dprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;

int IN() {
  int c, f, x;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0');
  while (isdigit(c = getchar()))
    x = (x << 1) + (x << 3) + c - '0';
  return !f ? x : -x;
}

typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> Vi;

const int N = 1e7 + 19;

int A[N], n;

int main() {
  n = IN();
  for (int i = 1, s = 0; !A[n]; i++) {
    for (int j = s + 1; j <= s + i && j <= n; j++)
      A[j] = i;
    s += i;
  }
  while (n) {
    printf("%d\n", A[n]);
    n -= A[n];
  }
}
Problem p03921 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>
#define pb push_back
#define mk make_pair
using namespace std;
const int INF = 1e9 + 1000;

typedef long long ll;

#define MAX_V 110005

struct edge {
  int to;
  int cost;
};

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int m;
  int p;
  cin >> V >> m;

  REP(i, V) {
    int k;
    cin >> k;
    REP(j, k) {
      int f, t, c;
      cin >> t;
      t--;
      t += V;
      f = i;
      c = 1;
      edge e = {t, c};
      G[f].pb(e);
      e.to = f;
      G[t].pb(e);
    }
  }
  V += m;
  dijkstra(0);
  bool f = true;
  REP(i, V - m) {
    if (d[i] == INF)
      f = false;
  }
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>
#define pb push_back
#define mk make_pair
using namespace std;
const int INF = 1e9 + 1000;

typedef long long ll;

#define MAX_V 210005

struct edge {
  int to;
  int cost;
};

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int m;
  int p;
  cin >> V >> m;

  REP(i, V) {
    int k;
    cin >> k;
    REP(j, k) {
      int f, t, c;
      cin >> t;
      t--;
      t += V;
      f = i;
      c = 1;
      edge e = {t, c};
      G[f].pb(e);
      e.to = f;
      G[t].pb(e);
    }
  }
  V += m;
  dijkstra(0);
  bool f = true;
  REP(i, V - m) {
    if (d[i] == INF)
      f = false;
  }
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p03921 (C++)

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

int P[100000];
bool has_talker[100000] = {false};

int root(int a) {
  if (P[a] == a) {
    return a;
  }
  return P[a] = root(P[a]);
}

void unite(int a, int b) { P[root(a)] = root(b); }

int main() {
  int N, M;
  int K, L[100000];

  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    P[i] = i;
  }

  for (int i = 0; i < N; i++) {
    cin >> K;
    for (int j = 0; j < K; j++) {
      cin >> L[j];
      L[j]--;
      has_talker[L[j]] = true;
    }
    for (int j = 0; j < K; j++) {
      for (int k = 1; k < K; k++) {
        unite(L[j], L[k]);
      }
    }
  }

  for (int i = 0; i < M; i++) {
    root(i);
  }

  int a = -1;
  for (int i = 0; i < M; i++) {
    // cout << has_talker[i] << ", " << root(i) << endl;
    if (a == -1 && has_talker[i] == true) {
      a = root(i);
      continue;
    }
    if (has_talker[i] && root(i) != a) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}

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

int P[100000];
bool has_talker[100000] = {false};

int root(int a) {
  if (P[a] == a) {
    return a;
  }
  return P[a] = root(P[a]);
}

void unite(int a, int b) { P[root(a)] = root(b); }

int main() {
  int N, M;
  int K, L[100000];

  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    P[i] = i;
  }

  for (int i = 0; i < N; i++) {
    cin >> K;
    for (int j = 0; j < K; j++) {
      cin >> L[j];
      L[j]--;
      has_talker[L[j]] = true;
    }
    for (int j = 1; j < K; j++) {
      unite(L[0], L[j]);
    }
  }

  for (int i = 0; i < M; i++) {
    root(i);
  }

  int a = -1;
  for (int i = 0; i < M; i++) {
    // cout << has_talker[i] << ", " << root(i) << endl;
    if (a == -1 && has_talker[i] == true) {
      a = root(i);
      continue;
    }
    if (has_talker[i] && root(i) != a) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}
Problem p03921 (C++)

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

bool used[100000], lused[100000];
vector<int> ls[100000], lang[100000];

void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < ls[v].size(); i++) {
    int l = ls[v][i];
    if (!lused[l]) {
      for (int j = 0; j < lang[l].size(); j++) {
        int to = lang[l][j];
        if (!used[to])
          dfs(to);
      }
    }
  }
}

int main() {
  int n, m;
  cin >> 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--;
      ls[i].push_back(l);
      lang[l].push_back(i);
    }
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}

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

bool used[100000], lused[100000];
vector<int> ls[100000], lang[100000];

void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < ls[v].size(); i++) {
    int l = ls[v][i];
    if (!lused[l]) {
      lused[l] = true;
      for (int j = 0; j < lang[l].size(); j++) {
        int to = lang[l][j];
        if (!used[to])
          dfs(to);
      }
    }
  }
}

int main() {
  int n, m;
  cin >> 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--;
      ls[i].push_back(l);
      lang[l].push_back(i);
    }
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
  return 0;
}
Problem p03921 (C++)

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

using namespace std;

void dfs(const vector<vector<int>> &adj_lists, int cur, vector<int> &us) {
  us[cur] = 1;
  for (int nxt : adj_lists[cur]) {
    if (!us[nxt]) {
      dfs(adj_lists, nxt, us);
    }
  }
}

int main(int argc, char *argv[]) {
  // read inputs
  int N, M;
  vector<int> ks;
  vector<vector<int>> lss;
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    int k, l;
    scanf("%d", &k);
    ks.push_back(k);
    lss.push_back(vector<int>());
    for (int j = 0; j < k; j++) {
      scanf("%d", &l);
      l--; // NOTE : modified input
      lss[i].push_back(l);
    }
  }

  // create graph
  vector<vector<int>> adj_lists(M, vector<int>());
  for (int i = 0; i < N; i++) {
    const int l_0 = lss[i][0];
    for (int j = 1, j_end = lss[i].size(); j < j_end; j++) {
      const int l_j = lss[i][j];
      adj_lists[l_0].push_back(l_j);
      adj_lists[l_j].push_back(l_0);
    }
  }

  // solve
  int possible = 1;
  vector<int> us(N, 0);
  dfs(adj_lists, lss[0][0], us);
  for (vector<int> &ls : lss) {
    if (!us[ls[0]]) {
      possible = 0;
      break;
    }
  }
  if (possible) {
    printf("YES\n");
  } else {
    printf("NO\n");
  }

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

void dfs(const vector<vector<int>> &adj_lists, int cur, vector<int> &us) {
  us[cur] = 1;
  for (int nxt : adj_lists[cur]) {
    if (!us[nxt]) {
      dfs(adj_lists, nxt, us);
    }
  }
}

int main(int argc, char *argv[]) {
  // read inputs
  int N, M;
  vector<int> ks;
  vector<vector<int>> lss;
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    int k, l;
    scanf("%d", &k);
    ks.push_back(k);
    lss.push_back(vector<int>());
    for (int j = 0; j < k; j++) {
      scanf("%d", &l);
      l--; // NOTE : modified input
      lss[i].push_back(l);
    }
  }

  // create graph
  vector<vector<int>> adj_lists(M, vector<int>());
  for (int i = 0; i < N; i++) {
    const int l_0 = lss[i][0];
    for (int j = 1, j_end = lss[i].size(); j < j_end; j++) {
      const int l_j = lss[i][j];
      adj_lists[l_0].push_back(l_j);
      adj_lists[l_j].push_back(l_0);
    }
  }

  // solve
  int possible = 1;
  vector<int> us(M, 0);
  dfs(adj_lists, lss[0][0], us);
  for (vector<int> &ls : lss) {
    if (!us[ls[0]]) {
      possible = 0;
      break;
    }
  }
  if (possible) {
    printf("YES\n");
  } else {
    printf("NO\n");
  }

  return 0;
}
Problem p03921 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
struct UF {
  int par[100000];
  int rank[100000];
  int g[100000];

  void init(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
      g[i] = 1;
    }
  }

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

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x != y) {
      if (rank[x] < rank[y]) {
        par[x] = y;
        g[y] += g[x];
        g[x] = 0;
      } else {
        par[y] = x;
        if (rank[x] == rank[y])
          rank[x]++;
        g[x] += g[y];
        g[y] = 0;
      }
    }
  }

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

int main(void) {
  int n, m;
  UF u;
  cin >> n >> m;
  u.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;
      u.unite(i, n + l - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (u.find(i) != u.find(0)) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
struct UF {
  int par[200000];
  int rank[200000];
  int g[200000];

  void init(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
      g[i] = 1;
    }
  }

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

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x != y) {
      if (rank[x] < rank[y]) {
        par[x] = y;
        g[y] += g[x];
        g[x] = 0;
      } else {
        par[y] = x;
        if (rank[x] == rank[y])
          rank[x]++;
        g[x] += g[y];
        g[y] = 0;
      }
    }
  }

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

int main(void) {
  int n, m;
  UF u;
  cin >> n >> m;
  u.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;
      u.unite(i, n + l - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (u.find(i) != u.find(0)) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}
Problem p03921 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;

int n, m;
vector<vector<int>> lang;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  cin >> n >> m;
  lang.resize(m + 5);
  UnionFind unio(n);
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    for (int j = 0; j < p; j++) {
      int mm;
      cin >> mm;
      for (int k : lang[mm]) {
        unio.unionSet(k, i);
      }
      lang[mm].push_back(i);
    }
  }
  if (unio.size(0) != n) {
    cout << "NO" << endl;
  } else
    cout << "YES" << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;

int n, m;
vector<vector<int>> lang;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  cin >> n >> m;
  lang.resize(m + 5);
  UnionFind unio(n);
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    for (int j = 0; j < p; j++) {
      int mm;
      cin >> mm;
      if (lang[mm].size())
        unio.unionSet(lang[mm][0], i);
      lang[mm].push_back(i);
    }
  }
  if (unio.size(0) != n) {
    cout << "NO" << endl;
  } else
    cout << "YES" << endl;
  return 0;
}
Problem p03921 (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 all(x) x.begin(), x.end()
#define dbg(x) cout << #x << ":" << x << endl
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

vector<int> g[222222];
int used[222222];

void dfs(int n = 0) {
  if (used[n])
    return;
  used[n] = 1;
  for (int i = 0; i < g[i].size(); i++)
    dfs(g[n][i]);
}

int main() {
  int n, m;
  cin >> 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--;
      g[n + l].pb(i);
      g[i].pb(n + l);
    }
  }

  dfs();

  bool ans = true;
  for (int i = 0; i < n; i++) {
    if (used[i])
      continue;
    ans = false;
  }

  cout << (ans ? "YES" : "NO") << endl;

  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 all(x) x.begin(), x.end()
#define dbg(x) cout << #x << ":" << x << endl
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

vector<int> g[222222];
int used[222222];

void dfs(int n = 0) {
  if (used[n])
    return;
  used[n] = 1;
  for (int i = 0; i < g[n].size(); i++)
    dfs(g[n][i]);
}

int main() {
  int n, m;
  cin >> 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--;
      g[n + l].pb(i);
      g[i].pb(n + l);
    }
  }

  dfs();

  bool ans = true;
  for (int i = 0; i < n; i++) {
    if (used[i])
      continue;
    ans = false;
  }

  cout << (ans ? "YES" : "NO") << endl;

  return 0;
}
Problem p03921 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int N, M, par[200500];

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

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N + M; i++)
    par[i] = i;
  for (int i = 0; i < N; i++) {
    int x;
    scanf("%d", &x);
    while (x--) {
      int y;
      scanf("%d", &y);
      y = y - 1 + N;
      par[find(i)] = find(y);
    }
  }

  for (int i = 0; i < N; i++)
    if (find(i) != find(0)) {
      puts("NO");
      return 0;
    }
  puts("YES");
  return 0;
}

Fixed submission:
#include <stdio.h>

int N, M, par[200500];

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

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N + M; i++)
    par[i] = i;
  for (int i = 0; i < N; i++) {
    int x;
    scanf("%d", &x);
    while (x--) {
      int y;
      scanf("%d", &y);
      y = y - 1 + N;
      par[find(i)] = find(y);
    }
  }

  for (int i = 0; i < N; i++)
    if (find(i) != find(0)) {
      puts("NO");
      return 0;
    }
  puts("YES");
  return 0;
}
Problem p03921 (C++)

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

typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)

int n, m;

//
// Union-Find
//
const int MAX_N = 100010;
int par[MAX_N], rnk[MAX_N];

// n要素で初期化
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rnk[i] = 0;
  }
}

// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// x と y の属する集合を併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (rnk[x] < rnk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rnk[x] == rnk[y])
      rnk[x]++;
  }
}

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

int main() {
  scanf("%d%d", &n, &m);
  init(n + m);

  rep(i, n) {
    int k;
    scanf("%d", &k);
    vector<int> lan;
    rep(j, k) {
      int l;
      scanf("%d", &l);
      --l;
      unite(i, n + l);
    }
  }

  rep(i, n) {
    if (!same(0, i)) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;

  return 0;
}

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

typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)

int n, m;

//
// Union-Find
//
const int MAX_N = 200010;
int par[MAX_N], rnk[MAX_N];

// n要素で初期化
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rnk[i] = 0;
  }
}

// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// x と y の属する集合を併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (rnk[x] < rnk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rnk[x] == rnk[y])
      rnk[x]++;
  }
}

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

int main() {
  scanf("%d%d", &n, &m);
  init(n + m);

  rep(i, n) {
    int k;
    scanf("%d", &k);
    vector<int> lan;
    rep(j, k) {
      int l;
      scanf("%d", &l);
      --l;
      unite(i, n + l);
    }
  }

  rep(i, n) {
    if (!same(0, i)) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;

  return 0;
}
Problem p03921 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
using namespace std;

int n, m, o, x, y, p[100100], r[100100], s[100100];
vector<int> v[100100];
vector<int> graph[100100];
bool vis[100100];

void dfs(int u) {
  if (vis[u])
    return;
  vis[u] = 1;
  for (int i = 0; i < graph[u].size(); i++) {
    int v = graph[u][i];
    dfs(v);
  }
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    p[i] = i;
    r[i] = 1;
    s[i] = 1;
    scanf("%d", &o);
    for (int j = 0; j < o; j++) {
      scanf("%d", &x);
      v[x].pb(i);
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      for (int k = j + 1; k < v[i].size(); k++) {
        x = v[i][j];
        y = v[i][k];
        graph[x].pb(y);
        graph[y].pb(x);
      }
    }
  }
  dfs(1);
  bool check = true;
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      check = false;
  }
  if (check)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
using namespace std;

int n, m, o, x, y, p[100100], r[100100], s[100100];
vector<int> v[100100];
vector<int> graph[100100];
bool vis[100100];

void dfs(int u) {
  if (vis[u])
    return;
  vis[u] = 1;
  for (int i = 0; i < graph[u].size(); i++) {
    int v = graph[u][i];
    dfs(v);
  }
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    p[i] = i;
    r[i] = 1;
    s[i] = 1;
    scanf("%d", &o);
    for (int j = 0; j < o; j++) {
      scanf("%d", &x);
      v[x].pb(i);
    }
  }
  for (int i = 1; i <= m; i++) {
    if (v[i].size() > 1)
      x = v[i][0];
    for (int j = 1; j < v[i].size(); j++) {
      y = v[i][j];
      graph[x].pb(y);
      graph[y].pb(x);
    }
  }
  dfs(1);
  bool check = true;
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      check = false;
  }
  if (check)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
Problem p03921 (C++)

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

struct UnionFind {
  int n;
  vector<int> r, p;
  UnionFind() {}
  UnionFind(int sz) : n(sz), r(sz, 1), p(sz, 0) { iota(p.begin(), p.end(), 0); }
  int find(int x) { return (x == p[x] ? x : p[x] = find(p[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 (x == y)
      return;
    if (r[x] < r[y])
      swap(x, y);
    r[x] += r[y];
    p[y] = x;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  std::vector<int> K(N);
  std::vector<int> L[N];
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    for (int j = 0; j < K[i]; j++) {
      int afj;
      cin >> afj;
      L[i].push_back(afj);
    }
  }
  UnionFind uf(N + 1);
  for (int i = 0; i < N; i++) {
    for (int j = 1; j < K[i]; j++) {
      uf.unite(L[i][0], L[i][j]);
    }
  }
  int out = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K[i]; j++) {
      if (!uf.same(L[0][0], L[i][j]))
        out++;
    }
  }
  if (out)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
}


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

struct UnionFind {
  int n;
  vector<int> r, p;
  UnionFind() {}
  UnionFind(int sz) : n(sz), r(sz, 1), p(sz, 0) { iota(p.begin(), p.end(), 0); }
  int find(int x) { return (x == p[x] ? x : p[x] = find(p[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 (x == y)
      return;
    if (r[x] < r[y])
      swap(x, y);
    r[x] += r[y];
    p[y] = x;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  std::vector<int> K(N);
  std::vector<int> L[N];
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    for (int j = 0; j < K[i]; j++) {
      int afj;
      cin >> afj;
      L[i].push_back(afj);
    }
  }
  UnionFind uf(M + 1);
  for (int i = 0; i < N; i++) {
    for (int j = 1; j < K[i]; j++) {
      uf.unite(L[i][0], L[i][j]);
    }
  }
  int out = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K[i]; j++) {
      if (!uf.same(L[0][0], L[i][j]))
        out++;
    }
  }
  if (out)
    cout << "NO" << endl;
  else
    cout << "YES" << endl;
}
Problem p03921 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define dump(x) cout << (x) << '\n';
typedef int64_t Int;
Int mod = 1e9 + 7;
Int INF = 1e9 + 18;

vector<Int> G[200005];
bool used[100005];

void dfs(Int x) {
  if (used[x])
    return;
  used[x] = true;
  for (Int i = 0; i < (Int)G[x].size(); i++) {
    Int to = G[x][i];
    if (used[to])
      continue;
    dfs(to);
  }
}

int main() {
  ios::sync_with_stdio(false);
  Int n, m;
  cin >> 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--;
      G[i].emplace_back(n + l);
      G[n + l].emplace_back(i);
    }
  }
  dfs(0);
  bool res = true;
  for (Int i = 0; i < n; i++) {
    if (!used[i])
      res = false;
  }
  dump(res ? "YES" : "NO");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define dump(x) cout << (x) << '\n';
typedef int64_t Int;
Int mod = 1e9 + 7;
Int INF = 1e9 + 18;

vector<Int> G[200005];
bool used[200005];

void dfs(Int x) {
  if (used[x])
    return;
  used[x] = true;
  for (Int i = 0; i < (Int)G[x].size(); i++) {
    Int to = G[x][i];
    if (used[to])
      continue;
    dfs(to);
  }
}

int main() {
  ios::sync_with_stdio(false);
  Int n, m;
  cin >> 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--;
      G[i].emplace_back(n + l);
      G[n + l].emplace_back(i);
    }
  }
  dfs(0);
  bool res = true;
  for (Int i = 0; i < n; i++) {
    if (!used[i])
      res = false;
  }
  dump(res ? "YES" : "NO");
  return 0;
}
Problem p03921 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, s, e) for (int i = (s); i < (e); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define pb(a) push_back(a)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;

struct UnionFind {
  vector<int> data;
  UnionFind(int sz) { data.assign(sz, -1); }
  bool unite(int x, int y) {
    x = root(x), y = root(y);
    if (x == y)
      return false;
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }
  int root(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = root(data[k]));
  }
  int size(int k) { return -data[root(k)]; }
};

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> l(n);
  vector<bool> spoken(m, false);
  REP(i, n) {
    int k;
    cin >> k;
    REP(j, k) {
      int x;
      cin >> x;
      l[i].pb(--x);
      spoken[i] = true;
    }
  }

  UnionFind UF(m);
  REP(i, n) { REP(j, l[i].size()) UF.unite(l[i][0], l[i][j]); }

  bool ans = true;
  REP(i, m) {
    if (spoken[i] && UF.root(i) != UF.root(l[0][0]))
      ans = false;
  }
  cout << (ans ? "YES" : "NO") << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, s, e) for (int i = (s); i < (e); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define pb(a) push_back(a)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;

struct UnionFind {
  vector<int> data;
  UnionFind(int sz) { data.assign(sz, -1); }
  bool unite(int x, int y) {
    x = root(x), y = root(y);
    if (x == y)
      return false;
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }
  int root(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = root(data[k]));
  }
  int size(int k) { return -data[root(k)]; }
};

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> l(n);
  vector<bool> spoken(m, false);
  REP(i, n) {
    int k;
    cin >> k;
    REP(j, k) {
      int x;
      cin >> x;
      l[i].pb(--x);
      spoken[x] = true;
    }
  }

  UnionFind UF(m);
  REP(i, n) { REP(j, l[i].size()) UF.unite(l[i][0], l[i][j]); }

  bool ans = true;
  REP(i, m) {
    if (spoken[i] && UF.root(i) != UF.root(l[0][0]))
      ans = false;
  }
  cout << (ans ? "YES" : "NO") << endl;
}
Problem p03921 (C++)

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

using namespace std;

static const int INF = 1e9 + 7;

struct Union_Find {
  int par[100001], root[100001];

  Union_Find() {
    for (int i = 0; i < 100001; i++) {
      par[i] = 0;
      root[i] = i;
    }
  }
  int find(int x) {
    if (root[x] == x)
      return x;
    return root[x] = find(root[x]);
  }

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

  void unite(int x, int y) {
    if (same(x, y))
      return;
    x = find(x);
    y = find(y);
    if (par[x] < par[y]) {
      root[y] = root[x];
    } else {
      root[x] = root[y];
      if (par[x] == par[y]) {
        par[x]++;
      }
    }
  }
};

void hawawa() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> l(n);
  Union_Find uf;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    int lang1;
    cin >> lang1;
    l[i].push_back(lang1);
    for (int j = 1; j < num; j++) {
      int lang2;
      cin >> lang2;
      l[i].push_back(lang2);
      uf.unite(lang1, lang2);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (!uf.same(l[i][0], l[j][0])) {
        cout << "NO\n";
        return;
      }
    }
  }
  cout << "YES\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  hawawa();
}

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

using namespace std;

static const int INF = 1e9 + 7;

struct Union_Find {
  int par[100001], root[100001];

  Union_Find() {
    for (int i = 0; i < 100001; i++) {
      par[i] = 0;
      root[i] = i;
    }
  }
  int find(int x) {
    if (root[x] == x)
      return x;
    return root[x] = find(root[x]);
  }

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

  void unite(int x, int y) {
    if (same(x, y))
      return;
    x = find(x);
    y = find(y);
    if (par[x] < par[y]) {
      root[y] = root[x];
    } else {
      root[x] = root[y];
      if (par[x] == par[y]) {
        par[x]++;
      }
    }
  }
};

void hawawa() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> l(n);
  Union_Find uf;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    int lang1;
    cin >> lang1;
    l[i].push_back(lang1);
    for (int j = 1; j < num; j++) {
      int lang2;
      cin >> lang2;
      l[i].push_back(lang2);
      uf.unite(lang1, lang2);
    }
  }
  for (int i = 1; i < n; i++) {
    if (!uf.same(l[0][0], l[i][0])) {
      cout << "NO\n";
      return;
    }
  }
  cout << "YES\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  hawawa();
}
Problem p03921 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

ll n, m;
vector<vector<ll>> g;
bool used[100005];

void dfs(ll p) {
  for (auto x : g[p]) {
    if (!used[x]) {
      used[x] = true;
      dfs(x);
    }
  }
}

int main() {
  cin >> n >> m;
  g.resize(n + m);
  REP(i, n) {
    ll k;
    cin >> k;
    REP(j, k) {
      ll l;
      cin >> l;
      l--;
      g[i].push_back(n + l);
      g[n + l].push_back(i);
    }
  }

  dfs(0);

  REP(i, n) {
    if (!used[i]) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (ll i = 0; i < n; i++)

ll n, m;
vector<vector<ll>> g;
bool used[200005];

void dfs(ll p) {
  for (auto x : g[p]) {
    if (!used[x]) {
      used[x] = true;
      dfs(x);
    }
  }
}

int main() {
  cin >> n >> m;
  g.resize(n + m);
  REP(i, n) {
    ll k;
    cin >> k;
    REP(j, k) {
      ll l;
      cin >> l;
      l--;
      g[i].push_back(n + l);
      g[n + l].push_back(i);
    }
  }

  dfs(0);

  REP(i, n) {
    if (!used[i]) {
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
}