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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

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

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    uf.merge(a, b);
  }

  set<int> s;
  rep(i, n) { s.insert(uf.root(i)); }

  ll ans = s.size() - 1;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

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

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    uf.merge(a, b);
  }

  set<int> s;
  rep(i, n) { s.insert(uf.root(i)); }

  cout << s.size() - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> parent;
  UnionFind(int N) : parent(N) {
    for (int i = 0; i < N; i++) {
      parent[i] = i;
    }
  }
  int getParent(int x) { return parent[x]; }

  int root(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return parent[x] = root(parent[x]);
    }
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx != ry) {
      parent[ry] = rx;
    }
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(int argc, char *argv[]) {
  int N, M;
  scanf("%d %d", &N, &M);
  vector<int> A(M);
  vector<int> B(M);
  UnionFind tree(N);
  for (int i = 0; i < M; i++) {
    scanf("%d %d", &A[i], &B[i]);
    if (A[i] > B[i]) {
      int tmp;
      tmp = B[i];
      B[i] = A[i];
      A[i] = tmp;
    }
    tree.unite(A[i] - 1, B[i] - 1);
  }
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (tree.getParent(i) == i) {
      count++;
    }
  }
  printf("%d\\n", count - 1);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> parent;
  UnionFind(int N) : parent(N) {
    for (int i = 0; i < N; i++) {
      parent[i] = i;
    }
  }
  int getParent(int x) { return parent[x]; }

  int root(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return root(parent[x]);
    }
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx != ry) {
      parent[rx] = ry;
    }
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(int argc, char *argv[]) {
  int N, M;
  scanf("%d %d", &N, &M);
  vector<int> A(M);
  vector<int> B(M);
  UnionFind tree(N);
  for (int i = 0; i < M; i++) {
    scanf("%d %d", &A[i], &B[i]);
    if (A[i] > B[i]) {
      int tmp;
      tmp = B[i];
      B[i] = A[i];
      A[i] = tmp;
    }
    tree.unite(A[i] - 1, B[i] - 1);
  }
  int count = 0;
  for (int i = 0; i < N; i++) {
    //		printf("%d\\n", tree.root(i));
  }
  for (int i = 0; i < N; i++) {
    if (tree.getParent(i) == i) {
      count++;
    }
  }
  printf("%d\\n", count - 1);
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;

class DisjointSet {
public:
  vector<int> rank, p;

  DisjointSet() {}
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++) {
      makeSet(i);
    }
  }

  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  }

  void unite(int x, int y) { link(findSet(x), findSet(y)); }

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  }

  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  }
};

int main() {
  int N, M, A, B;
  cin >> N >> M;
  DisjointSet ds = DisjointSet(N);

  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    ds.unite(A - 1, B - 1);
  }

  set<int> check;
  for (int i = 0; i < N; i++) {
    check.insert(ds.findSet(i));
  }
  cout << check.size() - 1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;

class DisjointSet {
public:
  vector<int> rank, p;

  DisjointSet() {}
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++) {
      makeSet(i);
    }
  }

  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  }

  void unite(int x, int y) { link(findSet(x), findSet(y)); }

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  }

  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  }
};

int main() {
  int N, M, A, B;
  cin >> N >> M;
  DisjointSet ds = DisjointSet(N);

  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    ds.unite(A, B);
  }

  set<int> check;
  for (int i = 0; i < N; i++) {
    check.insert(ds.findSet(i));
  }
  cout << check.size() - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(void) {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    tree.unite(A, B);
  }

  set<int> count{};
  for (int i = 0; i < N; i++) {
    count.insert(tree.root(i));
  }

  cout << count.size() - 1 << endl;
}

Error: 0

Fixed submission:

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

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(void) {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);

  for (int i = 0; i < M; i++) {
    int A, B;
    A--;
    B--;
    cin >> A >> B;
    tree.unite(A, B);
  }

  set<int> count{};
  for (int i = 0; i < N; i++) {
    count.insert(tree.root(i));
  }

  cout << count.size() - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include "stdio.h"
#include <vector>
using namespace std;

vector<int> gra[100010];
int v[100020];

