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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
typedef long long ll;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
const int INF = 1e9 + 5;
#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
using namespace std;
int n, m, u, v;
int memo[110000];
bool fl[110000];
vector<int> eg[110000];
int dp(int v) {
  if (fl[v]) {
    return memo[v];
  }
  int ans = 0;
  for (int to : eg[v]) {
    ans = max(ans, dp(to) + 1);
  }
  fl[v] = true;
  memo[v] = ans;
  return ans;
}
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)]); }
};
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n + 1);
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
    uf.unite(b, a);
  }
  set<int> st;
  for (int i = 1; i <= n; i++) {
    st.insert(uf.Find(i));
  }
  cout << st.size() - 1 << endl;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define NIL = -1;
#define all(x) x.begin(), x.end()
const ll INF = 1e9;
const ll mod = 1e9 + 7;

int digit(ll x) {
  if (x / 10 == 0)
    return 1;
  return digit(x / 10) + 1;
}

ll gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

bool is_prime(long long N) {
  if (N == 1)
    return false;
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0)
      return false;
  }
  return true;
}

ll lcm(ll a, ll b) { return ((a * b == 0)) ? 0 : (a / gcd(a, b) * b); }

double DegreeToRadian(double degree) { return degree * M_PI / 180.0; }

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

///////////////////////////////////////////////////////////////////////////////////

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> seen(n);
  vector<vector<int>> rep(n, vector<int>(n));
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    rep[a].push_back(b);
    rep[b].push_back(a);
  }

  queue<int> q;
  q.push(0);
  int cnt = 0;
  for (int i = 0; i < n; i++)
    seen[i] = 0;
  while (!q.empty()) {
    int nx = q.front();
    q.pop();
    if (seen[nx])
      continue;
    cnt++;
    for (auto v : rep[nx]) {
      seen[v]++;
      q.push(v);
    }
  }

  cout << cnt << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long int ll;

using namespace std;
const double PI = 3.14159265358979323846;

void bfs(vector<int> &visit, vector<vector<int>> &G, int s, int &count) {

  visit[s] = 1;

  for (int v : G[s]) {

    if (visit[v] == 0) {

      visit[v] = 1;
      count++;
      bfs(visit, G, v, count);
    }
  }
}

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

  vector<vector<int>> G(N, vector<int>(0));

  for (int i = 0; i < M; i++) {
    int A, B;

    cin >> A >> B;

    G[A - 1].push_back(B - 1);
    G[B - 1].push_back(A - 1);
  }
  vector<int> visit(N, 0);

  int count = 0;

  int s_i = 0;

  int c = 0;

  for (int i = 0; i < N; i++) {

    if (visit[i] == 0) {

      count++;
      bfs(visit, G, i, c);
    }
  }

  cout << count - 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;

using namespace std;
const double PI = 3.14159265358979323846;

void bfs(vector<int> &visit, vector<vector<int>> &G, int s) {

  visit[s] = 1;

  for (int v : G[s]) {

    if (visit[v] == 0) {

      visit[v] = 1;
      bfs(visit, G, v);
    }
  }
}

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

  vector<vector<int>> G(N, vector<int>(0));

  for (int i = 0; i < M; i++) {
    int A, B;

    cin >> A >> B;

    G[A - 1].push_back(B - 1);
    G[B - 1].push_back(A - 1);
  }
  vector<int> visit(N, 0);

  int count = 0;

  int s_i = 0;
  while (s_i != -1) {
    count++;
    bfs(visit, G, s_i);

    s_i = -1;
    for (int i = 0; i < N; i++) {

      if (visit[i] == 0) {

        s_i = i;
        break;
      }
    }
  }

  cout << count - 1 << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
int main() {
  int n, m;
  cin >> n >> m;
  Graph g(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> dist(n, -1);
  queue<int> que;
  int group = 0;
  rep(i, n) {
    if (dist[i] == 1)
      continue;
    dist[i] = 1;
    que.push(i);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int nv : g[v]) {
        if (dist[nv] == 1)
          continue;

        dist[nv] = 1;
        que.push(nv);
      }
    }
    group++;
  }
  cout << group - 1 << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
int main() {
  int n, m;
  cin >> n >> m;
  Graph g(n + 1);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> dist(n, -1);
  queue<int> que;
  int group = 0;
  rep(i, n + 1) {
    if (dist[i] == 1)
      continue;
    dist[i] = 1;
    que.push(i);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int nv : g[v]) {
        if (dist[nv] == 1)
          continue;

        dist[nv] = 1;
        que.push(nv);
      }
    }
    group++;
  }
  cout << group - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void dfs(vector<int> adj[], int sv, bool *visited) {
  visited[sv] = true;
  for (int i = 0; i < adj[sv].size(); i++) {
    if (visited[adj[sv][i]] == false) {
      dfs(adj, adj[sv][i], visited);
    }
  }
}
int cnt(vector<int> adj[], int n) {
  bool *visited = new bool[n];
  for (int i = 0; i < n; i++) {
    visited[i] = false;
  }

  int ct = 0;
  for (int i = 0; i < n; i++) {
    if (visited[i] == false) {
      dfs(adj, i, visited);
      ct++;
    }
  }
  return ct;
}
int main() {
  int n;
  cin >> n;
  int m;
  cin >> m;

  vector<int> adj[n];

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x - 1].push_back(y - 1);
    adj[y - 1].push_back(x - 1);
  }

  int ct = cnt(adj, n);
  cout << ct - 1;
}


