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

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

using namespace std;

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

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

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }
};

int main() {

  int N, M, count = 0;
  cin >> N >> M;
  UnionFind tree(N);

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

  cout << count - 1 << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

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

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }
};

int main() {

  int N, M, count = 0;
  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);
  }
  for (int i = 0; i < N; i++) {
    if (tree.root(i) == i)
      count++;
  }

  cout << count - 1 << endl;
  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

class UnionFind {
public:
  explicit UnionFind(int N) : root(N, -1), size(N, 1) {}
  int getRoot(int u) { return root[u] == -1 ? u : root[u] = getRoot(root[u]); }
  int getSize(int u) { return size[getRoot(u)]; }
  bool same(int a, int b) { return getRoot(a) == getRoot(b); }
  bool merge(int a, int b) {
    int u = getRoot(a);
    int v = getRoot(b);
    if (u == v)
      return false;
    root[u] = v;
    size[v] += size[u];
    return true;
  }

private:
  vector<int> root;
  vector<int> size;
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    uf.merge(a, b);
  }
  int res = -1;
  for (int i = 0; i < N; i++)
    if (uf.getRoot(i) == i)
      ++res;
  cout << res << endl;
}

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

using namespace std;

class UnionFind {
public:
  explicit UnionFind(int N) : root(N, -1), size(N, 1) {}
  int getRoot(int u) { return root[u] == -1 ? u : root[u] = getRoot(root[u]); }
  int getSize(int u) { return size[getRoot(u)]; }
  bool same(int a, int b) { return getRoot(a) == getRoot(b); }
  bool merge(int a, int b) {
    int u = getRoot(a);
    int v = getRoot(b);
    if (u == v)
      return false;
    root[u] = v;
    size[v] += size[u];
    return true;
  }

private:
  vector<int> root;
  vector<int> size;
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    uf.merge(a, b);
  }
  int res = -1;
  for (int i = 0; i < N; i++)
    if (uf.getRoot(i) == i)
      ++res;
  cout << res << endl;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

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

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

  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につける
    groups--;
  }

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

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind t(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    t.unite(a, b);
  }
  cout << t.groups - 1 << endl;
}

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

using namespace std;

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

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

  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につける
    groups--;
  }

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

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind t(N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    t.unite(a, b);
  }
  cout << t.groups - 1 << endl;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#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;
  int N;
  UnionFind(int n) : N(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 {
      parent[x] = root(parent[x]); // 親の付け替え(ならし)
      return 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);
}


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;
  int N;
  UnionFind(int n) : parent(n), N(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 {
      parent[x] = root(parent[x]); // 親の付け替え(ならし)
      return 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);
}
Problem p02536 (C++)

Buggy submission (status: 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 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);
}


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 {
      parent[x] = root(parent[x]);
      return 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);
}
Problem p02536 (C++)

Buggy submission (status: 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, B);
  }

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


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 - 1, B - 1);
  }

  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 (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
const int N = 1e9 + 7;
#define int long long
#define double long double
#define pb push_back
#define vi vector<int>
#define rep(i, n) for (int i = 1; i <= n; i++)
vi ar[100001];
int vis[100000];
void dfs(int node) {
  vis[node] = 1;
  for (int child : ar[node]) {
    if (!vis[child])
      dfs(child);
  }
}
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  int n, m, a, b;
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    ar[a].pb(b), ar[b].pb(a);
  }
  int cc = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cc++;
      dfs(i);
    }
  }
  cout << cc - 1;

  return 0;
}


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

