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

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

void dfs(int node, vector<vector<int>> arr, char c[]) {
  if (c[node] == 'g') {
    return;
  }
  c[node] = 'g';
  for (int i = 0; i < arr[node].size(); i++) {
    if (c[arr[node][i]] == 'w') {
      dfs(arr[node][i], arr, c);
    }
  }
}

int main() {
  /*
   #ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
//for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
*/

  int n, m;
  cin >> n >> m;
  vector<vector<int>> arr(n + 1);
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    arr[a].push_back(b);
    arr[b].push_back(a);
  }
  char *c = new char[n + 1];
  for (int i = 0; i <= n; i++) {
    c[i] = 'w';
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i] == 'w') {

      dfs(i, arr, c);
      if (i == 1) {
        continue;
      }
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}

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

void dfs(int node, vector<vector<int>> &arr, char c[]) {
  if (c[node] == 'g') {
    return;
  }
  c[node] = 'g';
  for (int i = 0; i < arr[node].size(); i++) {
    if (c[arr[node][i]] == 'w') {
      dfs(arr[node][i], arr, c);
    }
  }
}

int main() {
  /*
   #ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
//for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
*/

  int n, m;
  cin >> n >> m;
  vector<vector<int>> arr(n + 1);
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    arr[a].push_back(b);
    arr[b].push_back(a);
  }
  char *c = new char[n + 1];
  for (int i = 0; i <= n; i++) {
    c[i] = 'w';
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i] == 'w') {

      dfs(i, arr, c);
      if (i == 1) {
        continue;
      }
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define vv(T) vector<vector<T>>
#define coa                                                                    \
  cout << ans << endl;                                                         \
  return 0;
using namespace std;
using ll = long long;
template <typename T> using vec = vector<T>;
template <typename T> using vvec = vector<vector<T>>;
using qint = queue<int>;
using sint = stack<int>;
using pii = pair<int, int>;

class UnionFind {
  vector<int> parent;
  // UnionFind木の実装
public:
  UnionFind(int N) : parent(N, -1) {} // コンストラクタ
  int root(int x) {
    // 根を求める。
    if (parent[x] < 0)
      return x;
    return parent[x] = root(parent[x]);
  }
  void unite(int x, int y) {
    // xとyの木を結合する。
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (-parent[rx] > -parent[ry]) {
      // 小さい方の木を大きい方につなげる。
      parent[rx] += parent[ry];
      parent[ry] = rx;
    } else {
      parent[ry] += parent[rx];
      parent[rx] = ry;
    }
    return;
  }
  bool isSame(int x, int y) {
    // 同じ木にあるか?
    return root(x) == root(y);
  }
  int size(int x) {
    // xの木に属する要素はいくつか?
    return -parent[root(x)];
  }
};

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

  UnionFind uf(n);

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }

  int ans = -1;
  rep(i, n) {
    int ri = uf.root(i);
    if (ri == i)
      ans++;
  }

  coa;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define vv(T) vector<vector<T>>
#define coa                                                                    \
  cout << ans << endl;                                                         \
  return 0;
using namespace std;
using ll = long long;
template <typename T> using vec = vector<T>;
template <typename T> using vvec = vector<vector<T>>;
using qint = queue<int>;
using sint = stack<int>;
using pii = pair<int, int>;

