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

#include <iostream>

using namespace std;
#define Inf 1e6 + 7;
const int N = 1e7 + 7;
int s[N];
int Find(int i) {
  int r = i;
  while (s[r] != r) {
    r = s[r];
  }
  int x = i, j;
  while (x != r) {
    j = s[x];
    s[x] = r;
    x = j;
  }
  return r;
}
void Union(int i, int j) {
  int x = Find(i);
  int y = Find(j);
  if (x != y) {
    s[y] = x;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    s[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    Union(u, v);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == i) {
      ans++;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;
#define Inf 1e6 + 7;
const int N = 1e7 + 7;
int s[N];
int Find(int i) {
  if (s[i] == i) {
    return i;
  } else {
    return s[i] = Find(i);
  }
}
void Union(int i, int j) {
  int x = Find(i);
  int y = Find(j);
  if (x != y) {
    s[y] = x;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    s[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    Union(u, v);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == i) {
      ans++;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

int n, m;
vector<int> adj[100001];
bool vis[100001];

void dfs(int vtx) {
  vis[vtx] = true;
  for (int i : adj[vtx])
    if (!vis[i])
      dfs(i);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  int ans = -1;
  for (int i = 1; i <= n; i++)
    if (!vis[i])
      dfs(i), ans++;
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

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

struct union_find {
  vector<int> par, sz;
  union_find(int n) : par(n + 1), sz(n + 1) {
    for (int i = 1; i <= n; i++)
      par[i] = -1, sz[i] = 1;
  }
  int find(int x) { return par[x] == -1 ? x : par[x] = find(par[x]); }
  void unite(int a, int b) {
    a = find(a), b = find(b);
    if (sz[b] > sz[a])
      swap(a, b);
    sz[a] += sz[b];
    par[b] = a;
  }
};

int n, m;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  union_find dsu(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    dsu.unite(a, b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += (dsu.find(i) == i);
  cout << ans - 1 << '\\n';
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int father(vector<int> &f, int x) {
  return (f[x] == x) ? x : (f[x] = father(f, f[x]));
}
int merge(vector<int> &f, int x, int y) {
  x = father(f, x);
  y = father(f, y);
  if (x == y)
    return 0;
  f[x] = y;
  return 1;
}
int main() {
  int n;
  int m;
  cin >> n >> m;
  vector<int> f(n + 1, 0);
  for (int i = 1; i <= n; i++)
    f[i] = i;
  int ans = n - 1;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (merge(f, x, y))
      ans--;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int father(vector<int> &f, int x) {
  return (f[x] == x) ? x : (father(f, f[x]));
}
int merge(vector<int> &f, int x, int y) {
  x = father(f, x);
  y = father(f, y);
  if (x == y)
    return 0;
  f[x] = y;
  return 1;
}
int main() {
  int n;
  int m;
  cin >> n >> m;
  vector<int> f(n + 1, 0);
  for (int i = 1; i <= n; i++)
    f[i] = i;
  int ans = n - 1;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (merge(f, x, y))
      ans--;
  }
  cout << ans << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define fastio                                                                 \\
  cin.sync_with_stdio(false);                                                  \\
  cin.tie(NULL)
#define all(x) x.begin(), x.end()
using namespace std;
void dfs(int x, vector<vector<int>> &adj, vector<bool> &visited) {
  visited[x] = true;
  for (auto &a : adj[x]) {
    if (visited[a])
      continue;
    dfs(a, adj, visited);
  }
}
int main() {
  fastio;
  int n, m;
  int numCon = 0;
  cin >> n >> m;
  vector<bool> visited(n, false);
  vector<vector<int>> adj(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    assert(x >= 0 && x < n);
    assert(y >= 0 && y < n);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i, adj, visited);
      numCon += 1;
    }
  }
  cout << numCon - 1 << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define fastio                                                                 \\
  cin.sync_with_stdio(false);                                                  \\
  cin.tie(NULL)
#define all(x) x.begin(), x.end()
using namespace std;
int n, m;
void dfs(int x, vector<vector<int>> &adj, vector<bool> &visited) {
  if (x >= n)
    return;
  visited[x] = true;
  for (auto &a : adj[x]) {
    if (visited[a])
      continue;
    dfs(a, adj, visited);
  }
}
int main() {
  fastio;
  int numCon = 0;
  vector<bool> visited(n, false);
  cin >> n >> m;
  vector<vector<int>> adj(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    assert(x >= 0 && x < n);
    assert(y >= 0 && y < n);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i, adj, visited);
      numCon += 1;
    }
  }
  cout << numCon - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

vector<int> cities[100005];
bool visited[100005];

void dfs(int i) {
  visited[i] = true;
  for (int x : cities[i]) {
    if (not(visited[x])) {
      dfs(x);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    cities[a].push_back(b);
    cities[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (not(visited[i])) {
      ans++;
      dfs(i);
    }
  }
  cout << ans - 1;
}


Error: WA

Fixed submission:

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

using namespace std;

vector<int> cities[100000];
bool visited[100000];

void dfs(int i) {
  visited[i] = true;
  for (int x : cities[i]) {
    if (not(visited[x])) {
      dfs(x);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    cities[a].push_back(b);
    cities[b].push_back(a);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (not(visited[i])) {
      ans++;
      dfs(i);
    }
  }
  cout << ans - 1;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
using ll = long long;
const int MOD = 1e9 + 7;
const int INF = 2147000000;
// const ll INF = 1000000000000000000LL;

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

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

  int root(int x) {
    if (r[x] < 0)
      return x;
    return r[x] = root(r[x]);
  }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m);
  vector<ll> b(m);
  rep(i, m) { cin >> a[i] >> b[i]; }
  UnionFind tree(n);
  for (int i = 0; i < m; i++) {
    int A, B;
    A = a[i];
    A -= 1;
    B = b[i];
    B -= 1;
    tree.unite(A, B);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (tree.r[i] < 0)
      ans++;
  }
  cout << ans - 1 << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
using ll = long long;
const int MOD = 1e9 + 7;
const int INF = 2147000000;
// const ll INF = 1000000000000000000LL;

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

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

  int root(int x) {
    if (r[x] < 0)
      return x;
    return r[x] = root(r[x]);
  }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }

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

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m);
  vector<ll> b(m);
  rep(i, m) { cin >> a[i] >> b[i]; }
  UnionFind tree(n);
  for (int i = 0; i < m; i++) {
    int A, B;
    A = a[i]--;
    B = b[i]--;
    tree.unite(A, B);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (tree.r[i] < 0)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD = 1000000007;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> 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> seen(n, -1);
  int count = 0;
  rep(i, n) {
    if (seen[i] != -1)
      continue;
    queue<int> que;
    que.push(i);
    seen[i] = count;
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (auto nv : G[v]) {
        if (seen[nv] != -1)
          continue;
        que.push(nv);
        seen[nv] = count;
        // printf("%d %d %d\\n",count,v,nv);
      }
    }
    count++;
  }
  // 連結部の個数-1が答え
  cout << count - 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD = 1000000007;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> 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> seen(n, -1);
  rep(count, n + 1) {
    queue<int> que;
    rep(i, n) {
      if (seen[i] == -1) {
        que.push(i);
        seen[i] = count;
        break;
      }
    }
    // 探索完了時
    if (que.empty()) {
      // 連結部の個数-1が答え
      cout << count - 1 << endl;
      return 0;
    }
    // BFSする時
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (auto nv : G[v]) {
        if (seen[nv] != -1)
          continue;
        que.push(nv);
        seen[nv] = count;
      }
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>

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

int main() {

  int N, M, count = 0;
  cin >> N >> M;
  UnionFind tree(N);

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

  cout << count - 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>

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

int main() {

  int N, M, count = 0;
  cin >> N >> M;
  UnionFind tree(N);

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

  cout << count - 1 << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using Graph = vector<vector<int>>;

// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true; // v を訪問済にする
  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;     // next_v が探索済だったらスルー
    dfs(G, next_v); // 再帰的に探索
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  Graph g(n);
  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);
  }
  seen.resize(n);
  seen.assign(n, false);
  int cnt = 0;
  int size = 0;
  for (int i = 0; i < n; ++i) {
    if (seen[i]) {
      continue;
    }
    dfs(g, i);
    // for(int j=0; j<n; ++j){cout << seen[i] << ",";} cout << endl;

    for (int j = i; j < n; ++j) {
      if (seen[j]) {
        size++;
      } // cout << i << "," << j << "," << size << endl;
      else {
        break;
      }
    }
    if (size != n) {
      cnt++;
    }
  }
  // cnt++;
  cout << cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using Graph = vector<vector<int>>;

// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true; // v を訪問済にする
  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;     // next_v が探索済だったらスルー
    dfs(G, next_v); // 再帰的に探索
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  Graph g(n);
  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);
  }
  seen.resize(n);
  seen.assign(n, false);
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (seen[i]) {
      continue;
    }
    dfs(g, i);
    // for(int j=0; j<n; ++j){cout << seen[i] << ",";} cout << endl;
    for (int j = 0; j < n; ++j) {
      if (!seen[j]) {
        cnt++;
        break;
      }
    }
  }
  // cnt++;
  cout << cnt << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define PI 2 * acos(0)
#define ones(n) __builtin_popcount(n)
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAX 9000000
#define debug(x) cerr << #x << " = " << x << '\\n';
#define LOOP printf("LOOP!!")
#define READ freopen("input.txt", "r", stdin)
#define PRINT freopen("output.txt", "w", stdout);
#define print(a, b) printf("Case %d: %lld\\n", a, b)
#define all(x) (x).begin(), (x).end()
const ll MOD = 1e9 + 7;
using namespace std;
const int VAL = 2e5 + 10;
vector<int> v[VAL];

bool vis[VAL];

void dfs(int n) {

  vis[n] = true;

  for (int i = 0; i < v[n].size(); i++) {
    int next = v[n][i];

    if (!vis[next])
      dfs(next);
  }
}

/************MAIN_FUNCTION**********/
int main(int argc, char *argv[]) {
  /// Disjoint set union
  int n, m, a, b;
  cin >> n >> m;

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

    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int c = 0;
  for (int i = 1; i <= n; i++)
    if (!vis[i])
      dfs(i), c++;

  cout << c - 1 << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define PI 2 * acos(0)
#define ones(n) __builtin_popcount(n)
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAX 9000000
#define debug(x) cerr << #x << " = " << x << '\\n';
#define LOOP printf("LOOP!!")
#define READ freopen("input.txt", "r", stdin)
#define PRINT freopen("output.txt", "w", stdout);
#define print(a, b) printf("Case %d: %lld\\n", a, b)
#define all(x) (x).begin(), (x).end()
const ll MOD = 1e9 + 7;
using namespace std;
const int VAL = 20000000;
vector<int> v[20000];

bool vis[100000];

void dfs(int n) {

  vis[n] = true;

  for (int i = 0; i < v[n].size(); i++)
    if (!vis[v[n][i]])
      dfs(v[n][i]);
}

/************MAIN_FUNCTION**********/
int main(int argc, char *argv[]) {
  /// Disjoint set union
  int n, m, a, b;
  cin >> n >> m;

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

    cin >> a >> b;
    v[b].push_back(a);
    v[a].push_back(b);
  }
  int c = 0;
  for (int i = 0; i < n; i++)
    if (!vis[i])
      dfs(i), c++;

  cout << c - 1 << '\\n';
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
// (setq-default c-basic-offset 2)

// UnionFind
// coding: https://youtu.be/TdR816rqc3s?t=726
// comment: https://youtu.be/TdR816rqc3s?t=6822
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 + 1);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }

  set<int> st;
  for (int i = 1; i <= n; ++i) {
    st.insert(uf.find(i));
  }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// (setq-default c-basic-offset 2)

// UnionFind
// coding: https://youtu.be/TdR816rqc3s?t=726
// comment: https://youtu.be/TdR816rqc3s?t=6822
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);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }

  set<int> st;
  for (int i = 0; i < n; ++i) {
    st.insert(uf.find(i));
  }

  cout << (st.size() - 1) << endl;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

// #fuccccc
#include <bits/stdc++.h>
#define TASK ""
#define inpf freopen(TASK ".inp", "r", stdin)
#define outf freopen(TASK ".out", "w", stdout)
#define ll long long
#define db double
#define F first
#define S second
#define maxn 1000005
#define MP make_pair
#define PB push_back
using namespace std;
int n, m, tr[maxn];
vector<int> a[maxn];
void dfs(int u) {
  tr[u] = 1;
  for (auto v : a[u]) {
    if (!tr[v])
      dfs(v);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    a[u].PB(v);
    a[v].PB(u);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (tr[i] == 0)
      dfs(i), ans++;
  }
  cout << ans - 1;
}


Error: WA

Fixed submission:

// #fuccccc
#include <bits/stdc++.h>
#define TASK ""
#define inpf freopen(TASK ".inp", "r", stdin)
#define outf freopen(TASK ".out", "w", stdout)
#define ll long long
#define db double
#define F first
#define S second
#define maxn 1000005
#define MP make_pair
#define PB push_back

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;

// Graph class represents a undirected graph
// using adjacency list representation
class Graph {
  // No. of vertices
  int V;

  // Pointer to an array containing adjacency lists
  list<int> *adj;

  // A function used by DFS
  void DFSUtil(int v, bool visited[]);

public:
  // Constructor
  Graph(int V);

  void addEdge(int v, int w);
  int NumberOfconnectedComponents();
};

// Function to return the number of
// connected components in an undirected graph
int Graph::NumberOfconnectedComponents() {
  bool *visited = new bool[V];
  int count = 0;
  for (int v = 0; v < V; v++)
    visited[v] = false;

  for (int v = 0; v < V; v++) {
    if (visited[v] == false) {
      DFSUtil(v, visited);
      count += 1;
    }
  }
  return count;
}

void Graph::DFSUtil(int v, bool visited[]) {
  visited[v] = true;
  list<int>::iterator i;

  for (i = adj[v].begin(); i != adj[v].end(); ++i)
    if (!visited[*i])
      DFSUtil(*i, visited);
}

Graph::Graph(int V) {
  this->V = V;
  adj = new list<int>[V];
}
void Graph::addEdge(int v, int w) {
  adj[v].push_back(w);
  adj[w].push_back(v);
}
int main() {
  ll n, m;
  cin >> n >> m;
  Graph g(n);
  for (int i = 1; i <= m; i++) {
    ll u, v;
    cin >> u >> v;
    g.addEdge(u, v);
    g.addEdge(v, u);
  }
  cout << g.NumberOfconnectedComponents() - 1;
  return 0;
}