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

// atcoder
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
vector<int> v[100003];
bool used[100003];
void dfs(int root) {
  //	if(used[root]==true) return;
  used[root] = true;
  for (auto x : v[root]) {
    if (used[x] == false)
      dfs(x);
  }
  return;
}
int main() {
  int n, m, u, w;

  cin >> n >> m;
  while (m > 0) {
    cin >> u >> w;
    v[u].push_back(w);
    v[w].push_back(u);
    m--;
  }

  int road = 0;
  memset(used, false, sizeof(used));
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      dfs(i);
      road++;
    }
  }

  cout << road - 1;

  return 0;
}

Error: -11

Fixed submission:

// atcoder
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
vector<int> v[10003];
bool used[10003];
void dfs(int root) {
  // if(used[root]==true) return;
  //	used[root]=true;
  for (auto x : v[root]) {
    if (used[x] == false)
      dfs(x);
  }
  return;
}
int main() {
  int n, m, u, w;

  cin >> n >> m;
  while (m > 0) {
    cin >> u >> w;
    v[u].push_back(w);
    v[w].push_back(u);
    m--;
  }

  int road = 0;
  memset(used, false, sizeof(used));
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      dfs(i);
      road++;
    }
  }

  cout << road - 1;

  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma warning(disable : 4996)
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef tuple<ll, ll, ll> tl3;
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FORN(i, 1, n)
#define tc(t) while (t--)
// https://atcoder.jp/contests/abl/tasks/abl_c
vector<ll> v[101010];
bool isVisited[101010] = {
    0,
};
ll cnt = 0;
void dfs(ll x) {
  isVisited[x] = 1;
  for (auto k : v[x]) {
    if (isVisited[k] == 0) {
      dfs(k);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, m;
  cin >> n >> m;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  repn(i, n) {
    if (isVisited[i] == 0) {
      dfs(i);
      cnt++;
    }
  }
  cout << cnt - 1;

  return 0;
}


Error: TLE

Fixed submission:

#pragma warning(disable : 4996)
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef tuple<ll, ll, ll> tl3;
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FORN(i, 1, n)
#define tc(t) while (t--)
// https://atcoder.jp/contests/abl/tasks/abl_c
vector<ll> v[101010];
char isVisited[101010] = {
    0,
};
ll cnt = 1;
void bfs(ll x) {
  queue<ll> q;
  q.push(x);
  isVisited[x] = cnt;
  while (!q.empty()) {
    ll cur = q.front();
    q.pop();
    for (int i = 0; i < v[cur].size(); i++) {
      if (!isVisited[v[cur][i]]) {
        isVisited[v[cur][i]] = cnt;
        q.push(v[cur][i]);
      }
    }
  }
  cnt++;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, m;
  cin >> n >> m;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (!isVisited[i])
      bfs(i);
  }
  cout << cnt - 2;

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

#include <bits/stdc++.h>
const int mmax = 2e6;
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;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
const int mmax = 1e5;
using namespace std;
int 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, verdict: Runtime Error

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


Error: WA

Fixed submission:

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 20;
int fa[N];
int xfind(int x) { return fa[x] == x ? x : fa[x] = xfind(fa[x]); }
void into(int x, int y) {
  int xx = xfind(x);
  int yy = xfind(y);
  if (xx != yy)
    fa[xx] = yy;
}
int main() {

  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= N; i++)
    fa[i] = i;
  int x, y;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    into(x, y);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (fa[i] == i)
      cnt++;
  }
  cout << cnt - 1 << endl;
}


Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>
const int N = 1e5 + 10;
int f[N];
int n, m;

int getf(int v) {
  if (f[v] != v)
    f[v] = getf(f[v]);
  return f[v];
}
int merge(int v, int u) {
  int t1, t2;
  t1 = getf(v);
  t2 = getf(u);

  if (t1 != t2) {
    f[t2] = t1;
  }
}
int main() {
  int T, i, j, sum, a, b;

  sum = 0;
  scanf("%d %d", &n, &m);

  for (i = 1; i <= n; i++) {
    f[i] = i;
  }

  for (i = 1; i <= m; i++) {
    scanf("%d %d", &a, &b);
    merge(a, b);
  }
  for (i = 1; i <= n; i++) {
    if (f[i] == i) {
      sum++;
    }
  }
  printf("%d\\n", sum - 1);

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
const ll MOD = 1000000007;
struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }
  int root(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }
  bool issame(int x, int y) { return root(x) == root(y); }
  bool merge(int x, int y) {
    x = root(x), y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return -par[root(x)]; }
};
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  UnionFind tree(n);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    tree.merge(a, b);
  }
  int ans = 0;
  rep(i, n) {
    if (tree.par[i] < 0)
      ans++;
  }
  cout << ans - 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