class UnionFind {
  vector<int> parent;
  // UnionFind木の実装
public:
  UnionFind(int N) : parent(N, -1) {} // コンストラクタ
  int root(int x) {
    // 根を求める。
    if (parent[x] < 0)
      return x;
    return parent[x] = root(parent[x]);
  }
  void unite(int x, int y) {
    // xとyの木を結合する。
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (-parent[rx] > -parent[ry]) {
      // 小さい方の木を大きい方につなげる。
      parent[rx] += parent[ry];
      parent[ry] = rx;
    } else {
      parent[ry] += parent[rx];
      parent[rx] = ry;
    }
    return;
  }
  bool isSame(int x, int y) {
    // 同じ木にあるか?
    return root(x) == root(y);
  }
  int size(int x) {
    // xの木に属する要素はいくつか?
    return -parent[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.unite(a, b);
  }

  int ans = -1;
  rep(i, n) {
    int ri = uf.root(i);
    if (ri == i)
      ans++;
  }

  coa;
}
Problem p02536 (C++)

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

struct UnionFind {
  vector<int> par;
  vector<int> rank;
  int num;
  UnionFind(int N) {
    num = N;
    rank.resize(N);
    par.resize(N);
    for (int i = 0; i < N; i++) {
      rank[i] = 0;
      par[i] = i;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] > rank[y]) {
      swap(x, y);
    }
    if (rank[x] == rank[y])
      rank[y]++;
    par[x] = y;
    num--;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int count() const { return num; }
};

int main() {
  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;
    uf.unite(a, b);
  }
  cout << uf.count() - 1 << endl;
}

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

struct UnionFind {
  vector<int> par;
  vector<int> rank;
  int num;
  UnionFind(int N) {
    num = N;
    rank.resize(N);
    par.resize(N);
    for (int i = 0; i < N; i++) {
      rank[i] = 0;
      par[i] = i;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] > rank[y]) {
      swap(x, y);
    }
    if (rank[x] == rank[y])
      rank[y]++;
    par[x] = y;
    num--;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int count() const { return num; }
};

int main() {
  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--;
    uf.unite(a, b);
  }
  cout << uf.count() - 1 << endl;
}
Problem p02536 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <string>
using namespace std;

int par[100000]; // 添え字として0~99999を許容する

int root(int i) {
  if (par[i] == i) {
    return i;
  }
  return root(par[i]);
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    par[i] = i;
  }
  for (int i = 0; i < m; ++i) {
    int k1, k2;
    cin >> k1 >> k2;
    k1--;
    k2--;
    par[root(k1)] = root(k2);
  }
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (root(i) == i) {
      cnt++;
    }
  }
  cout << (cnt - 1);
}

Fixed submission:
#include <iostream>
#include <map>
#include <string>
using namespace std;

int par[100000]; // 添え字として0~99999を許容する

int root(int i) {
  if (par[i] == i) {
    return i;
  }
  return par[i] = root(par[i]);
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    par[i] = i;
  }
  for (int i = 0; i < m; ++i) {
    int k1, k2;
    cin >> k1 >> k2;
    k1--;
    k2--;
    par[root(k1)] = root(k2);
  }
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (root(i) == i) {
      cnt++;
    }
  }
  cout << (cnt - 1);
}
Problem p02536 (C++)

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

vector<int> graph[1000005];
int color[1000005];

int dfs(int v) {
  color[v] = 1;
  for (auto i : graph[v]) {
    if (color[i] == 0)
      dfs(i);
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  dfs(1);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (color[i] == 0) {
      ans++;
      dfs(i);
    }
  }
  cout << ans << endl;
}


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

vector<int> graph[1000005];
int color[1000005];

int dfs(int v) {
  color[v] = 1;
  for (auto i : graph[v]) {
    if (color[i] == 0)
      dfs(i);
  }
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  dfs(1);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (color[i] == 0) {
      ans++;
      dfs(i);
    }
  }
  cout << ans << endl;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long int;
vector<int> adj[30002];
bool v[30002];
int tam;

