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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

using namespace std;

class UnionFind {
public:
  UnionFind() : _n{0} {}
  UnionFind(int n) : _n{n}, pr(n, -1) {}

  int merge(int a, int b) {
    assert(0 <= a && a < _n);
    assert(0 <= b && b < _n);
    a = leader(a);
    b = leader(b);
    if (a == b)
      return a;
    if (-pr[a] < -pr[b])
      swap(a, b);
    pr[a] += pr[b];
    pr[b] = a;
    return a;
  }

  bool same(int a, int b) {
    assert(0 <= a && a < _n);
    assert(0 <= b && b < _n);
    return leader(a) == leader(b);
  }

  int leader(int a) {
    assert(0 <= a && a < _n);
    return (pr[a] < 0) ? a : (pr[a] = leader(pr[a]));
  }

private:
  int _n;
  vector<int> pr;
};

int main() {
  int N, Q;
  cin >> N >> Q;
  UnionFind U{N};
  while (Q--) {
    int t, u, v;
    cin >> t >> u >> v;
    if (!t) {
      U.merge(u, v);
    } else {
      cout << U.same(u, v) << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

class UnionFind {
private:
  vector<int> r;

public:
  UnionFind(int N) { r.assign(N, -1); }

  int root(int x) { return (r[x] < 0) ? x : root(r[x]); }

  bool connected(int x, int y) { return root(x) == root(y); }

  void merge(int x, int y) {
    x = root(x);
    y = root(y);

    if (r[x] < r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  UnionFind U{N};
  while (Q--) {
    int t, u, v;
    cin >> t >> u >> v;
    if (!t) {
      U.merge(u, v);
    } else {
      cout << U.connected(u, v) << endl;
    }
  }
  return 0;
}
Problem p02558 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

struct Unionfind {
  vector<int> par;
  vector<int> rank;

  void init(int n) {
    par.resize(n);
    rank.resize(n);
    for (int i = 0; i < n; 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 {
      if (rank[x] < rank[y])
        par[x] = y;
      else {
        par[y] = x;
        if (rank[x] == rank[y])
          rank[x]++;
      }
    }
  }

  bool issame(int x, int y) { return root(x) == root(y); }
};

int main() {
  int n, q;
  cin >> n >> q;
  Unionfind uf;
  uf.init(n);
  for (int i = 0; i < q; i++) {
    int query, a, b;
    cin >> query >> a >> b;
    if (query == 0)
      uf.unite(a, b);
    else
      cout << (uf.issame(a, b) ? 1 : 0) << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

struct Unionfind {
  vector<int> par;
  vector<int> rank;

  void init(int n) {
    par.resize(n);
    rank.resize(n);
    for (int i = 0; i < n; 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 {
      if (rank[x] < rank[y])
        par[x] = y;
      else {
        par[y] = x;
        if (rank[x] == rank[y])
          rank[x]++;
      }
    }
  }

  bool issame(int x, int y) { return root(x) == root(y); }
};

int main() {
  int n, q;
  cin >> n >> q;
  Unionfind uf;
  uf.init(n);
  for (int i = 0; i < q; i++) {
    int query, a, b;
    cin >> query >> a >> b, a--, b--;
    if (query == 0)
      uf.unite(a, b);
    else
      cout << (uf.issame(a, b) ? 1 : 0) << endl;
  }
}