alexjercan/bugnet huggingface dataset:
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    uf.unite(a, b); // a & b has connection
  }
  int ans = 0;
  set<int> check;
  for (int id : uf.d) {
    if (id < 0)
      ++ans;
  }
  cout << ans - 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    uf.unite(a, b); // a & b has connection
  }
  set<int> checked;
  rep(j, n) {
    if (uf.same(0, j))
      checked.insert(j);
  }
  int ans = 0;
  for (int i = 1; i < n; ++i) {
    if (checked.count(i) == 0) {
      ++ans;
      rep(j, n) {
        if (uf.same(i, j))
          checked.insert(j);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: 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[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";
  }
}


Error: 0

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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
using Graph = vector<vector<ll>>;

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= b; i--)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

#define INF 1000000000000 // 10^12
#define MOD 1000000007    // 10^9+7:合同式の法

int main() {

  ll N, M;
  cin >> N >> M;

  Graph G(N);
  for (ll i = 0; i < M; ++i) {
    ll a, b;
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }

  // BFS のためのデータ構造
  vector<ll> dist(N, -1); // 全頂点を「未訪問」に初期化
  // queue<ll> que;
  ll total = 0;

  // BFS 開始 (キューが空になるまで探索を行う)
  REP(i, N) {
    if (dist[i] == -1) {
      dist[i] = total;
      // cout << dist[i] << endl;
      total++;
      queue<ll> que;
      que.push(i);
      while (!que.empty()) {
        ll v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (ll nv : G[v]) {
          if (dist[nv] != -1)
            continue; // すでに発見済みの頂点は探索しない

          // 新たな白色頂点 nv について距離情報を更新してキューに追加する
          dist[nv] = dist[v];
          que.push(nv);
        }
      }
    }
  }
  cout << total - 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
using Graph = vector<vector<ll>>;

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= b; i--)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

#define INF 1000000000000 // 10^12
#define MOD 1000000007    // 10^9+7:合同式の法

int main() {

  ll N, M;
  cin >> N >> M;

  Graph G(N);
  for (ll i = 0; i < M; ++i) {
    ll a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  // BFS のためのデータ構造
  vector<ll> dist(N, -1); // 全頂点を「未訪問」に初期化
  // queue<ll> que;
  ll total = 0;

  // BFS 開始 (キューが空になるまで探索を行う)
  REP(i, N) {
    if (dist[i] == -1) {
      dist[i] = total;
      // cout << dist[i] << endl;
      total++;
      queue<ll> que;
      que.push(i);
      while (!que.empty()) {
        ll v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (ll nv : G[v]) {
          if (dist[nv] != -1)
            continue; // すでに発見済みの頂点は探索しない

          // 新たな白色頂点 nv について距離情報を更新してキューに追加する
          dist[nv] = dist[v];
          que.push(nv);
        }
      }
    }
  }
  cout << total - 1 << endl;

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

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int par(int i, vector<int> &group) {
  if (i == group[i])
    return i;
  else {
    return group[i] = par(group[i], group);
  }
}

bool same(int i, int j, vector<int> &group) {
  if (par(i, group) == par(j, group)) {
    return true;
  } else {
    return false;
  }
}

void unite(int i, int j, vector<int> &group, vector<int> &depth) {
  int a = par(i, group);
  int b = par(j, group);
  if (a != b) {
    if (depth[i] == depth[j]) {
      depth[i]++;
      group[b] = a;
    } else if (depth[i] > depth[j]) {
      group[b] = a;
    } else {
      group[a] = b;
    }
  }
}

int main(void) {

  int N, M;
  cin >> N >> M;
  vector<int> Par(N);
  vector<int> depth(N, 0);
  for (int i = 0; i < N; i++) {
    Par[i] = i;
  }
  int a, b;
  int c = 0;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;

    if (!same(a - 1, b - 1, Par)) {
      unite(a - 1, b - 1, Par, depth);
      c++;
    }
  }
  // cout << N-1-c << endl;

  int count = 0;
  vector<int> counts(N, 0);
  for (int i = 0; i < N; i++) {
    int p = par(i, Par);
    if (counts[p] == 0) {
      counts[p]++;
      count++;
    }
  }
  cout << count - 1 << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int par(int i, vector<int> &group) {
  if (i == group[i])
    return i;
  else {
    return group[i] = par(group[i], group);
  }
}

bool same(int i, int j, vector<int> &group) {
  if (par(i, group) == par(j, group)) {
    return true;
  } else {
    return false;
  }
}

void unite(int i, int j, vector<int> &group, vector<int> &depth) {
  int a = par(i, group);
  int b = par(j, group);
  if (a != b) {
    if (depth[i] == depth[j]) {
      depth[i]++;
      depth[j] = depth[i];
      int p = j, p_g = group[j];
      while (true) {
        p = j;
        p_g = group[j];
        group[j] = a;
        if (p == p_g)
          break;
      }

    } else if (depth[i] > depth[j]) {
      depth[j] = depth[i];
      int p = j, p_g = group[j];
      while (true) {
        p = j;
        p_g = group[j];
        group[j] = a;
        if (p == p_g)
          break;
      }
    } else {
      depth[i] = depth[j];
      int p = i, p_g = group[i];
      while (true) {
        p = i;
        p_g = group[i];
        group[i] = b;
        if (p == p_g)
          break;
      }
    }
  }
}

int main(void) {

  int N, M;
  cin >> N >> M;
  vector<int> Par(N);
  vector<int> depth(N, 0);
  for (int i = 0; i < N; i++) {
    Par[i] = i;
  }
  int a, b;
  int c = 0;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;

    if (!same(a - 1, b - 1, Par)) {
      unite(a - 1, b - 1, Par, depth);
      c++;
    }
  }
  // cout << N-1-c << endl;

  int count = 0;
  vector<int> counts(N, 0);
  for (int i = 0; i < N; i++) {
    int p = par(i, Par);
    if (counts[p] == 0) {
      counts[p]++;
      count++;
    }
  }
  cout << count - 1 << endl;

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


Error: WA

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;
    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, 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() {
  long n, m;
  cin >> n >> m;
  long a[m], b[m];
  UnionFind tree(n);

  for (long i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
    tree.unite(a[i] - 1, b[i] - 1);
  }

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

  cout << cnt;

  return 0;
}

Error: WA

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() {
  long n, m, cnt = 0;
  cin >> n >> m;

  UnionFind tree(n);

  for (long i = 0; i < m; i++) {
    long a, b;
    cin >> a >> b;
    if (!tree.same(a, b)) {
      tree.unite(a, b);
      cnt++;
    }
  }

  cout << cnt;

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

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int dfs(list<int> graph[], int node, int visited[], set<int> &s) {
  if (visited[node] != -1)
    return 0;

  visited[node] = 1;
  s.erase(node);

  for (auto i : graph[node]) {
    dfs(graph, i, visited, s);
  }

  return 1;
}

int main() {
  int n, m;
  cin >> n >> m;
  list<int> graph[n];

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  int visited[n];

  for (int i = 0; i < n; i++)
    visited[i] = -1;

  set<int> s;
  for (int i = 0; i < n; i++)
    s.insert(i);

  int count = -1;

  while (!s.empty()) {
    count++;
    auto ptrele = s.begin();
    auto ele = *ptrele;

    dfs(graph, ele, visited, s);
  }

  cout << count;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int dfs(list<int> graph[], int node, int visited[], set<int> &s) {
  if (visited[node] != -1)
    return 0;

  visited[node] = 1;
  s.erase(node);

  for (auto i : graph[node]) {
    dfs(graph, i, visited, s);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  list<int> graph[n];

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  int visited[n];

  for (int i = 0; i < n; i++)
    visited[i] = -1;

  set<int> s;
  for (int i = 0; i < n; i++)
    s.insert(i);

  int count = -1;
  // for(auto i:visited)
  // cout<<i<<" ";

  while (!s.empty()) {
    count++;
    auto ptrele = s.begin();
    auto ele = *ptrele;

    dfs(graph, ele, visited, s);
  }

  cout << count;

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


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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
const ll INF = 1LL << 60;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> A(N + 1, vector<ll>(0, 0));
  ll a, b;
  rep(i, M) {
    cin >> a >> b;
    A[a].push_back(b);
    A[b].push_back(a);
  }

  vector<ll> dist(N + 1, -1);
  queue<ll> que;
  ll ans = 0;
  for (ll i = 1; i < N + 1; i++) {
    if (dist[i] != -1) {
      continue;
    }
    if (dist[i] == -1) {
      ans++;
    }
    que.push(i);
    dist[i] = ans;
    while (!que.empty()) {
      ll v = que.front();
      que.pop();

      for (ll j = 0; j < (ll)A[v].size(); j++) {
        if (dist[A[v][j]] != -1) {
          continue;
        }
        que.push(A[v][j]);
        dist[A[v][j]] = ans;
      }
    }
  }

  cout << ans - 1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
const ll INF = 1LL << 60;

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() {
  ll N, M;
  cin >> N >> M;
  ll A[M], B[M];
  UnionFind tree(N);
  rep(i, M) {
    cin >> A[i] >> B[i];
    tree.unite(A[i], B[i]);
  }
  vector<ll> dist(N, 0);
  rep(i, N) { dist[tree.par[i]] = 1; }

  ll ans = 0;
  rep(i, N) {
    if (dist[i] != 0) {
      ans++;
    }
    // cout<<dist[i]<<endl;
  }
  ans = ans - 1;
  cout << ans << endl;
}