Error: TLE

Fixed submission:

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

void dfs(vector<unordered_set<ll>> adj, ll sv, ll n, bool *visited) {
  visited[sv] = true;
  for (auto it = adj[sv].begin(); it != adj[sv].end(); it++) {
    if (visited[*it] == false) {
      dfs(adj, (*it), n, visited);
    }
  }
}
ll cnt(vector<unordered_set<ll>> adj, ll n) {
  bool *visited = new bool[n];
  for (ll i = 0; i < n; i++) {
    visited[i] = false;
  }

  ll ct = 0;
  for (ll i = 0; i < n; i++) {
    if (visited[i] == false) {
      dfs(adj, i, n, visited);
      ct++;
    }
  }
  return ct;
}
int main() {
  ll n;
  cin >> n;
  ll m;
  cin >> m;

  vector<unordered_set<ll>> adj(n);

  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    adj[x - 1].insert(y - 1);
    adj[y - 1].insert(x - 1);
  }
  ll ct = cnt(adj, n);
  cout << ct - 1;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

// #pragma GCC optimize(2)
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
#define db double
#define pb push_back
#define pa pair<signed, signed>
#define mp make_pair
#define sc(z) scanf("%d", &(z))
#define _ff(i, a, b) for (signed i = (a); i <= (b); ++i)
#define _rr(i, a, b) for (signed i = (a); i >= (b); --i)
#define _f(i, a, b) for (signed i = (a); i < (b); ++i)
#define _r(i, a, b) for (signed i = (a); i > (b); --i)
#define lson node << 1, l, mid
#define rson node << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
const signed inf = 0x3f3f3f3f;
const signed maxn = 1e5 + 5;
vector<int> G[maxn];
int tot, v[maxn];
void dfs(int u) {
  v[u] = 1;
  _f(i, 0, G[u].size()) {
    int k = G[u][i];
    if (v[k])
      continue;
    dfs(k);
  }
}
int main() {
  memset(v, 0, sizeof(v));
  int n, m;
  sc(n), sc(m);
  int u, w;
  while (m--) {
    sc(u), sc(w);
    G[u].pb(w);
    G[w].pb(u);
  }
  _ff(i, 1, n) {
    if (v[i])
      continue;
    tot++;
    dfs(i);
  }
  printf("%d\\n", tot - 1);
  return 0;
}

Error: 0

Fixed submission:

// #pragma GCC optimize(2)
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
#define db double
#define pb push_back
#define pa pair<signed, signed>
#define mp make_pair
#define sc(z) scanf("%d", &(z))
#define _ff(i, a, b) for (signed i = (a); i <= (b); ++i)
#define _rr(i, a, b) for (signed i = (a); i >= (b); --i)
#define _f(i, a, b) for (signed i = (a); i < (b); ++i)
#define _r(i, a, b) for (signed i = (a); i > (b); --i)
#define lson node << 1, l, mid
#define rson node << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
const signed inf = 0x3f3f3f3f;
const signed maxn = 1e5 + 5;
vector<int> G[maxn];
int tot, v[maxn];
void dfs(int u, int w) {
  v[u] = 1;
  _f(i, 0, G[u].size()) {
    int k = G[u][i];
    if (k == w)
      continue;
    dfs(k, u);
  }
}
int main() {
  int n, m;
  sc(n), sc(m);
  int u, w;
  while (m--) {
    sc(u), sc(w);
    G[u].pb(w);
    G[w].pb(u);
  }
  _ff(i, 1, n) {
    if (v[i])
      continue;
    tot++;
    dfs(i, 0);
  }
  printf("%d\\n", tot - 1);
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

struct UnionFind {
  vector<ll> par;
  ll count = 0;
  UnionFind(ll n) : par(n) {
    for (ll i = 0; i < n; i++) {
      par[i] = i;
      if (i == 0)
        count = n;
    }
  }

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

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

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

  ll ans() { return count - 1; }
};

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

  UnionFind tree(n);

  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    tree.unite(a, b);
  }

  cout << tree.ans() << endl;
}

Error: 0

Fixed submission:

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

struct UnionFind {
  vector<int> par;
  int count = 0;
  UnionFind(int n) : par(n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      if (i == 0)
        count = n;
    }
  }

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

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

  int ans() { return count - 1; }
};

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

  cout << tree.ans() << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

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

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

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

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

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

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

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

  int ans = 0;

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

  cout << ans - 1;
}

Error: WA

Fixed submission:

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

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

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

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

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

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

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

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

  int ans = 0;

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

  cout << ans - 1;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

Error: -11

Fixed submission:

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

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


Error: WA

Fixed submission:

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

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

Error: 0

Fixed submission:

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

#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  // assign: 総とっかえ
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      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;

    // xにyをmerge
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[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.merge(a, b);
  }
  set<int> r;
  rep(i, n) { r.insert(uf.root(i)); }
  cout << r.size() - 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  // assign: 総とっかえ
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      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;

    // xにyをmerge
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

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

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    uf.merge(a, b);
  }
  set<int> r;
  rep(i, n) { r.insert(uf.root(i + 1)); }
  cout << r.size() - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: 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);
}

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();
}


Error: 0

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

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();
}