void dfs(int i) {
  if (v[i])
    return;
  v[i] = true;
  tam++;
  for (int j = 0; j < adj[i].size(); j++) {
    dfs(adj[i][j]);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  set<int> node;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
    node.insert(a);
    node.insert(b);
  }
  tam = 0;
  int grafos = 0;
  for (int i = 1; i <= n; i++) {
    if (!v[i] && node.count(i)) {
      dfs(i);
      grafos++;
    }
  }
  // cout<<grafos<<" "<<tam<<endl;
  int ans = 0;
  ans += (grafos - 1);
  ans += (n - tam);
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long int;
vector<int> adj[100002];
bool v[100002];
int tam;

void dfs(int i) {
  if (v[i])
    return;
  v[i] = true;
  tam++;
  for (int j = 0; j < adj[i].size(); j++) {
    dfs(adj[i][j]);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  set<int> node;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
    node.insert(a);
    node.insert(b);
  }
  tam = 0;
  int grafos = 0;
  for (int i = 1; i <= n; i++) {
    if (!v[i] && node.count(i)) {
      dfs(i);
      grafos++;
    }
  }
  // cout<<grafos<<" "<<tam<<endl;
  int ans = 0;
  ans += (grafos - 1);
  ans += (n - tam);
  cout << ans;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
int modP(int a, int b) {
  if (a == 1)
    return 1;
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    a *= a;
    a = a % mod;
    return modP(a, b / 2) % mod;
  }

  return a * modP(a, b - 1) % mod;
}
int add(int a, int b) { return (a + b) % mod; }
int inv(int a, int b) { return a * modP(b, mod - 2) % mod; }
int fact[(int)1e5];
int fac(int n) {
  if (n <= 1)
    return fact[n] = 1;
  if (fact[n] != 0)
    return fact[n];
  return fact[n] = n * fac(n - 1);
}
int Cnk(int n, int k) { return fac(n) / fac(k) / fac(n - k); }
vector<int> g[(int)2e5];
void dfs(int s, vector<bool> &vis, int p) {
  vis[s] = true;
  for (int i : g[s]) {
    if (i == p)
      continue;
    dfs(i, vis, s);
  }
}
signed main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int cnt = 0;
  vector<bool> vis(n, false);
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, vis, -1);
      cnt++;
    }
  }
  cout << cnt - 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
int modP(int a, int b) {
  if (a == 1)
    return 1;
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    a *= a;
    a = a % mod;
    return modP(a, b / 2) % mod;
  }

  return a * modP(a, b - 1) % mod;
}
int add(int a, int b) { return (a + b) % mod; }
int inv(int a, int b) { return a * modP(b, mod - 2) % mod; }
int fact[(int)1e5];
int fac(int n) {
  if (n <= 1)
    return fact[n] = 1;
  if (fact[n] != 0)
    return fact[n];
  return fact[n] = n * fac(n - 1);
}
int Cnk(int n, int k) { return fac(n) / fac(k) / fac(n - k); }
vector<int> g[(int)2e5];
void dfs(int s, vector<bool> &vis, int p) {
  vis[s] = true;
  for (int i : g[s]) {
    if (i == p || vis[i])
      continue;
    dfs(i, vis, s);
  }
}
signed main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int cnt = 0;
  vector<bool> vis(n, false);
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, vis, -1);
      cnt++;
    }
  }
  cout << cnt - 1 << endl;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
using namespace std;