const ll MOD = 1000000007;
struct UnionFind {
  vector<int> par;
  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }
  int root(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }
  bool issame(int x, int y) { return root(x) == root(y); }
  bool merge(int x, int y) {
    x = root(x), y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return -par[root(x)]; }
};
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  UnionFind tree(n);
  while (q--) {
    int a, b;
    a--, b--;
    scanf("%d%d", &a, &b);
    tree.merge(a, b);
  }
  int ans = 0;
  rep(i, n) {
    if (tree.par[i] < 0)
      ans++;
  }
  cout << ans - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: 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;
}


Error: TLE

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

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
struct UnionFind {
  vector<int> r;

  UnionFind(int N) { r = vector<int>(N + 1, -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;
  cin >> N;
  UnionFind UF(N);
  int M;
  cin >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    a;
    b;
    cin >> a >> b;
    UF.unite(a, b);
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    if (i == UF.root(i)) {
      ans++;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
struct UnionFind {
  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;
  cin >> N;
  UnionFind UF(N);
  int M;
  cin >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    a--;
    b--;
    cin >> a >> b;
    UF.unite(a, b);
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (i == UF.root(i)) {
      ans++;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: 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 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);
}

Error: TLE

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

#include <iostream>
#include <map>
#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 + 1);

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

  map<int, int> cnt;
  for (int i = 1; i <= N; i++) {
    cnt[tree.root(i)]++;
  }

  for (int i = 1; i <= N; i++) {
    cerr << tree.root(i) << endl;
  }

  cout << cnt.size() - 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <map>
#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 A, B;
    cin >> A >> B;
    tree.unite(A, B);
  }

  int ans{0};
  map<int, int> cnt;
  for (int i = 0; i < N; i++) {
    cnt[tree.par.at(i)]++;
  }

  cout << cnt.size() - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: 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;
    a--;
    b--;
    uf.unite(a, b);
  }
  cout << uf.count() - 1 << endl;
}

Error: 0

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;
    uf.unite(a, b);
  }
  cout << uf.count() - 1 << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int MOD = 1000000007;
const long long INF = 1LL << 60;
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;
}

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() {
  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.unite(A, B);
  }
  int res = 0;
  for (int i = 0; i < N; ++i) {
    if (uf.par[i] == i) {
      ++res;
    }
  }
  cout << res - 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int MOD = 1000000007;
const long long INF = 1LL << 60;
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;
}

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() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);
  for (int i = 0; i < M; ++i) {
    int A, B;
    --A, --B;
    cin >> A >> B;
    uf.unite(A, B);
  }
  int res = 0;
  for (int i = 0; i < N; ++i) {
    if (uf.par[i] == i) {
      ++res;
    }
  }
  cout << res - 1 << endl;

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

#include <bits/stdc++.h>

using namespace std;
int a, m, n, b, kt[100009], dem = 0;
vector<int> a1[100009];
void bfs(int a) {
  kt[a] = dem;
  for (int i : a1[a]) {
    if (kt[i] == 0)
      bfs(i);
  }
}
int main() {
  // freopen("tplt.inp","r",stdin);
  // freopen("tplt.out","w",stdout);
  cin >> n >> m;
  for (int i = 1; i <= 100009; i++)
    kt[i] = 0;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    a1[a].push_back(b);
    a1[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (kt[i] == 0) {
      dem++;
      bfs(i);
    }
  }
  cout << dem - 1;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int a, m, n, b, kt[100009] = {0}, dem = 0, a1[10009][10009] = {0};
void bfs(int a) {
  kt[a] = dem;
  for (int i = 1; i <= n; i++) {
    if (a1[a][i] == 1 && kt[i] == 0)
      bfs(i);
  }
}
int main() {
  // freopen("tplt.inp","r",stdin);
  // freopen("tplt.out","w",stdout);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    a1[a][b] = 1;
    a1[b][a] = 1;
  }
  for (int i = 1; i <= n; i++) {
    if (kt[i] == 0) {
      dem++;
      bfs(i);
    }
  }
  cout << dem - 1;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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


Error: 0

Fixed submission:

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