alexjercan/bugnet huggingface dataset:
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()

using ll = long long;
using ld = long double;
using namespace std;

const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ll N = 2e3 + 1;

int s, fact[N];

ll fastpow(ll a, ll n, ll m) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = (res * a) % m;
    n >>= 1;
    a = (a * a) % m;
  }
  return res;
}
ll add(ll a, ll b, ll m) { return (a % m + b % m) % m; }
ll sub(ll a, ll b, ll m) { return ((a - b) % m + m) % m; }
ll mul(ll a, ll b, ll m) { return (a % m * b % m) % m; }
ll div(ll a, ll b, ll m) { return mul(a, fastpow(b, m - 2, m), m); }

int ncr(int n, int r) {
  int x = mul(fact[r], fact[n - r], mod);
  return div(fact[n], x, mod);
}

void code() {
  cin >> s;
  ll ans = 0;
  fact[0] = fact[1] = 1;
  for (int i = 2; i < N; i++) {
    fact[i] = mul(fact[i - 1], i, mod);
  }

  for (int i = 1; i <= s / 3; i++) {
    int tmp = s - 3 * i;
    int a = ncr(tmp + i - 1, i - 1);
    ans = add(ans, a, mod);
  }

  cout << ans << "\\n";
}

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

  int t = 1;
  // cin>>t;

  while (t-- > 0) {
    code();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()

using ll = long long;
using ld = long double;
using namespace std;

const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ll N = 2e3 + 5;

int s, fact[N];

ll fastpow(ll a, ll n, ll m) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = (res * a) % m;
    n >>= 1;
    a = (a * a) % m;
  }
  return res;
}
ll add(ll a, ll b, ll m) { return (a % m + b % m) % m; }
ll sub(ll a, ll b, ll m) { return ((a - b) % m + m) % m; }
ll mul(ll a, ll b, ll m) { return (a % m * b % m) % m; }
ll div(ll a, ll b, ll m) { return mul(a, fastpow(b, m - 2, m), m); }

int ncr(int n, int r) {
  int x = mul(fact[r], fact[n - r], mod);
  return div(fact[n], x, mod);
}

void code() {
  cin >> s;
  ll ans = 0;
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= N; i++) {
    fact[i] = mul(fact[i - 1], i, mod);
  }

  for (int i = 1; i <= s / 3; i++) {
    int tmp = s - 3 * i;
    int a = ncr(tmp + i - 1, i - 1);
    ans = add(ans, a, mod);
  }

  cout << ans << "\\n";
}

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

  int t = 1;
  // cin>>t;

  while (t-- > 0) {
    code();
  }
}
Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

long long mod = 1000000007;

int dp[2005];