const int maxn = 4e6 + 10;
vector<int> G[maxn];
bool vis[maxn];
void dfs(int u, int fa) {
  vis[u] = true;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (v != fa) {
      dfs(v, u);
    }
  }
}
void solved() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i, -1), ans++;
  }
  cout << ans - 1 << endl;
}
int main() {
  solved();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
using namespace std;

const int maxn = 4e6 + 10;
vector<int> G[maxn];
bool vis[maxn];
void dfs(int u, int fa) {
  vis[u] = true;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (v != fa && !vis[v]) {
      dfs(v, u);
    }
  }
}
void solved() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i, -1), ans++;
  }
  cout << ans - 1 << endl;
}
int main() {
  solved();
  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
#define lli long long int
#define loop(i, x, n) for (int i = x; i < n; i++)
#define loopr(i, x, n) for (int i = n - 1; i >= x; i--)
#define st set<lli>
#define v vector<lli>
#define um unordered_map<lli, lli>
#define vpl vector<pair<lli, lli>>
#define llu long long unsigned
#define ms muliset<int, greater<int>>
#define ums unordered_map<string, lli>
#define pb push_back
#define time                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define test                                                                   \
  lli t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
v adj[100001];
lli vist[10001];

void dfs(lli node) {
  vist[node] = 1;
  for (lli child : adj[node])
    if (!vist[child])
      dfs(child);
}

int main() {
  time lli n, m;
  cin >> n >> m;
  loop(i, 0, m) {
    lli x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  lli c = -1;
  loop(i, 1, n + 1) {
    if (!vist[i]) {
      dfs(i);
      c++;
    }
  }
  cout << c << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
#define lli long long int
#define loop(i, x, n) for (int i = x; i < n; i++)
#define loopr(i, x, n) for (int i = n - 1; i >= x; i--)
#define st set<lli>
#define v vector<lli>
#define um unordered_map<lli, lli>
#define vpl vector<pair<lli, lli>>
#define llu long long unsigned
#define ms muliset<int, greater<int>>
#define ums unordered_map<string, lli>
#define pb push_back
#define time                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define test                                                                   \
  lli t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
v adj[100001];
lli vist[100001];

void dfs(lli node) {
  vist[node] = 1;
  for (lli child : adj[node])
    if (!vist[child])
      dfs(child);
}

int main() {
  time lli n, m;
  cin >> n >> m;
  loop(i, 0, m) {
    lli x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  lli c = -1;
  loop(i, 1, n + 1) {
    if (!vist[i]) {
      dfs(i);
      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;

struct UnionFind {
  // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの
  // そうでなければ親のid
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

  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])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  UnionFind UF(n);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    UF.unite(a[i], b[i]);
  }
  vector<int> root(n);
  for (int i = 0; i < n; i++) {
    root[UF.root(i)]++;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (root[i] != 0) {
      ans++;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}

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

struct UnionFind {
  // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの
  // そうでなければ親のid
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

  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])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  UnionFind UF(n);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    UF.unite(a[i], b[i]);
  }
  vector<int> root(n);
  for (int i = 0; i < n; i++) {
    root[UF.root(i)]++;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (root[i] != 0) {
      ans++;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mxn = 2e5 + 5;

class DisjointSet {
public:
  vector<int> parent;

  DisjointSet(int n) : parent(n) {
    for (int i = 0; i < n; i++) {
      parent[i] = i;
    }
  }

  void join(int a, int b) { parent[find(b)] = find(a); }

  int find(int a) { return a == parent[a] ? a : parent[a] = find(parent[a]); }

  bool check(int a, int b) { return find(a) == find(b); }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, n, x, t1, t2;
  cin >> n >> x;
  DisjointSet dsu(n);
  for (i = 0; i < x; i++) {
    cin >> t1 >> t2;
    dsu.join(t1, t2);
  }
  int ans = 0;
  int t = dsu.find(0);
  for (i = 0; i < n; i++) {
    int temp = dsu.find(i);
    if (temp != t) {
      ans++;
      dsu.join(0, i);
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mxn = 2e5 + 5;

class DisjointSet {
public:
  vector<int> parent;

  DisjointSet(int n) : parent(n) {
    for (int i = 0; i < n; i++) {
      parent[i] = i;
    }
  }

  void join(int a, int b) { parent[find(b)] = find(a); }

  int find(int a) { return a == parent[a] ? a : parent[a] = find(parent[a]); }

  bool check(int a, int b) { return find(a) == find(b); }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, n, x, t1, t2;
  cin >> n >> x;
  DisjointSet dsu(n);
  for (i = 0; i < x; i++) {
    cin >> t1 >> t2;
    t1--;
    t2--;
    dsu.join(t1, t2);
  }
  int ans = 0;
  int t = dsu.find(0);
  for (i = 0; i < n; i++) {
    int temp = dsu.find(i);
    if (temp != t) {
      ans++;
      dsu.join(0, i);
    }
  }
  cout << ans;
  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)

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);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    tree.unite(a, b);
  }
  int cnt = 0;
  rep(i, n) {
    if (tree.par[i] == i)
      cnt++;
  }
  cout << cnt - 1 << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)

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);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    tree.unite(a - 1, b - 1);
  }
  int cnt = 0;
  rep(i, n) {
    if (tree.par[i] == i)
      cnt++;
  }
  cout << cnt - 1 << endl;
}
Problem p02536 (C++)

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

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

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <algorithm> //next_permutation
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

const ll MOD = 1000000007;
const ll MAX = 2000001;

ll mod(ll a) { return a % MOD; }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

struct UnionFind {
  // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの
  // そうでなければ親のid
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

  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])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

int main() {
  ll n, m;
  cin >> n >> m;
  UnionFind UF(n);

  rep(i, m) {
    ll x, y;
    cin >> x >> y;

    UF.unite(x, y);
  }

  set<ll> parent;

  rep(i, n) { parent.insert(UF.root(i)); }

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


Fixed submission:
#define _GLIBCXX_DEBUG
#include <algorithm> //next_permutation
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

const ll MOD = 1000000007;
const ll MAX = 2000001;

ll mod(ll a) { return a % MOD; }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

struct UnionFind {
  // 自身が親であれば、その集合に属する頂点数に-1を掛けたもの
  // そうでなければ親のid
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N, -1); }

  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])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