int DFS(int s) {
  v[s] = 1;
  for (int i = 0; i < gra[s].size(); i++) {
    if (v[gra[s][i]] == 0)
      DFS(gra[s][i]);
  }
  return 0;
}

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++) {
    v[i] = 0;
  }
  int v1, v2;
  for (int i = 0; i < M; i++) {
    scanf("%d%d", &v1, &v2);
    gra[v1].push_back(v2);
    gra[v2].push_back(v1);
  }
  int res = 0;
  for (int i = 1; i <= N; i++)
    if (v[i] == 0) {
      DFS(i);
      res++;
    }
  printf("%d", res - 1);
}

Error: WA

Fixed submission:

#include "stdio.h"
#include <vector>
using namespace std;

vector<int> gra[10001];
int v[10002];

int DFS(int s) {
  v[s] = 1;
  for (int i = 0; i < gra[s].size(); i++) {
    if (v[gra[s][i]] == 0)
      DFS(gra[s][i]);
  }
  return 0;
}

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++) {
    v[i] = 0;
  }
  int v1, v2;
  for (int i = 0; i < M; i++) {
    scanf("%d%d", &v1, &v2);
    gra[v1].push_back(v2);
    gra[v2].push_back(v1);
  }
  int res = 0;
  for (int i = 1; i <= N; i++)
    if (v[i] == 0) {
      DFS(i);
      res++;
    }
  printf("%d", res - 1);
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int dsu[100010];
int fnd(int x) { return (x == dsu[x]) ? x : dsu[x] = fnd(dsu[x]); }
void merge(int u, int v) { dsu[fnd(u)] = fnd(v); }
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    dsu[i] = i;
  }
  int x, y, cnt = 0;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    merge(x, y);
  }
  for (int i = 1; i < n; i++) {
    if (fnd(i) != fnd(i + 1)) {
      merge(i, i + 1);
      cnt++;
    }
  }
  cout << cnt;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int dsu[100010];
int fnd(int x) { return (x == dsu[x]) ? x : dsu[x] = fnd(dsu[x]); }
int merge(int u, int v) { dsu[fnd(u)] = fnd(v); }
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    dsu[i] = i;
  }
  int x, y, cnt = 0;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    merge(x, y);
  }
  for (int i = 1; i < n; i++) {
    if (fnd(i) != fnd(i + 1)) {
      merge(i, i + 1);
      cnt++;
    }
  }
  cout << cnt;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
int n, m, fa[1000001], x, y, ans;
int father(int a) {
  if (fa[a] != a)
    fa[a] = father(fa[a]);
  return fa[a];
}
int main() {
  scanf("%d%d", &n, &m);
  ans = n - 1;
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    x = father(x);
    y = father(y);
    if (x != y) {
      fa[x] = y;
      ans--;
    }
  }
  printf("%d\\n", ans);
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int n, m, fa[1000001], x, y, ans;
int father(int a) {
  if (fa[a] != a)
    fa[a] = father(a);
  return fa[a];
}
int main() {
  scanf("%d%d", &n, &m);
  ans = n - 1;
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    x = father(x);
    y = father(y);
    if (x != y) {
      fa[x] = y;
      ans--;
    }
  }
  printf("%d\\n", ans);
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ll = long long;

struct unionfind {
  vi r;

  unionfind(int n) { r = vi(n, -1); }

  int root(int x) {
    if (r.at(x) == -1)
      return x;
    return r.at(x) = root(r.at(x));
  }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    r.at(y) = x;
    // if(r.at(x)>r.at(y)) swap(x,y);
    // r.at(x) += r.at(y);
    // r.at(y) = x;
    return true;
  }
};

int main() {
  int n, m, cnt;
  cin >> n >> m;
  cnt = n;
  unionfind uf(n);
  for (int ix = 0; ix < m; ix++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    // uf.unite(a,b);
    if (uf.unite(a, b) == true) {
      cnt--;
    }
  }
  cout << max(0, cnt - 1) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ll = long long;

struct unionfind {
  vi r;

  unionfind(int n) { r = vi(n, -1); }

  int root(int x) {
    if (r.at(x) == -1)
      return x;
    return r.at(x) = root(r.at(x));
  }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r.at(x) > r.at(y))
      swap(x, y);
    r.at(x) += r.at(y);
    r.at(y) = x;
    return true;
  }
};

