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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

class UnionFind {
  std::vector<int> r, rank;

public:
  UnionFind(int N) {
    for (int i = 0; i < N; i++) {
      r.push_back(i);
      rank.push_back(0);
    }
  }
  int root(int x) {
    if (r[x] == x) {
      return x;
    }
    return r[x] = root(r[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    }
    if (rank[x] < rank[y]) {
      r[x] = y;
    } else {
      r[y] = x;
      if (rank[x] == rank[y]) {
        rank[x] += 1;
      }
    }
    return true;
  }
  int show() {
    std::set<int> jud;
    int co = 0;
    for (int i : r) {
      auto iter = jud.find(root(i));
      if (iter == jud.end()) {
        co += 1;
        jud.insert(root(i));
      }
    }
    return co;
  }
};

int main() {
  int n, m;
  std::cin >> n >> m;
  UnionFind UF(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    UF.unite(a, b);
  }
  std::cout << UF.show() - 1 << std::endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

class UnionFind {
  std::vector<int> r;

public:
  UnionFind(int N) {
    for (int i = -1; i > -N - 1; i--) {
      r.push_back(i);
    }
  }
  int root(int x) {
    if (r[x] < 0) {
      return x;
    }
    return r[x] = root(r[x]);
  }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    }
    if (r[x] > r[y]) {
      std::swap(x, y);
    }
    r[x] += r[y];
    r[y] = x;
    return true;
  }
  int show() {
    std::set<int> jud;
    int co = 0;
    for (int i : r) {
      auto iter = jud.find(i);
      if (iter == jud.end()) {
        co += 1;
        jud.insert(i);
      }
    }
    return co;
  }
};

int main() {
  int n, m;
  std::cin >> n >> m;
  UnionFind UF(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    UF.unite(a, b);
  }
  std::cout << UF.show() - 1 << std::endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
vector<int> G[N];
int vis[N];
void dfs(int u) {
  vis[u] = 1;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (vis[v] == 0)
      dfs(v);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      dfs(i);
      cnt++;
    }
  }
  cout << cnt - 1 << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long int p[10001];
int main() {
  int m, n, a, b, k = 0, ma = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &a, &b);
    p[a]++;
    p[b]++;
  }
  for (int i = 1; i <= n; i++)
    if (p[i] > ma)
      ma = p[i];
  k = n - ma - 1;
  printf("%d", k);
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

map<int, int> ds; // 上下级关系
// map<int, int> ranks;

int FindSup(int root) {
  if (ds[root] == root)
    return root;
  ds[root] = FindSup(ds[root]);
  return ds[root];
}

void Union(int root1, int root2) {
  int x = FindSup(root1);
  int y = FindSup(root2);
  if (x == y)
    return;
  if (x > y)
    ds[y] = x;
  else
    ds[x] = y;
  /*else
  {
          ds[x] = y;
          ranks[y]++;
  }*/
}

int main() {
  int N, M, cnt = 0;
  int x, y;
  scanf("%d %d", &N, &M);

  for (int i = 1; i <= N; i++) {
    ds[i] = i;
    // ranks[i] = 0;
  }

  for (int i = 0; i < M; i++) {
    scanf("%d %d", &x, &y);
    if (FindSup(x) != FindSup(y)) {
      Union(x, y);
      cnt++;
    }
  }
  cnt = N - 1 - cnt;
  if (cnt < 0)
    cnt = 0;
  printf("%d", cnt);

  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

map<int, int> ds; // 上下级关系
map<int, int> ranks;

int FindSup(int root) {
  if (ds[root] == root)
    return root;
  ds[root] = FindSup(ds[root]);
}

void Union(int root1, int root2) {
  int x = FindSup(root1);
  int y = FindSup(root2);
  if (x == y)
    return;
  if (ranks[x] > ranks[y])
    ds[y] = x;
  else if (ranks[x] < ranks[y])
    ds[x] = y;
  else {
    ds[x] = y;
    ranks[y]++;
  }
}

int main() {
  int N, M, cnt = 0;
  int x, y;
  scanf("%d %d", &N, &M);

  for (int i = 1; i <= N; i++) {
    ds[i] = i;
    ranks[i] = 0;
  }

  for (int i = 0; i < M; i++) {
    scanf("%d %d", &x, &y);
    if (FindSup(x) != FindSup(y)) {
      Union(x, y);
      cnt++;
    }
  }
  cnt = N - 1 - cnt;
  if (cnt < 0)
    cnt = 0;
  printf("%d", cnt);

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

#include <bits/stdc++.h>

using namespace std;

map<int, int> ds;
map<int, int> ranks;

int find_root(int x) { return x == ds[x] ? x : ds[x] = find_root(ds[x]); }

void union_set(int x, int y) {
  int x_root = find_root(x);
  int y_root = find_root(y);
  if (x_root != y_root) {
    if (ranks[x_root] > ranks[y_root]) {
      ds[y_root] = x_root;
    } else if (ranks[x_root] < ranks[y_root]) {
      ds[x_root] = y_root;
    } else {
      ds[x_root] = y_root;
      ranks[y_root]++;
    }
  }
}

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

  // 初始化
  for (int i = 1; i <= n; i++) {
    ds[i] = i;
    ranks[i] = 0;
  }

  int cnt = 0;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;

    x = find_root(x);
    y = find_root(y);
    if (x == y) {
      continue;
    }

    union_set(x, y);
    cnt++;
  }

  cnt = n - 1 - cnt;
  if (cnt < 0) {
    cnt = 0;
  }
  cout << cnt << "\\n";

  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

map<int, int> ds; // 上下级关系

int FindSup(int root) {
  if (ds[root] == root)
    return root;
  ds[root] = FindSup(ds[root]);
}

void Union(int root1, int root2) {
  int x = FindSup(root1);
  int y = FindSup(root2);
  if (x == y)
    return;
  if (x > y)
    ds[y] = x;
  else
    ds[x] = y;
}

int main() {
  int N, M, cnt = 0;
  int x, y;
  scanf("%d %d", &N, &M);

  for (int i = 1; i <= N; i++)
    ds[i] = i;

  for (int i = 0; i < M; i++) {
    scanf("%d %d", &x, &y);
    if (FindSup(x) != FindSup(y)) {
      Union(x, y);
      cnt++;
    }
  }
  cnt = N - 1 - cnt;
  if (cnt < 0)
    cnt = 0;
  printf("%d", cnt);

  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: 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)] = 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);
  }
}