int main() {
  ll n, m;
  cin >> n >> m;
  UnionFind UF(n);

  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;

    UF.unite(x, y);
  }

  set<ll> parent;

  rep(i, n) { parent.insert(UF.root(i)); }

  cout << parent.size() - 1 << endl;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cstdlib>
#include <numeric>
#define ll long long int
#define all(x) x.begin(), x.end()

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

#define v vector<long long int>
#define vv vector<int>
#define gcd(m, n) __gcd(m, n)
#define vp vector<pair<long long int, long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int, long long int>
#define f(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, n) for (ll i = a; i < n; i += 1)
#define fe(d, e) for (auto d : e)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll, ll>
#define N 100005LL
using namespace std;

vector<vector<int>> graph;
vector<bool> visited;

void dfs(int node) {
  visited[node] = true;
  for (auto u : graph[node])
    if (!visited[u])
      dfs(u);
}

int solve1() {
  int n, m;
  cin >> n >> m;
  graph.resize(n + 1);
  visited.resize(n + 1, false);
  f(i, m) {
    ll x, y;
    cin >> x >> y;
    graph[x].pb(y);
    graph[y].pb(x);
  }
  ll ans = 0;
  loop(i, 1, n + 1) {
    if (!visited[i]) {
      ans += 1;
      dfs(i);
    }
  }

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

int main() {
  fastio;
  ll t = 1;
  // cin>>t;
  while (t--)
    solve1();
}


Fixed submission:
#include <bits/stdc++.h>
#include <cstdlib>
#include <numeric>
#define ll long long int
#define all(x) x.begin(), x.end()

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

#define v vector<long long int>
#define vv vector<int>
#define gcd(m, n) __gcd(m, n)
#define vp vector<pair<long long int, long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int, long long int>
#define f(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, n) for (ll i = a; i < n; i += 1)
#define fe(d, e) for (auto d : e)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll, ll>
#define N 100005LL
using namespace std;

vector<vector<int>> graph;
vector<bool> visited;

void dfs(int node) {
  visited[node] = true;
  for (auto u : graph[node])
    if (!visited[u])
      dfs(u);
}

void solve1() {
  int n, m;
  cin >> n >> m;
  graph.resize(n + 1);
  visited.resize(n + 1, false);
  f(i, m) {
    ll x, y;
    cin >> x >> y;
    graph[x].pb(y);
    graph[y].pb(x);
  }
  ll ans = 0;
  loop(i, 1, n + 1) {
    if (!visited[i]) {
      ans += 1;
      dfs(i);
    }
  }

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

int main() {
  fastio;
  ll t = 1;
  // cin>>t;
  while (t--)
    solve1();
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
// luogu-judger-enable-o2
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int x, y;
int p[5000];
int find(int x) {
  if (p[x] != x)
    p[x] = find(p[x]);
  return p[x];
}
map<int, bool> mp;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    p[i] = i;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    p[find(x - 1)] = find(y - 1);
  }
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    if (!mp[find(i)]) {
      sum++;
      mp[find(i)] = true;
    }
  }
  cout << sum - 1 << endl;
  return 0;
}