#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
const int N = 1e9 + 7;
#define int long long
#define double long double
#define pb push_back
#define vi vector<int>
#define rep(i, n) for (int i = 1; i <= n; i++)
vi ar[100001];
int vis[100001];
void dfs(int node) {
  vis[node] = 1;
  for (int child : ar[node]) {
    if (!vis[child])
      dfs(child);
  }
}
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  int n, m, a, b;
  cin >> n >> m;
  while (m--) {
    cin >> a >> b;
    ar[a].pb(b), ar[b].pb(a);
  }
  int cc = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cc++;
      dfs(i);
    }
  }
  cout << cc - 1;

  return 0;
}
Problem p02536 (C++)

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

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

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

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(void) {
  long long N, M;
  cin >> N >> M;
  vector<long long> A(M);
  vector<long long> B(M);
  for (long long i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }
  UnionFind tree(N);
  for (long long i = 0; i < M; i++) {
    tree.unite(A[i], B[i]);
  }
  vector<long long> count(N, 0);
  long long ans = 0;
  for (long long i = 0; i < N; i++) {
    if (count[tree.root(i)] == 1) {
    } else {
      count[tree.root(i)] = 1;
      ans++;
    }
  }
  cout << ans - 1 << endl;
}

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

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

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

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(void) {
  long long N, M;
  cin >> N >> M;
  vector<long long> A(M);
  vector<long long> B(M);
  for (long long i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }
  UnionFind tree(N);
  for (long long i = 0; i < M; i++) {
    tree.unite(A[i] - 1, B[i] - 1);
  }
  vector<long long> count(N, 0);
  long long ans = 0;
  for (long long i = 0; i < N; i++) {
    if (count[tree.root(i)] == 1) {
    } else {
      count[tree.root(i)] = 1;
      ans++;
    }
  }
  cout << ans - 1 << endl;
}
Problem p02536 (C++)

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

#define all(x) x.begin(), x.end()

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long long MOD = 1e9 + 7;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
const int MAXN = 200050;

int fa[MAXN];
int find(int x) {
  if (fa[x] == x)
    return x;
  else
    return fa[x] = find(fa[x]);
}
void solve(int T) {
  int n, m, u, v;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &u, &v);
    fa[find(u)] = v;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (fa[i] == i)
      ans++;
  }
  printf("%d", ans - 1);
}
signed main() {
  int t = 1;
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  // scanf("%d",&t);
  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}

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

#define all(x) x.begin(), x.end()

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long long MOD = 1e9 + 7;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
const int MAXN = 200050;

int fa[MAXN];
int find(int x) {
  if (fa[x] == x)
    return x;
  else
    return fa[x] = find(fa[x]);
}
void solve(int T) {
  int n, m, u, v;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &u, &v);
    fa[find(u)] = find(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (fa[i] == i)
      ans++;
  }
  printf("%d", ans - 1);
}
signed main() {
  int t = 1;
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  // scanf("%d",&t);
  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}
Problem p02536 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int father(vector<int> &f, int x) {
  return (f[x] == x) ? x : (father(f, f[x]));
}
int merge(vector<int> &f, int x, int y) {
  x = father(f, x);
  y = father(f, y);
  if (x == y)
    return 0;
  f[x] = y;
  return 1;
}
int main() {
  int n;
  int m;
  cin >> n >> m;
  vector<int> f(n + 1, 0);
  for (int i = 1; i <= n; i++)
    f[i] = i;
  int ans = n - 1;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (merge(f, x, y))
      ans--;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int father(vector<int> &f, int x) {
  return (f[x] == x) ? x : (f[x] = father(f, f[x]));
}
int merge(vector<int> &f, int x, int y) {
  x = father(f, x);
  y = father(f, y);
  if (x == y)
    return 0;
  f[x] = y;
  return 1;
}
int main() {
  int n;
  int m;
  cin >> n >> m;
  vector<int> f(n + 1, 0);
  for (int i = 1; i <= n; i++)
    f[i] = i;
  int ans = n - 1;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (merge(f, x, y))
      ans--;
  }
  cout << ans << endl;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <vector>
#define maxn 1000
using namespace std;

vector<int> G[maxn];
bool vis[maxn] = {false};

int m, n;

void dfs(int v) {
  vis[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (vis[G[v][i]] == false) {
      dfs(G[v][i]);
    }
  }
}

int main() {
  scanf("%d %d", &m, &n);
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    G[a].push_back(b);
    G[b].push_back(a);
  }

  int block = 0;
  for (int i = 1; i <= m; i++) {
    if (vis[i] == false) {
      dfs(i);
      block++;
    }
  }

  printf("%d", block - 1);

  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>
#define maxn 200000
using namespace std;

vector<int> G[maxn];
bool vis[maxn] = {false};

int m, n;

void dfs(int v) {
  vis[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (vis[G[v][i]] == false) {
      dfs(G[v][i]);
    }
  }
}

int main() {
  scanf("%d %d", &m, &n);
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    G[a].push_back(b);
    G[b].push_back(a);
  }

  int block = 0;
  for (int i = 1; i <= m; i++) {
    if (vis[i] == false) {
      dfs(i);
      block++;
    }
  }

  printf("%d", block - 1);

  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }

  int cnt() {
    int cnt = 0;
    for (auto it : data)
      if (it < 0)
        cnt++;
    return cnt;
  }
};

