alexjercan/bugnet huggingface dataset:
Problem p02536 (C++) — buggy submission, verdict: 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 < 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;
}


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

#include <bits/stdc++.h>
using namespace std;
int u, v;
int n, m;

int fa[100005];

void init() {
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
  }
}

int get(int x) {
  if (fa[x] == x) {
    return x;
  }
  return fa[x] = get(fa[x]);
}

int main() {
  scanf("%d %d", &n, &m);
  init();
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &u, &v);
    int x = get(u);
    int y = get(v);
    if (x != y) {
      fa[y] = x;
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (fa[i] == i) {
      cnt++;
    }
  }
  printf("%d", cnt - 1);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int u, v;
} a[100005];
int n, m;

int fa[100005];

void init() {
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
  }
}

int get(int x) {
  if (fa[x] == x) {
    return x;
  }
  return fa[x] = get(fa[x]);
}

int main() {
  scanf("%d %d", &n, &m);
  init();
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", &a[i].u, &a[i].v);
    int x = get(a[i].u);
    int y = get(a[i].v);
    if (x != y) {
      fa[y] = x;
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (fa[i] == i) {
      cnt++;
    }
  }
  printf("%d", cnt - 1);
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define BIL ((ll)1e9)
#define MOD ((ll)1e9 + 7)
#define INF (1LL << 60) // 1LL<<63でオーバーフロー
#define inf (1 << 29)   // 1<<29でオーバーフロー

struct UnionFind {
  vector<int> par;

  UnionFind(int n) {
    for (int i = 0; i < n; i++) {
      par.push_back(i);
    }
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  bool merge(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return false;
    // if(par[rx] > par[ry]) swap(x, y);
    // par[rx] += par[ry];
    par[ry] = rx;
    return true;
  }
  bool isSame(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
  // int size(int x){
  //     return -par[root(x)];
  // }
};

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);

  int n, m;
  cin >> n >> m;

  UnionFind city(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    city.merge(a, b);
  }

  set<int> check;
  for (int i = 0; i < n; i++) {
    int j = city.root(i);
    check.insert(j);
  }

  cout << check.size() - 1 << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define BIL ((ll)1e9)
#define MOD ((ll)1e9 + 7)
#define INF (1LL << 60) // 1LL<<63でオーバーフロー
#define inf (1 << 29)   // 1<<29でオーバーフロー

struct UnionFind {
  vector<int> par;

  UnionFind(int n) { par = vector<int>(n, -1); }
  int root(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }
  bool merge(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return false;
    if (par[rx] > par[ry])
      swap(x, y);
    par[rx] += par[ry];
    par[ry] = rx;
    return true;
  }
  bool isSame(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
  int size(int x) { return -par[root(x)]; }
};

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);

  int n, m;
  cin >> n >> m;

  UnionFind city(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    city.merge(a, b);
  }

  set<int> check;
  for (int i = 0; i < n; i++) {
    int j = city.root(i);
    check.insert(j);
  }

  cout << check.size() - 1 << endl;

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

Error: 127
/tmp/698db5b4-43f2-4c19-ad5c-7cb05fa974b7.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


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

#include <bits/stdc++.h>
#include <iostream>
#define ll long long
using namespace std;
const ll num = 1e9 + 7;
ll mod(ll n) { return (n % num + num) % num; }
ll add(ll x, ll y) { return mod(mod(x) + mod(y)); }
ll mult(ll x, ll y) { return mod(mod(x) * mod(y)); }
ll sub(ll x, ll y) { return mod(mod(x) - mod(y)); }
ll pt(ll n, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return n % num;
  ll t = pt(n, p / 2);
  if (p % 2 == 0)
    return mult(t, t);
  else
    return mult(n, mult(t, t));
}
const int num1 = 100001;
vector<ll> ad[num1];
ll vt[100001];
void dfs(ll i, ll pt) {
  vt[i] = 1;
  for (auto it : ad[i]) {
    if (it != pt && vt[it] == 0)
      dfs(it, i);
  }
}

void solve() {
  ll n, m;
  cin >> n >> m;
  memset(vt, 0, sizeof(vt));
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    ad[x].push_back(y);
    ad[y].push_back(x);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    if (vt[i] == 0) {
      dfs(i, -1);
      ans++;
    }
  }
  // dfs(vt,ad,1,-1);

  cout << ans - 1 << endl;
}
int main() {
  ll t;
  t = 1;
  while (t--) {
    solve();
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define ll long long
using namespace std;
const ll num = 1e9 + 7;
ll mod(ll n) { return (n % num + num) % num; }
ll add(ll x, ll y) { return mod(mod(x) + mod(y)); }
ll mult(ll x, ll y) { return mod(mod(x) * mod(y)); }
ll sub(ll x, ll y) { return mod(mod(x) - mod(y)); }
ll pt(ll n, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return n % num;
  ll t = pt(n, p / 2);
  if (p % 2 == 0)
    return mult(t, t);
  else
    return mult(n, mult(t, t));
}
void dfs(vector<ll> &vt, vector<vector<ll>> &ad, ll i, ll pt) {
  vt[i] = 1;
  for (auto it : ad[i]) {
    if (it != pt && vt[it] == 0)
      dfs(vt, ad, it, i);
  }
}
void solve() {
  ll n, m;
  cin >> n >> m;
  vector<ll> vt(n + 1, 0);
  vector<vector<ll>> ad(n + 1, vector<ll>(n + 1));
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    ad[x].push_back(y);
    ad[y].push_back(x);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    if (vt[i] == 0) {
      dfs(vt, ad, i, -1);
      ans++;
    }
  }
  // dfs(vt,ad,1,-1);

  cout << ans - 1 << endl;
}
int main() {
  ll t;
  t = 1;
  while (t--) {
    solve();
  }
}
Problem p02536 (C++) — buggy submission, verdict: 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 || 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;
}


Error: 0

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 1e9 + 7;

struct UnionFind {
  vector<int> par;
  vector<int> count;

  UnionFind(int N) : par(N), count(N) {
    for (int i = 0; i < N; i++) {
      par[i] = i;
      count[i] = 1;
    }
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  int getCount(int x) { return count[x]; }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (count[rx] < count[ry]) {
      count[ry] += count[rx];
      par[rx] = ry;
    } else {
      count[rx] += count[ry];
      par[ry] = rx;
    }
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  auto u = UnionFind(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    u.unite(a, b);
  }
  set<int> s;
  for (int i = 0; i < n; i++) {
    s.insert(u.root(i));
  }
  cout << s.size() - 1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 1e9 + 7;

struct UnionFind {
  vector<int> par;
  vector<int> count;

  UnionFind(int N) : par(N), count(N) {
    for (int i = 0; i < N; i++) {
      par[i] = i;
      count[i] = 1;
    }
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  int getCount(int x) { return count[x]; }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (count[rx] < count[ry]) {
      count[ry] += count[rx];
      par[rx] = ry;
    } else {
      count[rx] += count[ry];
      par[ry] = rx;
    }
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  auto u = UnionFind(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    u.unite(a, b);
  }
  set<int> s;
  for (int i = 0; i < n; i++) {
    s.insert(u.root(i));
  }
  cout << n - s.size() << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int p[100010];
int n, m;
int cnt[100010];
int ans;

int fifa(int x) {
  int t = x;
  while (p[t] != t) {
    t = p[t];
  }
  while (p[x] != t) {
    int tmp = p[x];
    p[x] = t;
    x = tmp;
  }
  return t;
}

void join(int x, int y) {
  int px = fifa(x);
  int py = fifa(y);
  if (px != py) {
    p[px] = py;
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    p[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    join(a, b);
  }
  for (int i = 1; i <= n; i++) {
    if (fifa(i) == i) {
      ans++;
    }
  }
  printf("%d\\n", ans - 1);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int p[10010];
int n, m;
int cnt[100010];
int ans;

int fifa(int x) {
  int t = x;
  while (p[t] != t) {
    t = p[t];
  }
  while (p[x] != t) {
    int tmp = p[x];
    p[x] = t;
    x = tmp;
  }
  return t;
}

void join(int x, int y) {
  int px = fifa(x);
  int py = fifa(y);
  if (px != py) {
    p[px] = py;
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    p[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    join(a, b);
  }
  for (int i = 1; i <= n; i++) {
    cnt[fifa(i)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[fifa(i)] == 1) {
      ans++;
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  std::vector<std::vector<int>> adj(N + 1);

  int ai, bi;
  for (int i = 0; i < M; ++i) {
    cin >> ai >> bi;

    adj[ai].push_back(bi);
    adj[bi].push_back(ai);
  }

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

  // for(int i = 1; i <= N; ++i) {
  // 	for (int j = 1; j <= N; ++j)
  // 		cout << adj[i][j] << " ";
  // 	cout << endl;
  // }

  int res = 0;

  bool *visited = (bool *)malloc(sizeof(bool) * (N + 1));
  memset(visited, false, N + 1);

  // cout << "visited: ";
  // for(int i = 1; i <= N; ++i) {
  // 	cout << visited[i] << " ";
  // }
  // cout << endl;

  for (int i = 1; i <= N; ++i) {
    if (visited[i])
      continue;
    // cout << "visiting " << i << endl;
    std::stack<int> v;
    v.push(i);

    while (!v.empty()) {

      int x = v.top();
      v.pop();

      visited[x] = true;
      // cout << "visited[" << x << "] = " << visited[x] << endl;

      for (auto ip = adj[x].begin(); ip != adj[x].end(); ++ip) {
        if (!visited[*ip])
          v.push(*ip);
      }
    }
    res += 1;
  }

  // cout << "After, visited: ";
  // for(int i = 1; i <= N; ++i) {
  // 	cout << visited[i] << " ";
  // }
  // cout << endl;

  cout << res - 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  std::vector<bool> v(N + 1, false);
  std::vector<std::vector<bool>> adj(N + 1, v);

  int ai, bi;
  for (int i = 0; i < M; ++i) {
    cin >> ai >> bi;

    adj[ai][bi] = true;
    adj[bi][ai] = true;
  }

  for (int i = 1; i <= N; ++i) {
    adj[i][i] = 1;
  }

  int res = 0;

  bool *visited = (bool *)malloc(sizeof(bool) * (N + 1));
  memset(visited, false, N + 1);

  for (int i = 1; i <= N; ++i) {
    if (visited[i])
      continue;

    std::stack<int> v;
    v.push(i);

    while (!v.empty()) {

      int x = v.top();
      v.pop();

      visited[x] = true;

      for (int j = 1; j <= N; ++j) {
        if (adj[x][j])
          if (!visited[j])
            v.push(j);
      }
    }
    res += 1;
  }

  cout << res - 1 << endl;

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

#include <bits/stdc++.h>

using namespace std;

typedef struct node {
  int key;
  struct node *next;
} Node;

Node *creeazaNod(int k) {
  Node *p = (Node *)malloc(sizeof(Node));
  p->key = k;
  p->next = NULL;
  return p;
}

void insert_last(Node **first, Node **last, int givenKey) {
  Node *p = creeazaNod(givenKey);
  if (*first == NULL)
    *first = *last = p;
  else {
    p->next = NULL;
    (*last)->next = p;
    *last = p;
  }
}

int main() {
  int n, m, nrg = 0;
  cin >> n >> m;
  vector<Node *> fst(n + 1);
  vector<Node *> lst(n + 1);
  for (; m; --m) {
    int a, b;
    cin >> a >> b;
    insert_last(&fst[a], &lst[a], b);
    insert_last(&fst[b], &lst[b], a);
  }
  vector<int> c(n + 1), viz(n + 1);
  for (int i = 1; i <= n; ++i)
    if (viz[i] == 0) {
      ++nrg;
      int st = 0, dr = 0;
      c[st] = i;
      viz[i] = 1;
      while (st <= dr) {
        int prim = c[st];
        for (Node *p = fst[prim]; p != NULL; p = p->next) {
          int nod = p->key;
          if (viz[nod] == 0) {
            viz[nod] = 1;
            ++dr;
            c[dr] = nod;
          }
        }
        ++st;
      }
    }
  cout << nrg - 1;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, m, nrg = 0;
  cin >> n >> m;
  int **lista = (int **)calloc(n + 1, sizeof(int *));
  for (int i = 0; i <= n; ++i)
    lista[i] = (int *)calloc(1, sizeof(int));
  for (; m; --m) {
    int a, b;
    cin >> a >> b;
    int num = lista[a][0];
    lista[a] = (int *)realloc(lista[a], (num + 1) * sizeof(int));
    ++num;
    lista[a][num] = b;
    lista[a][0] = num;

    num = lista[b][0];
    lista[b] = (int *)realloc(lista[b], (num + 1) * sizeof(int));
    ++num;
    lista[b][num] = a;
    lista[b][0] = num;
  }
  vector<int> c(n + 1), viz(n + 1);
  for (int i = 1; i <= n; ++i)
    if (viz[i] == 0) {
      ++nrg;
      int st = 0, dr = 0;
      c[st] = i;
      viz[i] = 1;
      while (st <= dr) {
        int prim = c[st];
        for (int j = 1; j <= lista[prim][0]; ++j) {
          int nod = lista[prim][j];
          if (viz[nod] == 0) {
            viz[nod] = 1;
            ++dr;
            c[dr] = nod;
          }
        }
        ++st;
      }
    }
  cout << nrg - 1;
}