int main() {
  int n, m, cnt;
  cin >> n >> m;
  cnt = n;
  unionfind uf(n);
  for (int ix = 0; ix < m; ix++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (uf.unite(a, b) == true) {
      cnt--;
    }
  }
  cout << (cnt == 0 ? 0 : cnt - 1) << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)

#define ll long long

#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value)                                 \\
  vector<vector<type>> name(length, vector<type>(width, value));

int main() {
  struct UnionFind {
    vector<int> par; // 親の番号

    UnionFind(int N) : par(N) { // 初期化で最初はすべてが根であるとする
      for (int i = 0; i < N; i++)
        par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰的に求める
      if (par[x] == x)
        return x;                   // 根を返す
      return par[x] = root(par[x]); // データxの親の番号でroot()を再帰する
    }

    void unite(int x, int y) { // xとyの木を併合
      int rx = root(x);        // xの根
      int ry = root(y);        // yの根
      if (rx == ry)
        return; // 同じ木にある時はそのまま
      par[rx] = ry; // 別々の木にある時は根を同じにしてくっつける
    }

    bool same(int x, int y) { // xとyが同じ木に属するか判別する
      int rx = root(x);
      int ry = root(y);
      return rx == ry;
    }
  };

  int N, M;
  cin >> N >> M;
  UnionFind u(N);
  froop1(0, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    u.unite(a, b);
  }
  int count = 0;
  set<int> m;
  froop1(0, N) {
    int p = u.root(i);
    m.insert(p);
  }
  count = m.size() - 1;

  cout << count << endl;
}

Error: 0

Fixed submission:

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

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)

#define ll long long

#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value)                                 \\
  vector<vector<type>> name(length, vector<type>(width, value));

int main() {
  struct UnionFind {
    vector<int> par; // 親の番号

    UnionFind(int N) : par(N) { // 初期化で最初はすべてが根であるとする
      for (int i = 0; i < N; i++)
        par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰的に求める
      if (par[x] == x)
        return x;                   // 根を返す
      return par[x] = root(par[x]); // データxの親の番号でroot()を再帰する
    }

    void unite(int x, int y) { // xとyの木を併合
      int rx = root(x);        // xの根
      int ry = root(y);        // yの根
      if (rx == ry)
        return; // 同じ木にある時はそのまま
      par[rx] = ry; // 別々の木にある時は根を同じにしてくっつける
    }

    bool same(int x, int y) { // xとyが同じ木に属するか判別する
      int rx = root(x);
      int ry = root(y);
      return rx == ry;
    }
  };

  int N, M;
  cin >> N >> M;
  UnionFind u(N);
  froop1(0, M) {
    int a, b;
    cin >> a >> b;
    u.unite(a, b);
  }
  int count = 0;
  set<int> m;
  froop1(0, N) {
    int p = u.par[i];
    m.insert(p);
  }
  count = m.size() - 1;

  cout << count << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

template <typename T> void drop(const T &x) {
  cout << x << '\\n';
  exit(0);
}

struct UnionFind {
  vector<int> d;

  UnionFind(int n) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }

  bool unite(int x, int y) {
    x = root(x);
    y = root(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 root(x) == root(y); }

  int size(int x) { return -d[root(x)]; }
};

void milkshake() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  int ans = n - 1;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (!uf.same(a, b)) {
      uf.unite(a, b);
      ans--;
    }
  }
  cout << ans << '\\n';
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt = 1;
  // cin >> tt;
  while (tt--)
    milkshake();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

template <typename T> void drop(const T &x) {
  cout << x << '\\n';
  exit(0);
}

int n, m;
vector<vector<int>> road;
vector<bool> seen;

void dfs(int c) {
  seen[c] = true;
  for (auto nc : road[c]) {
    if (seen[nc])
      continue;
    dfs(nc);
  }
}

void milkshake() {
  cin >> n >> m;
  road.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    road[a].emplace_back(b);
    road[b].emplace_back(a);
  }
  bool ok = false;
  int ans = 0;
  while (!ok) {
    seen.assign(n, false);
    dfs(0);
    for (int i = 0; i < n; i++) {
      if (!seen[i]) {
        road[0].emplace_back(i);
        ans++;
        ok = false;
        break;
      }
      ok = true;
    }
  }
  cout << ans << '\\n';
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt = 1;
  // cin >> tt;
  while (tt--)
    milkshake();

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