void solve() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }
  cout << uf.cnt() - 1 << endl;
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }

  int cnt() {
    int cnt = 0;
    for (auto it : data)
      if (it < 0)
        cnt++;
    return cnt;
  }
};

void solve() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }
  cout << uf.cnt() - 1 << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02536 (C++)

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

int main() {
  long long n, m;
  cin >> n >> m;
  vector<vector<int>> michi(n, vector<int>(0));
  vector<int> checked(n, 0);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    michi.at(a).push_back(b);
    michi.at(b).push_back(a);
  }
  long long ans = 0;
  int y = 1;
  int l = 0;
  while (y != 0) {
    queue<int> q;
    q.push(l);
    checked.at(l) = 1;
    while (q.size()) {
      int x = q.front();
      q.pop();

      for (int i = 0; i < (int)michi.at(x).size(); i++) {
        int u = michi.at(x).at(i);
        if (checked.at(u) == 1) {
          continue;
        }
        checked.at(u) = 1;
        q.push(u);
      }
    }
    y = 0;
    for (int i = 0; i < n; i++) {
      if (checked.at(i) == 0) {
        l = i;
        y = 1;
        // cout << l << endl;
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}


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

int main() {
  long long n, m;
  cin >> n >> m;
  vector<vector<int>> michi(n, vector<int>(0));
  vector<int> checked(n, 0);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    michi.at(a).push_back(b);
    michi.at(b).push_back(a);
  }
  long long ans = 0;
  int y = 1;
  int l = 0;
  while (y != 0) {
    queue<int> q;
    q.push(l);
    checked.at(l) = 1;
    while (q.size()) {
      int x = q.front();
      q.pop();

      for (int i = 0; i < (int)michi.at(x).size(); i++) {
        int u = michi.at(x).at(i);
        if (checked.at(u) == 1) {
          continue;
        }
        checked.at(u) = 1;
        q.push(u);
      }
    }
    y = 0;
    for (int i = l; i < n; i++) {
      if (checked.at(i) == 0) {
        l = i;
        y = 1;
        // cout << l << endl;
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <vector>

using namespace std;

int a[10005];
vector<int> g[10005];
bool vis[10005];

void dfs(int x) {
  vis[x] = true;
  for (int i : g[x]) {
    if (!vis[i]) {
      dfs(i);
    }
  }
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cnt++;
      dfs(i);
    }
  }
  printf("%d\n", cnt - 1);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <vector>

using namespace std;

const int N = 1e5 + 5;
int a[N];
vector<int> g[N];
bool vis[N];

void dfs(int x) {
  vis[x] = true;
  for (int i : g[x]) {
    if (!vis[i]) {
      dfs(i);
    }
  }
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cnt++;
      dfs(i);
    }
  }
  printf("%d\n", cnt - 1);
  return 0;
}
Problem p02536 (C++)

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

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につける
  }
};

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

  UnionFind tree(N);

  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    tree.unite(x, y); // xの木とyの木を併合する
  }
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    int a = tree.root(i);
    mp[a]++;
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (mp[i] >= 1)
      ans++;
  }
  cout << ans - 1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

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につける
  }
};

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

  UnionFind tree(N);

  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    tree.unite(x, y); // xの木とyの木を併合する
  }
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    int a = tree.root(i);
    mp[a]++;
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (mp[i] >= 1)
      ans++;
  }
  cout << ans - 1 << endl;
  return 0;
}
Problem p02536 (C++)

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

vector<vector<int>> adj;
vector<bool> vis;

void dfs(int v, int p) {
  vis[v] = 1;

  for (auto x : adj[v]) {
    if (x != p)
      dfs(x, v);
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  adj = vector<vector<int>>(n);
  vis = vector<bool>(n, false);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  int ans = 0;

  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, -1);
      ans++;
    }
  }

  cout << ans - 1;
}

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