Fixed submission:
// luogu-judger-enable-o2
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int x, y;
int p[100001];
int find(int x) {
  if (p[x] != x)
    p[x] = find(p[x]);
  return p[x];
}
map<int, bool> mp;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    p[i] = i;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    p[find(x - 1)] = find(y - 1);
  }
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    if (!mp[find(i)]) {
      sum++;
      mp[find(i)] = true;
    }
  }
  cout << sum - 1 << endl;
  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define ll long long
#include <set>
#include <vector>
// vector<ll>adj[100001];
ll parent[100001];
ll tao[100001];
ll find(ll i) {
  if (parent[i] == i)
    return i;
  else
    return parent[i] = find(parent[i]);
}
ll union_set(ll a, ll b) {
  ll x = find(a);
  ll y = find(b);
  if (tao[x] < tao[y])
    swap(x, y);
  parent[y] = x;
  if (tao[x] == tao[y])
    tao[x]++;
}
set<ll> s;
int main() {
  ll n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
    tao[i] = 1;
  }
  while (m--) {
    ll l, r;
    cin >> l >> r;
    union_set(l, r);
  }
  for (int i = 1; i <= n; i++)
    s.insert(find(i));
  cout << s.size() - 1 << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
#define ll long long
#include <set>
#include <vector>
// vector<ll>adj[100001];
ll parent[100001];
ll tao[100001];
ll find(ll i) {
  if (parent[i] == i)
    return i;
  else
    return parent[i] = find(parent[i]);
}
void union_set(ll a, ll b) {
  ll x = find(a);
  ll y = find(b);
  if (tao[x] < tao[y])
    swap(x, y);
  parent[y] = x;
  if (tao[x] == tao[y])
    tao[x]++;
}
set<ll> s;
int main() {
  ll n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
    tao[i] = 1;
  }
  while (m--) {
    ll l, r;
    cin >> l >> r;
    union_set(l, r);
  }
  for (int i = 1; i <= n; i++)
    s.insert(find(i));
  cout << s.size() - 1 << endl;

  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define int long long int