#include <bits/stdc++.h>

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

  std::vector<int> union_find(N);
  for (int i = 0; i < N; i++) {
    union_find[i] = i;
  }

  auto find_root = [&union_find](int idx) {
    std::vector<int> inter;
    while (union_find[idx] != idx) {
      inter.emplace_back(idx);
      idx = union_find[idx];
    }
    for (auto i : inter) {
      union_find[i] = idx;
    }
    return idx;
  };

  int a, b;
  for (int i = 0; i < M; i++) {
    std::cin >> a >> b;
    a--;
    b--;
    int root_a = find_root(a);
    int root_b = find_root(b);
    if (root_a != root_b) {
      union_find[root_a] = root_b;
    }
  }
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    int root = find_root(i);
    if (root == i) {
      cnt++;
    }
  }
  std::cout << cnt - 1 << std::endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

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

  std::vector<int> union_find(N);
  for (int i = 0; i < N; i++) {
    union_find[i] = i;
  }
  int a, b;
  auto find_root = [&union_find](int idx) {
    std::vector<int> inter;
    while (union_find[idx] != idx) {
      inter.emplace_back(idx);
      idx = union_find[idx];
    }
    for (auto i : inter) {
      union_find[i] = idx;
    }
    return idx;
  };

  for (int i = 0; i < M; i++) {
    std::cin >> a >> b;
    int root_a = find_root(a);
    int root_b = find_root(b);
    if (root_a != root_b) {
      union_find[root_a] = root_b;
    }
  }
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    int root = find_root(i);
    if (root == i) {
      cnt++;
    }
  }
  std::cout << cnt - 1 << std::endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

#define ll long long

struct dsu {
public:
  dsu() : _n(0) {}
  dsu(int n) : _n(n), parent_or_size(n, -1) {}

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

  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);
    if (parent_or_size[a] < 0)
      return a;
    return parent_or_size[a] = leader(parent_or_size[a]);
  }

  int size(int a) {
    assert(0 <= a && a < _n);
    return -parent_or_size[leader(a)];
  }

  std::vector<std::vector<int>> groups() {
    std::vector<int> leader_buf(_n), group_size(_n);
    for (int i = 0; i < _n; i++) {
      leader_buf[i] = leader(i);
      group_size[leader_buf[i]]++;
    }
    std::vector<std::vector<int>> result(_n);
    for (int i = 0; i < _n; i++) {
      result[i].reserve(group_size[i]);
    }
    for (int i = 0; i < _n; i++) {
      result[leader_buf[i]].push_back(i);
    }
    result.erase(
        std::remove_if(result.begin(), result.end(),
                       [&](const std::vector<int> &v) { return v.empty(); }),
        result.end());
    return result;
  }

private:
  int _n;
  // root node: -1 * component size
  // otherwise: parent
  std::vector<int> parent_or_size;
};

int main() {
  int N, M;
  std::cin >> N >> M;
  dsu d(N);
  for (int i = 0; i < M; i++) {
    int x, y;
    std::cin >> x >> y;
    --x;
    --y;
    d.merge(x, y);
  }
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (d.leader(i) == i)
      cnt++;
  }
  std::cout << cnt - 1 << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define ll long long

struct UnionFind {
  std::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])
      std::swap(x, y);
    d[x] += d[y];
    d[y] = x;
    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() {
  int N, M;
  std::cin >> N >> M;
  UnionFind uf(N);
  for (int i = 0; i < M; i++) {
    int x, y;
    x--;
    y--;
    std::cin >> x >> y;
    uf.unite(x, y);
  }

  int ans = 0;
  std::map<int, int> data;
  for (int i = 0; i < N; i++) {
    int tmp = uf.size(i);
    if (data[tmp] == 0 and tmp > 1) {
      ans++;
      data[tmp] = 1;
    }
    if (tmp == 1) {
      ans++;
    }
  }
  std::cout << ans - 1 << std::endl;

  return 0;
}