vector<vector<int>> adj;
vector<bool> vis;

void dfs(int v, int p) {
  vis[v] = 1;

  for (auto x : adj[v]) {
    if (x != p && !vis[x])
      dfs(x, v);
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  adj = vector<vector<int>>(n);
  vis = vector<bool>(n, false);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  int ans = 0;

  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, -1);
      ans++;
    }
  }

  cout << ans - 1;
}
Problem p02536 (C++)

Buggy submission (status: 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(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);
}

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(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);
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;

#define fbo find_by_order
#define ook order_of_key
#define fi first
#define se second
#define long long long
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set; mt19937_64
// rng(std::chrono::system_clock::now().time_since_epoch().count());

// /mnt/c/Users/vince/Desktop/POST-IOI/

int par[100003];
int parent(int u) { return par[u] = (u == par[u]) ? u : parent(par[u]); }
int gab(int u, int v) { par[parent(u)] = parent(v); }

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // freopen("input.in", "r", stdin);

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

  while (m--) {
    int u, v;
    scanf("%d %d", &u, &v);
    if (parent(u) == parent(v))
      continue;
    gab(u, v);
  }
  set<int> st;
  for (int i = 1; i <= n; i++)
    st.insert(parent(i));
  printf("%lu\n", st.size() - 1);
}

Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;

#define fbo find_by_order
#define ook order_of_key
#define fi first
#define se second
#define long long long
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set; mt19937_64
// rng(std::chrono::system_clock::now().time_since_epoch().count());

// /mnt/c/Users/vince/Desktop/POST-IOI/

int par[100003];
int parent(int u) { return par[u] = (u == par[u]) ? u : parent(par[u]); }
void gab(int u, int v) { par[parent(u)] = parent(v); }

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // freopen("input.in", "r", stdin);

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

  while (m--) {
    int u, v;
    scanf("%d %d", &u, &v);
    if (parent(u) == parent(v))
      continue;
    gab(u, v);
  }
  set<int> st;
  for (int i = 1; i <= n; i++)
    st.insert(parent(i));
  printf("%lu\n", st.size() - 1);
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

struct UnionFind {
  vector<int> Parent;
  UnionFind(int n) { Parent = vector<int>(n, -1); }

  int root(int a) {
    if (Parent[a] < 0)
      return a;
    return root(Parent[a]);
  }

  int size(int a) { return -Parent[root(a)]; }

  bool connect(int a, int b) {
    if (root(a) == root(b))
      return false;
    if (size(a) < size(b))
      swap(a, b);
    Parent[root(a)] -= size(b);
    Parent[root(b)] = root(a);
    return true;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind field(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    field.connect(a, b);
  }
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (field.Parent[i] < 0) {
      cnt++;
    }
  }
  if (cnt == 0)
    cout << 0 << endl;
  else
    cout << cnt - 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

struct UnionFind {
  vector<int> Parent;
  UnionFind(int n) { Parent = vector<int>(n, -1); }

  int root(int a) {
    if (Parent[a] < 0)
      return a;
    return root(Parent[a]);
  }

  int size(int a) { return -Parent[root(a)]; }

  bool connect(int a, int b) {
    if (root(a) == root(b))
      return false;
    if (size(a) < size(b))
      swap(a, b);
    Parent[root(a)] -= size(b);
    Parent[root(b)] = root(a);
    return true;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind field(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    field.connect(a, b);
  }
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (field.Parent[i] < 0) {
      cnt++;
    }
  }
  if (cnt == 0)
    cout << 0 << endl;
  else
    cout << cnt - 1 << endl;
}
Problem p02536 (C++)

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

const int N = 100010;
int n, m, cnt, father[N];

int find(int x) { return x == father[x] ? x : father[x] = find(father[x]); }

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    father[i] = i;
  for (int i = 1, x, y; i <= m; i++) {
    scanf("%d%d", &x, &y);
    father[find(x)] = y;
  }
  for (int i = 1; i <= n; i++)
    if (find(i) == i)
      cnt++;
  printf("%d\n", cnt - 1);
  return 0;
}

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

const int N = 100010;
int n, m, cnt, father[N];

