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

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f(a, b) for (ll i = a; i <= b; i++)
#define pb push_back
#define pf push_front
#define mp make_pair
using namespace std;
#define N 200005
const ll INF = 1000000001;
const ll MOD = 1000000007;

vector<vector<ll>> g(N);
// vector< pair<ll, pair< ll, ll > > > g(N);
ll vis[N] = {0}, par[N] = {0}, child[N] = {0}, cnt = 0;
map<pair<ll, ll>, ll> ump;

void dfs(ll v) {
  vis[v] = 1;
  cnt++;
  for (auto u : g[v])
    if (!vis[u])
      dfs(u);
}

void solve() {
  ll n, m;
  cin >> n >> m;
  f(1, n) g[i].clear();
  f(0, m - 1) {
    ll x, y;
    cin >> x >> y;
    if (ump[{x, y}] != 0)
      continue;
    g[x].pb(y);
    g[y].pb(x);
    ump[{x, y}] += 1;
    ump[{y, x}] += 1;
  }
  ll ans = 0;
  f(1, n) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  // ll t;cin >> t;for(ll index=1; index<=t; index++){
  // cout << "Case #" << index << ": " ;
  solve();
  //}
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f(a, b) for (ll i = a; i <= b; i++)
#define pb push_back
#define pf push_front
#define mp make_pair
using namespace std;
#define N 100005
const ll INF = 1000000001;
const ll MOD = 1000000007;

vector<vector<ll>> g(N);
// vector< pair<ll, pair< ll, ll > > > g(N);
ll vis[N] = {0}, par[N] = {0}, child[N] = {0}, cnt = 0;
map<pair<ll, ll>, ll> ump;

void dfs(ll v) {
  vis[v] = 1;
  cnt++;
  for (auto u : g[v])
    if (!vis[u])
      dfs(u);
}

void solve() {
  ll n, m;
  cin >> n >> m;
  f(1, n) g[i].clear();
  f(0, m - 1) {
    ll x, y;
    cin >> x >> y;
    if (ump[{x, y}] != 0)
      continue;
    g[x].pb(y);
    g[y].pb(x);
    ump[{x, y}] += 1;
    ump[{y, x}] += 1;
  }
  ll ans = 0;
  f(1, n) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  // ll t;cin >> t;for(ll index=1; index<=t; index++){
  // cout << "Case #" << index << ": " ;
  solve();
  //}
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct UnionFind {
  vector<int> f;

  UnionFind(int n) : f(n, -1) {}

  int root(int x) {
    if (f[x] < 0)
      return x;

    else
      return f[x] = root(f[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;

    if (size(x) < size(y))
      swap(rx, ry);
    f[rx] += f[ry];
    f[ry] = rx;
  }

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

int main(void) {
  int N, M, a, b;

  cin >> N >> M;

  int A[M], B[M];

  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }

  UnionFind uf(N);

  for (int i = 0; i < M; i++) {
    a = A[i] - 1, b = B[i] - 1;
    uf.unite(a, b);
  }

  int ans = 0;

  for (int i = 0; i < N; i++) {
    ans = max(ans, uf.size(i));
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct UnionFind {
  vector<int> f;

  UnionFind(int n) : f(n) {
    for (int i = 0; i < n; i++)
      f[i] = -1;
  }

  int root(int x) {
    if (f[x] < 0)
      return x;

    else
      return f[x] = root(f[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (f[x] < f[y])
      swap(x, y);

    if (rx == ry)
      return;
    f[x] += f[y];
    f[y] = rx;
  }

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

int main(void) {
  int N, M, a, b;

  cin >> N >> M;

  int A[M], B[M];

  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }

  UnionFind uf(N);

  for (int i = 0; i < M; i++) {
    a = A[i] - 1, b = B[i] - 1;
    uf.unite(a, b);
  }

  int ans = 0;

  for (int i = 0; i < N; i++) {
    ans = max(ans, uf.size(i));
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#define ul unsigned long long int
#define vi vector<int>
#define vl vector<int64_t>
#define all(x) x.begin(), x.end()
#define pb push_back
#define fo(x, a, b) for (int x = a; x < b; x++)
#define rfo(x, a, b) for (int x = a; x >= b; x--)
#define fi first
#define sec second
#define mp make_pair
int sgn(int x) { return x > 0 ? 1 : (x < 0 ? -1 : 0); }
typedef pair<int, int> pairs;
double pi = 3.14159265358979323846;
ll mod = 1000000007;
ll n, m, k = 0;
vi adj[200001];
bool vis[200001] = {false};

void dfs(int x) {
  vis[x] = true;
  k++;
  for (auto ip = adj[x].begin(); ip != adj[x].end(); ip++) {
    if (!vis[*ip]) {
      dfs(*ip);
    }
  }
}
void solve() {
  cin >> n >> m;
  ll ans = 0;
  fo(i, 0, m) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  fo(i, 0, n + 1) {
    if (!vis[i]) {
      int temp = 0;
      k = 0;
      dfs(i);
    }
    ans = max(ans, k);
  }
  cout << ans;
  cout << "\\n";
  return;
}
int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll t;
  // cin>>t;
  // while(t--)
  { solve(); }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#define ul unsigned long long int
#define vi vector<int>
#define vl vector<int64_t>
#define all(x) x.begin(), x.end()
#define pb push_back
#define fo(x, a, b) for (int x = a; x < b; x++)
#define rfo(x, a, b) for (int x = a; x >= b; x--)
#define fi first
#define sec second
#define mp make_pair
int sgn(int x) { return x > 0 ? 1 : (x < 0 ? -1 : 0); }
typedef pair<int, int> pairs;
double pi = 3.14159265358979323846;
ll mod = 1000000007;
ll n, m, k = 0;
vi adj[100001];
bool vis[100001] = {false};

void dfs(int x) {
  vis[x] = true;
  k++;
  for (auto ip = adj[x].begin(); ip != adj[x].end(); ip++) {
    if (!vis[*ip]) {
      dfs(*ip);
    }
  }
}
void solve() {
  cin >> n >> m;
  ll ans = 0;
  fo(i, 0, m) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  fo(i, 0, n + 1) {
    if (!vis[i]) {
      int temp = 0;
      k = 0;
      dfs(i);
    }
    ans = max(ans, k);
  }
  cout << ans;
  cout << "\\n";
  return;
}
int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll t;
  // cin>>t;
  // while(t--)
  { solve(); }
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

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

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

  vector<int> dist(n, 0);
  queue<int> q;
  int ans = -1;
  for (int i = 0; i < n; ++i) {
    if (dist[i] == 0) {

      dist[i] = 1;
      q.push(i);
      int cnt = 0;

      while (!q.empty()) {
        int now = q.front();
        q.pop();

        for (auto &v : G[now]) {
          if (dist[v] == 0) {
            dist[v] = 1;
            q.push(v);
            cnt++;
          }
        }
      }

      ans = max(ans, cnt);
    }
  }

  cout << ans + 1 << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

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

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

  vector<int> d(n);

  for (int i = 0; i < n; ++i) {
    vector<int> dist(n, 1e9 + 10);
    queue<int> q;
    dist[i] = 0;
    q.push(i);
    int cnt = 0;

    while (!q.empty()) {
      int now = q.front();
      q.pop();

      for (auto &v : G[now]) {
        if (dist[v] > dist[now] + 1) {
          dist[v] = dist[now] + 1;
          q.push(v);
          cnt++;
        }
      }
    }

    d[i] = cnt;
  }

  int ans = *max_element(d.begin(), d.end());

  cout << ans + 1 << endl;

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

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

struct UnionFind {
  vector<int> r;

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

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

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

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

int main(void) {
  int n, m;
  cin >> n >> m;
  UnionFind UF(n);
  while (m--) {
    int a, b;
    cin >> a >> b;
    UF.unite(a - 1, b - 1);
  }

  int ans = 0;
  while (n--)
    ans = max(ans, UF.size(n));

  cout << ans << endl;
  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

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

struct UnionFind {
  vector<int> r;

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

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

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

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

int main(void) {
  int n, m;
  cin >> n >> m;
  UnionFind UF(n);
  while (m--) {
    int a, b;
    cin >> a >> b;
    UF.unite(a, b);
  }

  int ans = 0;
  while (n--) {
    ans = max(ans, UF.size(n));
  }
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

struct UF {
  vector<int> data;
  UF(int n) { data.assign(n, -1); }
  void merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (data[y] < data[x])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
  }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UF data(n);
  int ans = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    data.merge(a, b);
  }
  rep(i, n) chmax(ans, data.size(i));
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

struct UF {
  vector<int> data;
  UF(int n) { data.assign(n, -1); }
  void merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (data[y] < data[x])
      swap(x, y);
    data[x] += data[y];
  }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UF data(n);
  int ans = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    data.merge(a, b);
  }
  rep(i, n) chmax(ans, data.size(i));
  cout << ans << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define ll long long
#define ull unsigned long long
#define ii pair<int, int>
#define iii pair<int, ii>
#define iil pair<ll, ll>
#define iiil pair<ll, iil>
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define s(v) sort(v.begin(), v.end())
#define r(v) reverse(v.begin(), v.end())
#define pf printf
#define sf scanf
#define MIN INT_MIN
#define MAX INT_MAX
#define Accepted 0
#define GCD(x, y) __gcd(x, y)
#define endl "\\n"
#define sz size()
using namespace std;

const int maxn = 1e6 + 69;
int vis[maxn];
int cnt;
vector<int> graph[maxn];

void DFS(int i) {
  cnt++;
  vis[i] = 1;

  for (int j = 0; j < graph[i].sz; j++) {
    int r = graph[i][j];
    if (!vis[r]) {
      DFS(r);
    }
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  int ans = -1;

  while (m--) {
    int x, y;
    cin >> x >> y;
    graph[x].pb(y);
    graph[y].pb(x);
  }

  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cnt = 0;
      DFS(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
}

int main() {
  // fast
  // int tst;    cin>>tst;
  // int cs = 1;

  // while(tst--){
  //   cout << "Case "<<cs++<<": ";
  solve();
  //}

  return Accepted;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define ll long long
#define ull unsigned long long
#define ii pair<int, int>
#define iii pair<int, ii>
#define iil pair<ll, ll>
#define iiil pair<ll, iil>
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define s(v) sort(v.begin(), v.end())
#define r(v) reverse(v.begin(), v.end())
#define pf printf
#define sf scanf
#define MIN INT_MIN
#define MAX INT_MAX
#define Accepted 0
#define GCD(x, y) __gcd(x, y)
#define endl "\\n"
#define sz size()
using namespace std;

const int maxn = 1e6;
int vis[maxn];
int cnt;
vector<int> graph[maxn];

int DFS(int i) {
  cnt++;
  vis[i] = 1;

  for (int j = 0; j < graph[i].sz; j++) {
    int r = graph[i][j];
    if (!vis[r]) {
      DFS(r);
    }
  }
}

void solve() {
  int n, m;
  cin >> n >> m;
  int ans = -1;

  while (m--) {
    int x, y;
    cin >> x >> y;
    graph[x].pb(y);
    graph[y].pb(x);
  }

  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      cnt = 0;
      DFS(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
}

int main() {
  // fast
  // int tst;    cin>>tst;
  // int cs = 1;

  // while(tst--){
  //   cout << "Case "<<cs++<<": ";
  solve();
  //}

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

#include <bits/stdc++.h>
using namespace std;
vector<int> e[200005];
int n, m, ans;
bool vis[200005];
void bfs(int x) {
  queue<int> q;
  int num = 1;
  q.push(x);
  vis[x] = 1;
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    for (int i = 0; i < e[k].size(); ++i)
      if (!vis[e[k][i]]) {
        vis[e[k][i]] = 1;
        ++num;
        q.push(e[k][i]);
      }
  }
  if (num > ans)
    ans = num;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      bfs(i);
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> e[200005];
int n, m, ans;
bool vis[200005];
int bfs(int x) {
  queue<int> q;
  int num = 1;
  q.push(x);
  vis[x] = 1;
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    for (int i = 0; i < e[k].size(); ++i)
      if (!vis[e[k][i]]) {
        vis[e[k][i]] = 1;
        ++num;
        q.push(e[k][i]);
      }
  }
  if (num > ans)
    ans = num;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      bfs(i);
  cout << ans;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

// #include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
#define pb emplace_back
#define LOCAL

using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
typedef long long ll;
typedef pair<int, int> Pii;

template <typename T> inline void read(T &t) {
  char c = getchar();
  t = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c))
    t = t * 10 + c - 48, c = getchar();
  t = f * t;
}

template <typename T, typename... Args> inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}

int n, m;
int a[maxn << 1];
int f[maxn << 1];

int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }

void merge(int x, int y) {
  int tx = find(x);
  int ty = find(y);
  if (tx != ty) {
    f[ty] = tx;
  }
}

int main() {
  read(n, m);
  for (int i = 1; i <= n; i++) {
    f[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int u, v;
    read(u, v);
    if (find(u) != find(v)) {
      merge(u, v);
    }
  }
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    int tmp = find(i);
    a[tmp]++;
    ans = max(a[tmp], ans);
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

// #include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
#define pb emplace_back
#define LOCAL

using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 1e2 + 5;
typedef long long ll;
typedef pair<int, int> Pii;
const ll mod = 1e9 + 7;

template <typename T> inline void read(T &t) {
  char c = getchar();
  t = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c))
    t = t * 10 + c - 48, c = getchar();
  t = f * t;
}

template <typename T, typename... Args> inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}

int n, m;
map<int, int> mps;
int f[maxn << 1];

int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }

void merge(int x, int y) {
  int tx = find(x);
  int ty = find(y);
  if (tx != ty) {
    f[ty] = tx;
  }
}

int main() {
  read(n, m);
  for (int i = 1; i <= n; i++) {
    f[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int u, v;
    read(u, v);
    if (find(u) != find(v)) {
      merge(u, v);
    }
  }
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    int tmp = find(i);
    mps[tmp]++;
    ans = max(mps[tmp], ans);
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define int long long

vector<int> graph[200005];
void add_edge(int u, int v) {
  graph[u].push_back(v);
  graph[v].push_back(u);
}

// Function to traverse the undirected graph
// using DFS algorithm and keep a track of
// individual lengths of connected chains
void depthFirst(int v, vector<int> graph[], vector<bool> &visited, int &ans) {
  // Marking the visited vertex as true
  visited[v] = true;

  // Incrementing the count of
  // connected chain length
  ans++;

  for (auto i : graph[v]) {
    if (visited[i] == false) {
      // Recursive call to the DFS algorithm
      depthFirst(i, graph, visited, ans);
    }
  }
}

// Function to initialize the graph
// and display the result
int UniqueConnectedComponent(int n, vector<int> graph[]) {
  int mx = -1;

  // Initializing boolean visited array
  // to mark visited vertices
  vector<bool> visited(n + 1, false);

  // Initializing a Set container
  unordered_set<int> result;

  // Following loop invokes DFS algorithm
  for (int i = 1; i <= n; i++) {
    if (visited[i] == false) {
      // ans variable stores the
      // individual counts
      int ans = 0;

      // DFS algorithm
      depthFirst(i, graph, visited, ans);

      // Inserting the counts of connected
      // components in set
      result.insert(ans);
      mx = max(mx, ans);
    }
  }
  return mx;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    add_edge(a, b);
  }
  cout << UniqueConnectedComponent(n, graph);
  return 0;
}


Error: WA

Fixed submission:

// CPP program to count number of existing
// groups and number of new groups that can
// be formed.
#include <bits/stdc++.h>
using namespace std;

class Graph {
  int V;

  list<int> *adj;

  int countUtil(int v, bool visited[]);

public:
  Graph(int V);

  void addRelation(int v, int w);
  void countGroups();
};

Graph::Graph(int V) {
  this->V = V;
  adj = new list<int>[V];
}

void Graph::addRelation(int v, int w) {
  v--;
  w--;
  adj[v].push_back(w);
  adj[w].push_back(v);
}

int Graph::countUtil(int v, bool visited[]) {
  int count = 1;
  visited[v] = true;
  for (auto i = adj[v].begin(); i != adj[v].end(); ++i)
    if (!visited[*i])
      count = count + countUtil(*i, visited);
  return count;
}

void Graph::countGroups() {
  bool *visited = new bool[V];
  memset(visited, 0, V * sizeof(int));

  int existing_groups = 0, new_groups = 1;
  for (int i = 0; i < V; i++) {

    if (visited[i] == false) {
      existing_groups++;
      new_groups = new_groups * countUtil(i, visited);
    }
  }

  if (existing_groups == 1)
    new_groups = 0;

  cout << existing_groups << endl;
}

// Driver code
int main() {
  int n;
  cin >> n;
  int m;
  Graph g(n);
  while (m--) {
    int a, b;
    cin >> a >> b;
    g.addRelation(a, b);
  }
  g.countGroups();

  return 0;
}