Error: WA

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)] = 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, verdict: 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] - 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;
}

Error: 0

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], 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;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int visited[100001];
vector<int> p[100001];
int num = 0;
void dfs(int curr, int prev) {
  if (visited[curr] == 2) {
    return;
  }
  if (visited[curr] == 1) {
    num++;
    return;
  }
  visited[curr] = 1;
  for (int i = 0; i < p[curr].size(); i++) {
    if (p[curr][i] != prev) {
      dfs(p[curr][i], curr);
    }
  }
  visited[curr] = 2;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int s, e;
    cin >> s >> e;
    p[s].push_back(e);
    p[e].push_back(s);
  }
  for (int i = 1; i <= n; i++) {
    if (visited[i] != 2) {
      dfs(i, 0);
    }
  }
  cout << max((n - 1) - (m - num), 0) << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int visited[100001];
vector<int> p[100001];
int num = 0;
void dfs(int curr, int prev) {
  if (visited[curr] == 1) {
    num++;
    return;
  }
  visited[curr] = 1;
  for (int i = 0; i < p[curr].size(); i++) {
    if (p[curr][i] != prev) {
      dfs(p[curr][i], curr);
    }
  }
  visited[curr] = 2;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int s, e;
    cin >> s >> e;
    p[s].push_back(e);
    p[e].push_back(s);
  }
  for (int i = 1; i <= n; i++) {
    if (visited[i] != 2) {
      dfs(i, 0);
    }
  }
  cout << (n - 1) - m - num << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

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() {
  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 - 1, y - 1); // xの木とyの木を併合する
  }

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (!tree.same(0, i)) {
      tree.unite(0, i);
      cnt++;
    }
  }

  cout << cnt << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> AB(N);
  vector<int> bucket(N, 0);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    AB.at(i) = make_pair(x, y);
    bucket.at(x - 1)++;
    bucket.at(y - 1)++;
  }
  vector<int> big_city;
  for (int i = 0; i < N; i++) {
    if (bucket.at(i) > 1) {
      big_city.push_back(i);
    }
  }
  for (int i = 0; big_city.size(); i++) {
    for (int j = 0; j < M; j++) {
      if (AB.at(j).first == big_city.at(i)) {
        bucket.at(AB.at(j).second - 1) = -1;
      } else if (AB.at(j).second == big_city.at(i)) {
        bucket.at(AB.at(j).first - 1) = -1;
      }
    }
  }

  int cnt0 = 0, cnt1 = 0, cntbig = 0;
  for (int i = 0; i < N; i++) {
    if (bucket.at(i) == 0)
      cnt0++;
    else if (bucket.at(i) == 1)
      cnt1++;
    else if (bucket.at(i) > 1)
      cntbig++;
  }

  cout << cnt0 + cnt1 / 2 + cntbig - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: 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);
  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;
}

Error: 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);
  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;
}
Problem p02536 (C++) — buggy submission, verdict: 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]); }
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);
}

Error: 0

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]); }
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);
}
Problem p02536 (C++) — buggy submission, verdict: 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;
    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;
}

Error: 0

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;
    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, verdict: 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;
    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;
}

Error: 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;
    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, verdict: Runtime Error

#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;
}

Error: WA

Fixed submission:

#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;
}