#define vec vector<int>
#define inf 1e18
using namespace std;
vector<int> adj[100001];
int vis[100001];
int dfs(int node) {
  vis[node] = 1;
  for (int ch : adj[node]) {
    if (!vis[ch])
      dfs(ch);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tt = 1;
  // cin>>tt;
  while (tt--) {
    int n, m, i;
    cin >> n >> m;
    for (i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      adj[u].pb(v);
      adj[v].pb(u);
    }
    int c = 0;
    for (i = 1; i <= n; i++) {
      if (!vis[i])
        dfs(i), c++;
    }
    cout << c - 1;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define int long long int
#define vec vector<int>
#define inf 1e18
using namespace std;
vector<int> adj[100001];
int vis[100001];
void dfs(int node) {
  vis[node] = 1;
  for (int ch : adj[node]) {
    if (!vis[ch])
      dfs(ch);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tt = 1;
  // cin>>tt;
  while (tt--) {
    int n, m, i;
    cin >> n >> m;
    for (i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      adj[u].pb(v);
      adj[v].pb(u);
    }
    int c = 0;
    for (i = 1; i <= n; i++) {
      if (!vis[i])
        dfs(i), c++;
    }
    cout << c - 1;
  }
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int mmax = 1e5;
using namespace std;
long long n, m, ans;
bool vis[mmax];
vector<int> adj[mmax];
void dfs(int u) {
  vis[u] = 1;
  for (int v : adj[u])
    if (!vis[v])
      dfs(v);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("test.inp", "r", stdin);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      dfs(i);
      ans++;
    }
  cout << --ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
const int mmax = 1e6;
using namespace std;
long long n, m, ans;
bool vis[mmax];
vector<int> adj[mmax];
void dfs(int u) {
  vis[u] = 1;
  for (int v : adj[u])
    if (!vis[v])
      dfs(v);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("test.inp", "r", stdin);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      dfs(i);
      ans++;
    }
  cout << --ans;
  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
/* -> Written by <-
   -----------
  |K_A_Z_A_M_A|
  |___________|
  |    _    |
  |   (^_^)   |
  |  /( | )\  |
  |____|_|____|
    H O A N G
*/

#define Task ""
#define F first
#define S second
#define push_back pb
#define bit(x, i) (x >> i) & 1
#define inf 1e9 + 7
#define INF 1e18 + 7
#define all(x) x.begin(), x.end()

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;

const int MOD = 998244353;
const double eps = 1e-6;
const int maxn = 1e5 + 5;

int n, m;
vector<int> a[maxn];
int par[maxn];

void dfs(int u) {
  for (auto v : a[u])
    if (v != par[u]) {
      par[v] = u;
      dfs(v);
    }
}

void Solve() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    a[u].pb(v);
    a[v].pb(u);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (par[i] == 0) {
      cnt++;
      dfs(i);
    }
  cout << cnt - 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  if (fopen(Task ".inp", "r")) {
    freopen(Task ".inp", "r", stdin);
    freopen(Task ".out", "w", stdout);
  }
  int test_case = 1;
  //    cin >> test_case;
  while (test_case--)
    Solve();
  return 0;
}


Fixed submission:
/* -> Written by <-
   -----------
  |K_A_Z_A_M_A|
  |___________|
  |    _    |
  |   (^_^)   |
  |  /( | )\  |
  |____|_|____|
    H O A N G
*/

#define Task ""
#define F first
#define S second
#define push_back pb
#define bit(x, i) (x >> i) & 1
#define inf 1e9 + 7
#define INF 1e18 + 7
#define all(x) x.begin(), x.end()

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;

const int MOD = 998244353;
const double eps = 1e-6;
const int maxn = 1e5 + 5;

int n, m;
vector<int> a[maxn];
int par[maxn];

void dfs(int u) {
  for (auto v : a[u])
    if (v != par[u]) {
      if (par[v])
        continue;
      par[v] = u;
      dfs(v);
    }
}

void Solve() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    a[u].pb(v);
    a[v].pb(u);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (par[i] == 0) {
      cnt++;
      dfs(i);
    }
  cout << cnt - 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  if (fopen(Task ".inp", "r")) {
    freopen(Task ".inp", "r", stdin);
    freopen(Task ".out", "w", stdout);
  }
  int test_case = 1;
  //    cin >> test_case;
  while (test_case--)
    Solve();
  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
//_nebula_
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
int t;
const int M = 1e9 + 7;
const int inf = INT64_MAX;
int max(int a, int b, int c) { return max(a, max(b, c)); }
int min(int a, int b, int c) { return min(a, min(b, c)); }
int fspow(int x, int y) {
  int ans;
  if (y == 0)
    return 1;
  ans = fspow(x, y / 2);
  if (y % 2 == 0)
    return ((ans * ans) % M);
  return (x * ((ans * ans) % M)) % M;
}
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(x % y, y);
}
int p2(int x) {
  int ans = 0;
  while (x > 1) {
    ans++;
    x /= 2;
  }
  return ans;
}
vector<int> grp[10005];
bool vis[100005];
void dfs(int s) {
  if (vis[s])
    return;
  vis[s] = 1;
  for (auto i : grp[s])
    dfs(i);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  t = 1;
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  //  cin>>t;
  // int tmp=t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      grp[u].push_back(v);
      grp[v].push_back(u);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (vis[i])
        continue;
      ans++;
      dfs(i);
    }
    cout << ans - 1 << "\n";
  }
}


Fixed submission:
//_nebula_
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
int t;
const int M = 1e9 + 7;
const int inf = INT64_MAX;
int max(int a, int b, int c) { return max(a, max(b, c)); }
int min(int a, int b, int c) { return min(a, min(b, c)); }
int fspow(int x, int y) {
  int ans;
  if (y == 0)
    return 1;
  ans = fspow(x, y / 2);
  if (y % 2 == 0)
    return ((ans * ans) % M);
  return (x * ((ans * ans) % M)) % M;
}
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(x % y, y);
}
int p2(int x) {
  int ans = 0;
  while (x > 1) {
    ans++;
    x /= 2;
  }
  return ans;
}
vector<int> grp[100005];
bool vis[100005];
void dfs(int s) {
  if (vis[s])
    return;
  vis[s] = 1;
  for (auto i : grp[s])
    dfs(i);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  t = 1;
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  //  cin>>t;
  // int tmp=t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      grp[u].push_back(v);
      grp[v].push_back(u);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (vis[i])
        continue;
      ans++;
      dfs(i);
    }
    cout << ans - 1 << "\n";
  }
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>
// #include "atcoder/all"

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