int find(int x) { return x == father[x] ? x : father[x] = find(father[x]); }

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    father[i] = i;
  for (int i = 1, x, y; i <= m; i++) {
    scanf("%d%d", &x, &y);
    father[find(x)] = find(y);
  }
  for (int i = 1; i <= n; i++)
    if (find(i) == i)
      cnt++;
  printf("%d\n", cnt - 1);
  return 0;
}
Problem p02536 (C++)

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

using namespace std;

vector<int> cities[100000];
bool visited[100000];

void dfs(int i) {
  visited[i] = true;
  for (int x : cities[i]) {
    if (not(visited[x])) {
      dfs(x);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    cities[a].push_back(b);
    cities[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (not(visited[i])) {
      ans++;
      dfs(i);
    }
  }
  cout << ans - 1;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;

using namespace std;

vector<int> cities[100005];
bool visited[100005];

void dfs(int i) {
  visited[i] = true;
  for (int x : cities[i]) {
    if (not(visited[x])) {
      dfs(x);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    cities[a].push_back(b);
    cities[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (not(visited[i])) {
      ans++;
      dfs(i);
    }
  }
  cout << ans - 1;
}
Problem p02536 (C++)

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

int cities, roads, a, b, group;
vector<int> adj[10100];
bool visited[100100];

void dfs(int c) {
  if (!visited[c]) {
    visited[c] = true;
    for (int i = 0; i < adj[c].size(); i++) {
      int d = adj[c][i];
      dfs(d);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> cities >> roads;
  for (int i = 1; i <= roads; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= cities; i++) {
    if (!visited[i])
      group++;
    dfs(i);
  }
  cout << group - 1 << endl;
}


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

int cities, roads, a, b, group;
vector<int> adj[100100];
bool visited[100100];

void dfs(int c) {
  if (!visited[c]) {
    visited[c] = true;
    for (int i = 0; i < adj[c].size(); i++) {
      int d = adj[c][i];
      dfs(d);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> cities >> roads;
  for (int i = 1; i <= roads; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= cities; i++) {
    if (!visited[i])
      group++;
    dfs(i);
  }
  cout << group - 1 << endl;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b, blocks;
vector<int> toc;
vector<vector<int>> rd;
bool used[100009];
void DFS(int i) {
  used[i] = 1;
  for (int j = 0; j < toc[i]; j++) {
    DFS(rd[i][j]);
  }
}
int main() {
  cin >> n >> m;
  rd.resize(n);
  toc.resize(n);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    rd[a - 1].push_back(b - 1);
    rd[b - 1].push_back(a - 1);
    toc[a - 1]++;
    toc[b - 1]++;
  }
  for (int i = 0; i < n; i++) {
    if (used[i])
      continue;
    blocks++;
    DFS(i);
  }
  cout << blocks - 1;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b, blocks;
vector<int> toc;
vector<vector<int>> rd;
bool used[100009];
void DFS(int i) {
  used[i] = 1;
  for (int j = 0; j < toc[i]; j++) {
    if (used[rd[i][j]])
      continue;
    DFS(rd[i][j]);
  }
}
int main() {
  cin >> n >> m;
  rd.resize(n);
  toc.resize(n);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    rd[a - 1].push_back(b - 1);
    rd[b - 1].push_back(a - 1);
    toc[a - 1]++;
    toc[b - 1]++;
  }
  for (int i = 0; i < n; i++) {
    if (used[i])
      continue;
    blocks++;
    DFS(i);
  }
  cout << blocks - 1;
  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> vi(n, 0);
  vector<vector<int>> v(n, vector<int>(n, -1));
  map<int, vector<int>> mp;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    mp[x].push_back(y);
    mp[y].push_back(x);
  }

  int cnt1 = 1;
  for (int i = 0; i < n; i++) {
    if (vi[i] != 0)
      continue;
    cnt1++;
    queue<int> q;
    q.push(i + 1);

    while (!q.empty()) {
      int size = q.size();
      for (int j = 0; j < size; j++) {
        int el = q.front();
        q.pop();
        vi[el - 1] = cnt1;
        for (int ii = 0; ii < mp[el].size(); ii++) {
          if (vi[mp[el][ii] - 1] == 0)
            q.push(mp[el][ii]);
        }
      }
    }
  }
  set<int> st;
  for (auto el : vi)
    st.insert(el);
  cout << st.size() - 1 << "\n";
  return 0;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> vi(n, 0);
  map<int, vector<int>> mp;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    mp[x].push_back(y);
    mp[y].push_back(x);
  }

  int cnt1 = 1;
  for (int i = 0; i < n; i++) {
    if (vi[i] != 0)
      continue;
    cnt1++;
    queue<int> q;
    q.push(i + 1);

    while (!q.empty()) {
      int size = q.size();
      for (int j = 0; j < size; j++) {
        int el = q.front();
        q.pop();
        vi[el - 1] = cnt1;
        for (int ii = 0; ii < mp[el].size(); ii++) {
          if (vi[mp[el][ii] - 1] == 0)
            q.push(mp[el][ii]);
        }
      }
    }
  }
  set<int> st;
  for (auto el : vi)
    st.insert(el);
  cout << st.size() - 1 << "\n";
  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> rec;
int find(int u) { return u == rec[u] ? u : rec[u] = find(rec[u]); }
int main() {
  int n, m;
  cin >> n >> m;
  rec.resize(n);
  for (int i = 0; i < n; ++i) {
    rec[i] = i;
  }
  int c = n - 1;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    int _u = find(u), _v = find(v);
    if (_u != _v) {
      rec[_v] = _u;
      --c;
    }
  }
  cout << c << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> rec;
int find(int u) { return u == rec[u] ? u : rec[u] = find(rec[u]); }
int main() {
  int n, m;
  cin >> n >> m;
  rec.resize(n);
  for (int i = 0; i < n; ++i) {
    rec[i] = i;
  }
  int c = n - 1;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    int _u = find(u), _v = find(v);
    if (_u != _v) {
      rec[_v] = _u;
      --c;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02536 (C++)

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

#define rep(i, n) for (ll i = 0; i < n; i++)
class UnionFind {
  vector<int> par;
  vector<int> siz;

public:
  UnionFind(int n) : par(n), siz(n, 1) {
    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]);
    }
  }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return 0;
    }
    if (siz[x] == siz[y]) {
      swap(x, y);
    }
    siz[x] += siz[y];
    par[y] = x;
    return 1;
  }
  int size(int x) { return siz[root(x)]; }
};
int main() {
  int n, q, a, b;
  cin >> n >> q;
  int ans = n - 1;
  UnionFind uf(n);
  rep(i, q) {
    cin >> a >> b;

    if (uf.unite(a, b)) {
      ans--;
    }
  }
  cout << ans << endl;
}


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

#define rep(i, n) for (ll i = 0; i < n; i++)
class UnionFind {
  vector<int> par;
  vector<int> siz;

public:
  UnionFind(int n) : par(n), siz(n, 1) {
    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]);
    }
  }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return 0;
    }
    if (siz[x] == siz[y]) {
      swap(x, y);
    }
    siz[x] += siz[y];
    par[y] = x;
    return 1;
  }
  int size(int x) { return siz[root(x)]; }
};
int main() {
  int n, q, a, b;
  cin >> n >> q;
  int ans = n - 1;
  UnionFind uf(n);
  rep(i, q) {
    cin >> a >> b;
    a--;
    b--;
    if (uf.unite(a, b)) {
      ans--;
    }
  }
  cout << ans << endl;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
// In the name of god
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define use_file                                                               \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
using namespace std;
using ll = long long;
const ll N = 1e5, inf = 4e18, mod = 1e9 + 7;
ll n, m, ans;
vector<ll> adj[N];
bool mark[N];
void dfs(ll u) {
  mark[u] = 1;
  for (int j : adj[u]) {
    if (!mark[j])
      dfs(j);
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << ans - 1;
  cout << endl;
}


Fixed submission:
// In the name of god
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define use_file                                                               \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
using namespace std;
using ll = long long;
const ll N = 1e5 + 5, inf = 4e18, mod = 1e9 + 7;
ll n, m, ans;
vector<ll> adj[N];
bool mark[N];
void dfs(ll u) {
  mark[u] = 1;
  for (int j : adj[u]) {
    if (!mark[j])
      dfs(j);
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << ans - 1;
  cout << endl;
}