int main() {
  int n;
  cin >> n;
  dp[0] = 1;
  for (int i = 3; i <= n; i++) {
    for (int j = 3; j <= i; j++) {
      dp[i] += dp[i - j];
      dp[i] %= mod;
    }
  }
  cout << dp[n] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

long long mod = 1000000007;

int dp[2005][2005];
int coinit(int n) {
  if (n == 0) {
    return 1;
  }
  if (n < 3) {
    return 0;
  }
  int answer = 0;
  for (int i = 3; i <= n; i++) {
    if (dp[n][i] == -1) {
      dp[n][i] = coinit(n - i);
      dp[n][i] %= mod;
      answer += dp[n][i];
    } else {
      answer += dp[n][i];
    }
    answer %= mod;
  }

  return answer;
}
int main() {
  int n;
  cin >> n;
  memset(dp, -1, sizeof dp);
  cout << coinit(n);
  return 0;
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

typedef long long ll;

const ll mod = 1e9 + 7;
ll dp[2005];

int main() {
  ll n;
  cin >> n;
  dp[0] = 1;
  for (int i = 3; i <= n; i++) {
    for (int j = i - 3; j >= 0; j--) {
      dp[i] = (dp[i] + dp[j]) % mod;
    }
  }
  cout << dp[n] << endl;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

typedef long long ll;

const ll mod = 1e9 + 7;
ll dp[2002];

int main() {
  ll n;
  cin >> n;
  fill(dp + 3, dp + n + 1, 1);
  for (int i = 4; i <= n; i++) {
    for (int j = i - 3; j >= 3; j--) {
      dp[i] = (dp[i] + dp[j]) % mod;
    }
  }
  cout << dp[n] << endl;
}
Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
int dp[2100]; // dp[i] 和为 i 的方案数

int main() {
  int S;
  scanf("%d", &S);
  dp[0] = 1;
  for (int i = 3; i <= S; i++) {   // sum
    for (int k = 3; k <= i; k++) { // next
      dp[i] = (dp[i] + dp[i - k]) % MOD;
    }
  }
  printf("%d\\n", dp[S]);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
int dp[2100][2100]; // dp[i][j] 长度为i的序列和为j 的方案数

int main() {
  int S;
  scanf("%d", &S);
  dp[0][0] = 1;
  for (int i = 3; i <= S; i++) {       // sum
    for (int j = 1; j <= i / 3; j++) { // length
      for (int k = 3; k <= i; k++) {   // next
        dp[j][i] = (dp[j][i] + dp[j - 1][i - k]) % MOD;
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= S / 3; i++) {
    ans = (ans + dp[i][S]) % MOD;
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define ll long long
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define gmid ((l + r) >> 1)
using namespace std;

const int p = 1e9 + 7;

int f[2002];

int main() {
  int n;
  cin >> n;
  f[0] = 1;
  for (int i = 0; i <= n; ++i)
    for (int j = 3; j + i <= n; ++j)
      f[i + j] = (f[i + j] + f[i]) % p;
  cout << f[n] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define ll long long
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define gmid ((l + r) >> 1)
using namespace std;

const int p = 1e9 + 7;

int f[2002][2002];

int main() {
  int n;
  cin >> n;
  int k = n / 3;
  f[0][0] = 1;
  for (int i = 3; i <= n; ++i)
    for (int j = 1; j <= i / 3; ++j)
      for (int k = 3; k <= i - j + 1; ++k)
        f[i][j] = (f[i][j] + f[i - k][j - 1]) % p;
  long long ans = 0;
  for (int j = 1; j <= n / 3; ++j)
    ans = (ans + f[n][j]) % p;
  cout << ans << endl;
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1000000007
ll n;
ll Dp[2001];
long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }

ll solve(ll s) {
  if (s == 1 || s == 2)
    return 0LL;
  if (s == 3 || s == 4 || s == 5 || s == 0)
    return 1LL;
  if (Dp[s] == -1) {
    Dp[s] = 0;
    for (int j = 3; j <= n; j++) {
      if (s - j >= 0)
        Dp[s] = add(Dp[s], solve(s - j));
    }
  }
  // else Dp[s]++;
  return Dp[s];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  memset(Dp, -1, sizeof(Dp));
  cin >> n;
  cout << solve(n); //<<endl;
  // for(int i=0;i<=n;i++)
  // cout<<Dp[i]<<" ";
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1000000007
ll n;
ll Dp[2001];
long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }

ll solve(ll s) {
  if (s == 1 || s == 2)
    return 0;
  if (s == 3 || s == 4 || s == 5 || s == 0)
    return 1;
  if (Dp[s] == -1) {
    Dp[s] = 0;
    for (int j = 3; j <= n; j++) {
      Dp[s] = add(Dp[s], solve(s - j));
    }
  }
  // else Dp[s]++;
  return Dp[s];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  memset(Dp, -1, sizeof(Dp));
  cin >> n;
  cout << solve(n); //<<endl;
  // for(int i=0;i<=n;i++)
  // cout<<Dp[i]<<" ";
  return 0;
}
Problem p02555 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
// #include<atcoder/all>

using namespace std;
// using namespace atcoder;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

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

  long long S;
  const long long mod = 1000000007;
  cin >> S;
  long long dp[S + 10];
  for (int i = 0; i < S + 10; i++) {
    dp[i] = 0;
  }
  dp[0] = 1;
  for (int i = 0; i < S; i++) {
    for (int k = 3; k <= S - i; k++) {
      dp[i + k] += dp[i] % mod;
    }
  }

  cout << dp[S] % mod << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
// #include<atcoder/all>

using namespace std;
// using namespace atcoder;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

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

  long long S;
  const long long mod = 1000000007;
  cin >> S;
  long long dp[S + 10][S + 10];
  for (int i = 0; i < S + 10; i++) {
    for (int j = 0; j < S + 10; j++) {
      dp[i][j] = 0;
    }
  }
  dp[0][0] = 1;
  long long to = S / 3 + 1;
  for (int j = 0; j < to; j++) {
    for (int i = j; i < S; i++) {
      for (int k = 3; k <= S - i; k++) {
        dp[i + k][j + 1] += dp[i][j] % mod;
      }
    }
  }
  long long sum = 0;
  for (int i = 0; i <= S; i++) {
    sum += dp[S][i];
    sum %= mod;
    // cout<<sum<<endl;
  }
  cout << sum << endl;
}
Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

typedef long long ll;
const ll m = 1e9 + 7;

int main(int argc, char **argv) {
  int S;
  std::cin >> S;

  std::vector<ll> A(2001, 0);

  A[3] = 1;

  for (int i = 4; i < S + 1; i++) {
    A[i] = A[i - 3] + A[i - 1];
    A[i] %= m;
  }

  std::cout << A[S] << std::endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

typedef long long ll;
const ll m = 1e9 + 7;

int main(int argc, char **argv) {
  int S;
  std::cin >> S;

  std::vector<ll> A(S + 1, 0);

  A[3] = 1;

  for (int i = 4; i < S + 1; i++) {
    A[i] = A[i - 3] + A[i - 1];
    A[i] %= m;
  }

  std::cout << A[S] << std::endl;
}
Problem p02558 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
const int Mxn = 200005;
int p[Mxn];

int par(int u) {
  if (p[u] == u)
    return u;

  return (p[u] = par(p[u]));
}

void uni(int a, int b) {
  int p1 = par(a);
  int p2 = par(b);
  p[p2] = p1;
}

int main() {
  fastio;
  int n, q;
  cin >> n >> q;

  for (int i = 1; i <= n; i++)
    p[i] = i;
  while (q--) {
    int t, u, v;
    cin >> t >> u >> v;
    if (t == 0) {
      uni(u, v);
    } else {
      cout << (par(u) == par(v) ? 1 : 0) << "\\n";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
const int Mxn = 200005;
int p[Mxn];

int par(int u) {
  if (p[u] == u)
    return u;

  return (p[u] = par(p[u]));
}

int uni(int a, int b) {
  int p1 = par(a);
  int p2 = par(b);
  p[p2] = p1;
}

int main() {
  fastio;
  int n, q;
  cin >> n >> q;

  for (int i = 1; i <= n; i++)
    p[i] = i;
  while (q--) {
    int t, u, v;
    cin >> t >> u >> v;
    if (t == 0) {
      uni(u, v);
    } else {
      cout << (par(u) == par(v) ? 1 : 0) << "\\n";
    }
  }
  return 0;
}
Problem p02558 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>

int n, m;

struct unionfind {
  int parent;
  int node;
  int edge;
};
unionfind g[200001];

void init() {
  for (int i = 0; i <= n; i++) {
    g[i].parent = i;
    g[i].node = 1;
    g[i].edge = 0;
  }
}

int find(int x) {
  if (x == g[x].parent) {
    return x;
  }
  int res = find(g[x].parent);
  g[x].node = g[res].node;
  g[x].edge = g[res].edge;
  g[x].parent = res;
  return res;
}

void merge(int x, int y) {
  if (x > y) {
    x ^= y;
    y ^= x;
    x ^= y;
  }
  g[x].node += g[y].node;
  g[x].edge += g[y].edge + 1;
  g[y].parent = x;
}

int main() {
  int q, a, u, v;
  cin >> n >> q;
  init();
  for (int i = 0; i < q; i++) {
    cin >> a >> u >> v;
    u = find(u);
    v = find(v);
    if (a == 0 && u != v) {
      merge(u, v);
    } else if (a == 1) {
      cout << (u == v ? 1 : 0) << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>

int n, m;

struct unionfind {
  int parent;
  int node;
  int edge;
};
unionfind g[100001];

void init() {
  for (int i = 0; i <= n; i++) {
    g[i].parent = i;
    g[i].node = 1;
    g[i].edge = 0;
  }
}

int find(int x) {
  if (x == g[x].parent) {
    return x;
  }
  int res = find(g[x].parent);
  g[x].node = g[res].node;
  g[x].edge = g[res].edge;
  g[x].parent = res;
  return res;
}

void merge(int x, int y) {
  if (x > y) {
    x ^= y;
    y ^= x;
    x ^= y;
  }
  g[x].node += g[y].node;
  g[x].edge += g[y].edge + 1;
  g[y].parent = x;
}

int main() {
  int q, a, u, v;
  cin >> n >> q;
  init();
  for (int i = 0; i < q; i++) {
    cin >> a >> u >> v;
    u = find(u);
    v = find(v);
    if (a == 0 && u != v) {
      merge(u, v);
    } else if (a == 1) {
      cout << (u == v ? 1 : 0) << endl;
    }
  }
}
Problem p02558 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int id[200010];
int root(int i) { return !id[i] ? i : id[i] = root(id[i]); }
int main() {
  int n, q;
  cin >> n >> q;
  while (q--) {
    int t, u, v;
    cin >> t >> u >> v;
    u++, v++;
    if (t) {
      cout << (root(u) == root(v)) << "\\n";
    } else {
      if (root(v) != root(u))
        id[root(v)] = root(u);
    }
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int id[200010];
int root(int i) { return !id[i] ? i : id[i] = root(id[i]); }
int main() {
  int n, q;
  cin >> n >> q;
  while (q--) {
    int t, u, v;
    cin >> t >> u >> v;
    if (t) {
      cout << (root(u) == root(v)) << "\\n";
    } else {
      id[root(v)] = root(u);
    }
  }
}
Problem p02558 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<int> par, size;
  UnionFind(int x) {
    par.resize(x);
    size.resize(x, 1);
    for (int i = 0; i < x; i++) {
      par[i] = i;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int consize(int x) { return size[find(x)]; }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (size[x] < size[y]) {
      par[x] = y;
      size[y] += size[x];
    } else {
      par[y] = x;
      size[x] += size[y];
    }
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  UnionFind Onion(N);
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0)
      Onion.unite(b, c);
    else
      cout << (Onion.same(b, c) ? 1 : 0) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<int> par, size;
  UnionFind(int x) {
    par.resize(x);
    size.resize(x, 1);
    for (int i = 0; i < x; i++) {
      par[i] = i;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int consize(int x) { return size[find(x)]; }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (size[x] < size[y]) {
      par[x] = y;
      size[y] += size[x];
    } else {
      par[y] = x;
      size[x] += size[y];
    }
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  UnionFind Onion(M);
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0)
      Onion.unite(b, c);
    else
      cout << (Onion.same(b, c) ? 1 : 0) << endl;
  }
}
Problem p02558 (C++) — buggy submission, verdict: Runtime Error

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

#pragma region lib_unionfind_tree
namespace lib {
/*
  class UnionFind
  usage: Unionfind u(100), u.unite(1, 10), u.find(1, 13)
*/
class UnionFind {
public:
  UnionFind() {}
  UnionFind(int n) : parent(n, -1), rank(n, 0) {}

  int find(int a) {
    if (parent[a] == -1)
      return a;
    else
      return parent[a] = find(parent[a]);
  }

  void unite(int a, int b) {
    a = find(a);
    b = find(b);

    if (a == b)
      return;
    if (rank[a] == rank[b]) {
      parent[a] = b;
      rank[b]++;
      return;
    }
    if (rank[a] > rank[b])
      swap(a, b);
    parent[a] = b;
  }

private:
  vector<int> parent, rank;
};
} // namespace lib
#pragma endregion

int main() {
  int N, Q;
  scanf("%d%d", &N, &Q);

  lib::UnionFind uf(N);

  while (Q--) {
    int t, u, v;
    scanf("%d%d%d", &t, &u, &v);

    if (t == 0)
      uf.unite(u, v);
    else
      printf("%d\\n", static_cast<int>(uf.find(u) == uf.find(v)));
  }

  return 0;
}


Error: -11

Fixed submission:

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

#pragma region lib_unionfind_tree
namespace lib {
/*
  class UnionFind
  usage: Unionfind u(100), u.unite(1, 10), u.find(1, 13)
*/
class UnionFind {
public:
  UnionFind() {}
  UnionFind(int n) : parent(n, -1), rank(n, 0) {}

  int find(int a) {
    if (parent[a] == -1)
      return a;
    else
      return parent[a] = find(parent[a]);
  }

  void unite(int a, int b) {
    a = find(a);
    b = find(b);

    if (a == b)
      return;
    if (rank[a] == rank[b]) {
      parent[a] = b;
      rank[b]++;
      return;
    }
    if (rank[a] > rank[b])
      swap(a, b);
    parent[a] = b;
  }

private:
  vector<int> parent, rank;
};
} // namespace lib
#pragma endregion

int main() {
  int N, Q;
  scanf("%d%d%d", &N, &Q);

  lib::UnionFind uf(N);

  while (Q--) {
    int t, u, v;
    scanf("%d%d%d", &t, &u, &v);

    if (t == 0)
      uf.unite(u, v);
    else
      printf("%d\\n", (uf.find(u) == uf.find(v)));
  }

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

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

vector<int> root;
vector<int> Rank;

// 通常バージョン
int find(int x) {
  if (root[x] == x)
    return x;
  root[x] = find(root[x]);
  return root[x];
}

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

void Union(int x, int y) {
  x = find(x);
  y = find(y);
  if (check(x, y)) {
    return;
  }
  if (Rank[x] >= Rank[y])
    root[y] = x;
  else
    root[x] = y;
  if (Rank[x] == Rank[y])
    Rank[x]++;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> set_root(n);
  for (int i = 0; i < n; i++)
    set_root[i] = i;
  root = set_root;
  vector<int> set_Rank(n, 0);
  Rank = set_Rank;
  for (int i = 0; i < m; i++) {
    int t, u, v;
    cin >> t >> u >> v;
    if (t == 0)
      Union(u, v);
    if (t == 1)
      cout << check(u, v) << endl;
  }
}

Error: WA

Fixed submission:

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

vector<int> root;
vector<int> Rank;

// 通常バージョン
int find(int x) {
  if (root[x] == x)
    return x;
  root[x] = find(root[x]);
  return root[x];
}

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

void Union(int x, int y) {
  x = find(x);
  y = find(y);
  if (check(x, y)) {
    return;
  }
  if (Rank[x] >= Rank[y])
    root[y] = x;
  else
    root[x] = y;
  if (Rank[x] == Rank[y])
    Rank[x]++;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> set_root(n);
  for (int i = 0; i < n; i++)
    set_root[i] = i;
  root = set_root;
  vector<int> set_Rank(n, 0);
  Rank = set_Rank;
  for (int i = 0; i < m; i++) {
    int t, u, v;
    cin >> t >> u >> v;
    u--;
    v--;
    if (t == 0)
      Union(u, v);
    if (t == 1)
      cout << check(u, v) << endl;
  }
}
Problem p02558 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define int long long
using namespace std;

const int N = 2e5 + 5;
const int mod = 1e9 + 7;

int parent[N], siz[N];

void make_set(int v) { parent[v] = v; }

int find_set(int v) {
  while (v != parent[v])
    v = parent[v];
  return v;
}

void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (siz[a] < siz[b])
      swap(a, b);
    parent[b] = a;
    siz[a] += siz[b];
  }
}

int32_t main() {
  fast;
  memset(siz, 0, sizeof siz);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    make_set(i);
  while (q--) {
    int t, u, v;
    cin >> t >> u >> v;
    if (t == 0)
      union_sets(u, v);
    else {
      if (find_set(u) == find_set(v))
        cout << 1 << endl;
      else
        cout << 0 << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define int long long
using namespace std;

const int N = 1e5 + 5;
const int mod = 1e9 + 7;

int parent[N], siz[N];

void make_set(int v) { parent[v] = v; }

int find_set(int v) {
  while (v != parent[v])
    v = parent[v];
  return v;
}

void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (siz[a] < siz[b])
      swap(a, b);
    parent[b] = a;
    siz[a] += siz[b];
  }
}

int32_t main() {
  fast;
  memset(siz, 0, sizeof siz);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    make_set(i);
  while (q--) {
    int t, u, v;
    cin >> t >> u >> v;
    if (t == 0)
      union_sets(u, v);
    else {
      if (find_set(u) == find_set(v))
        cout << 1 << endl;
      else
        cout << 0 << endl;
    }
  }
}