// doc:
// https://shibh308.github.io/library/library/lib/classes/unionfind.cpp.html
struct UnionFind {
  vector<int> par;
  int count;
  UnionFind(int n) : par(n, -1), count(0) {}
  int Find(int x) { return par[x] < 0 ? x : Find(par[x]); }
  int Size(int x) { return par[x] < 0 ? -par[x] : Size(par[x]); }
  bool Unite(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return ++count;
  }
};

void solve(long long n, long long m, std::vector<long long> a,
           std::vector<long long> b) {
  UnionFind uf(n);
  for (int i = 0; i < n; ++i)
    uf.Unite(a[i] - 1, b[i] - 1);
  cout << n - uf.count - 1 << endl;
}

signed main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, M, std::move(A), std::move(B));
  return 0;
}


Fixed submission:
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>
// #include "atcoder/all"

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

// doc:
// https://shibh308.github.io/library/library/lib/classes/unionfind.cpp.html
struct UnionFind {
  vector<int> par;
  int count;
  UnionFind(int n) : par(n, -1), count(0) {}
  int Find(int x) { return par[x] < 0 ? x : Find(par[x]); }
  int Size(int x) { return par[x] < 0 ? -par[x] : Size(par[x]); }
  bool Unite(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return ++count;
  }
};

void solve(long long n, long long m, std::vector<long long> a,
           std::vector<long long> b) {
  UnionFind uf(n);
  for (int i = 0; i < m; ++i)
    uf.Unite(a[i] - 1, b[i] - 1);
  cout << n - uf.count - 1 << endl;
}

signed main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> A(M);
  std::vector<long long> B(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
  }
  solve(N, M, std::move(A), std::move(B));
  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, M;
  cin >> N >> M;
  graph r(N + 1);
  rep(i, M) {
    int A, B;
    cin >> A >> B;

    r[A].push_back(B);

    r[B].push_back(A);
  }
  vector<bool> rooted(N + 1, false);
  int ans = 0;
  REP(i, N) {
    if (rooted[i] == false) {
      rooted[i] = true;
      queue<int> q;
      for (int &e : r[i]) {
        q.push(e);
      }
      while (!q.empty()) {
        int a = q.front();
        rooted[a] = true;
        q.pop();
        for (int &e : r[a]) {
          q.push(e);
        }
      }
      ans++;
    }
  }

  cout << ans - 1 << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, M;
  cin >> N >> M;
  graph r(N + 1);
  rep(i, M) {
    int A, B;
    cin >> A >> B;

    r[A].push_back(B);

    r[B].push_back(A);
  }
  vector<bool> rooted(N + 1, false);
  int ans = 0;
  REP(i, N) {
    if (rooted[i] == false) {
      rooted[i] = true;
      queue<int> q;
      for (int &e : r[i]) {
        q.push(e);
      }
      while (!q.empty()) {
        int a = q.front();
        rooted[a] = true;
        q.pop();
        for (int &e : r[a]) {
          if (rooted[e] == false)
            q.push(e);
        }
      }
      ans++;
    }
  }

  cout << ans - 1 << endl;
}