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

#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef pair<lli, lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
const lli MAX = 1e9 + 7;

void dfs(int v, vector<bool> &used, vector<vector<int>> &g, int &sz) {
  if (!used[v])
    sz++;
  used[v] = true;

  for (size_t i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (!used[to])
      dfs(to, used, g, sz);
  }
}

int main() {
  fast;
  int t = 1;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> adj(n);
    for (int i = 0; i < m; i++) {
      lli x, y;
      cin >> x >> y;
      adj[x - 1].push_back(y - 1);
      adj[y - 1].push_back(x - 1);
    }
    vector<bool> vis(n);
    int ans = 1;
    for (int i = 0; i < n; i++) {
      if (!vis[i]) {
        int sz = 0;
        dfs(i, vis, adj, sz);
        ans = max(sz, ans);
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef pair<lli, lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
const lli MAX = 1e9 + 7;

void dfs(int v, vector<bool> &used, vector<vector<int>> &g) {
  used[v] = true;
  for (size_t i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (!used[to])
      dfs(to, used, g);
  }
}

int main() {
  fast;
  int t = 1;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> adj(n);
    vector<bool> vis1(n);
    for (int i = 0; i < m; i++) {
      lli x, y;
      cin >> x >> y;
      adj[x - 1].push_back(y - 1);
      adj[y - 1].push_back(x - 1);
      vis1[x - 1] = 1;
      vis1[y - 1] = 1;
    }
    vector<bool> vis(n);
    int con = 0;
    int single = 0;
    for (int i = 0; i < n; i++)
      if (!vis1[i])
        single++;
    for (int i = 0; i < n; i++) {
      if (!vis[i]) {
        con++;
        dfs(i, vis, adj);
      }
    }
    n -= single;
    con -= single;
    cout << (n / con + n % con) << "\\n";
  }
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

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

pair<int, int> root(int x, vector<int> &g, int t) {
  if (x == g.at(x)) {
    return pair<int, int>(x, t);
  } else {
    pair<int, int> k = root(g.at(x), g, t + 1);
    g.at(x) = k.first;
    return k;
  }
}

void connect(int a, int b, vector<int> &g) {
  pair<int, int> m = root(a, g, 0);
  pair<int, int> n = root(b, g, 0);
  if (m.first != n.first) {
    if (m.second > n.second) {
      g.at(n.first) = m.first;
    } else {
      g.at(m.first) = n.first;
    }
  }
  return;
}

int main() {

  int N, M;
  cin >> N >> M;

  vector<int> G(N);
  for (int i = 0; i < N; i++) {
    G.at(i) = i;
  }

  int A, B;

  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    connect(A - 1, B - 1, G);
  }

  vector<int> G2(N, 0);
  for (int i = 0; i < N; i++) {
    G2.at(root(i, G, 0).first)++;
  }

  int X = 0;
  for (int i = 0; i < N; i++) {
    X = max(X, G2.at(i));
  }

  cout << X << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

pair<int, int> root(int x, vector<int> &g, int t) {
  if (x == g.at(x)) {
    return pair<int, int>(x, t);
  } else {
    pair<int, int> k = root(g.at(x), g, t + 1);
    g.at(x) = k.first;
    return k;
  }
}

void connect(int a, int b, vector<int> &g) {
  pair<int, int> m = root(a, g, 0);
  pair<int, int> n = root(b, g, 0);
  if (m.first != n.first) {
    if (m.second > n.second) {
      g.at(n.first) = m.first;
    } else {
      g.at(m.first) = n.first;
    }
  }
  return;
}

int main() {

  int N, M;
  cin >> N >> M;

  vector<int> G(N);
  for (int i = 0; i < N; i++) {
    G.at(i) = i;
  }

  int A, B;

  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    connect(A - 1, B - 1, G);
  }

  int X = 0;
  int Y = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (root(j, G, 0).first == i) {
        Y++;
      }
    }
    if (X < Y) {
      X = Y;
    }
    Y = 0;
  }

  cout << X << endl;

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

#include <bits/stdc++.h>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
using ll = long long;

ll find(ll x, vector<ll> &par) {
  if (par.at(x) == x) {
    return x;
  } else {
    return par.at(x) = find(par.at(x), par);
  }
}

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(M), B(M);

  vector<ll> par(N), rank(N), size(N);
  for (ll i = 0; i < N; i++) {
    par.at(i) = i;
    rank.at(i) = 0;
    size.at(i) = 1;
  }

  for (ll i = 0; i < M; i++) {
    cin >> A.at(i) >> B.at(i);
    ll x = find(A.at(i) - 1, par);
    ll y = find(B.at(i) - 1, par);
    if (x != y) {
      if (rank.at(x) < rank.at(y)) {
        par.at(x) = par.at(y);
        size.at(x) += size.at(y);
        size.at(y) = size.at(x);
      } else {
        par.at(y) = par.at(x);
        if (rank.at(x) == rank.at(y))
          rank.at(x)++;
        size.at(x) += size.at(y);
        size.at(y) = size.at(x);
      }
    }
  }
  ll maxsize = 0;
  for (ll i = 0; i < N; i++) {
    ll x = find(i, par);
    ll a = size.at(x);
    if (a > maxsize)
      maxsize = a;
  }
  cout << maxsize;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 5) >= this->size() (which is 5)


Fixed submission:

#include <bits/stdc++.h>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
using ll = long long;

ll find(ll x, vector<ll> &par) {
  if (par.at(x) == x) {
    return x;
  } else {
    return par.at(x) = find(par.at(x), par);
  }
}

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(M), B(M);

  vector<ll> par(N), rank(N), size(N);
  for (ll i = 0; i < N; i++) {
    par.at(i) = i;
    rank.at(i) = 0;
    size.at(i) = 1;
  }

  for (ll i = 0; i < M; i++) {
    cin >> A.at(i) >> B.at(i);
    ll x = find(A.at(i), par);
    ll y = find(B.at(i), par);
    if (x != y) {
      if (rank.at(x) < rank.at(y)) {
        par.at(x) = par.at(y);
        size.at(x) += size.at(y);
        size.at(y) = size.at(x);
      } else {
        par.at(y) = par.at(x);
        if (rank.at(x) == rank.at(y))
          rank.at(x)++;
        size.at(x) += size.at(y);
        size.at(y) = size.at(x);
      }
    }
  }
  ll maxsize = 0;
  for (ll i = 0; i < N; i++) {
    ll x = find(i, par);
    ll a = size.at(x);
    if (a > maxsize)
      maxsize = a;
  }
  cout << maxsize;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast_cin                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
unordered_set<ll> st[200005];
int countt = 0;
void DFSUtil(long long v, bool visited[]) {
  // Mark the current node as visited and print it
  visited[v] = true;
  //  cout << v << " ";
  countt++;
  // Recur for all the vertices
  // adjacent to this vertex

  for (auto i : st[v])
    if (!visited[i])
      DFSUtil(i, visited);
}
int main() {
  fast_cin;
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

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

  ll u, v;
  for (ll i = 0; i < M; i++) {
    cin >> u >> v;
    --u, --v;
    st[u].insert(v);
    st[v].insert(u);
  }

  bool vis[N];
  for (ll i = 0; i < N; i++)
    vis[i] = false;
  ll ans = 0;
  countt = 0;
  for (ll v = 0; v < N; v++) {
    if (vis[v] == false) {
      // print all reachable vertices
      // from v
      DFSUtil(v, vis);
      if (countt > ans)
        ans = countt;
      countt = 0;
    }
  }
  cout << ans << "\\n";
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lmax LLONG_MAX
#define lmin LLONG_MIN
#define imax INT_MAX
#define imin INT_MIN
#define f(i, a, b) for (long long i = a; i < b; i++)
#define rf(i, a, b) for (long long i = a; i >= b; i--)
#define tc                                                                     \\
  long long t;                                                                 \\
  cin >> t;                                                                    \\
  while (t--)
#define pb push_back
#define mp make_pair
#define gcd __gcd
#define F first
#define S second
#define input(a, n)                                                            \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> a[i];
#define output(a, n)                                                           \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";
#define up upper_bound
#define lo lower_bound
#define endl "\\n"
#define mem(a) memset(a, -1, sizeof(a))
#define ll long long
#define umap unordered_map

ll vis[100005] = {0};
set<ll> v[100005];
ll cnt = 0;
void dfs(ll x) {
  if (vis[x] == 1)
    return;

  vis[x] = 1;
  cnt++;
  for (auto p : v[x]) {
    dfs(p);
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll n, m;

  cin >> n >> m;

  f(i, 0, m) {
    ll a, b;
    cin >> a >> b;
    v[a - 1].insert(b - 1);
    v[b - 1].insert(a - 1);
  }

  ll ans = 0;
  f(i, 0, n) {
    cnt = 0;
    if (vis[i] == 0) {
      dfs(i);
    }
    ans = max(ans, cnt);
  }

  cout << ans << endl;

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

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

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

  vector<set<ll>> v(N);
  rep(i, M) {
    ll t1, t2;
    cin >> t1 >> t2;
    t1--;
    t2--;
    v[t1].insert(t2);
    v[t2].insert(t1);
  }
#if 0
    rep(i,N) {
        for (auto j : v[i]) {
            cout << j << " ";
        }
        cout << endl;
    }
#endif
  vector<int> dist(N, -1);
  vector<int> m(N, -1);
  for (int j = 0; j < N; ++j) {
    if (m[j] == 1)
      continue;
    queue<ll> que;
    que.push(j);
    m[j] = 1;
    int num = 0;
    while (1) {
      if (que.empty())
        break;
      ll f = que.front();
      num++;
      que.pop();
      for (auto i : v[f]) {
        if (m[i] != 1) {
          que.push(i);
          m[i] = 1;
        }
      }
    }
    dist[j] = num;
  }
  sort(dist.begin(), dist.end(), greater<ll>());
  cout << dist[0] << endl;
}


Error: WA

Fixed submission:

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

int main() {
  ll N, M;
  cin >> N >> M;
  if (M == 0) {
    cout << N << endl;
    exit(0);
  }
  vector<vector<ll>> g(N, vector<ll>(N, 0));
#if 0
    rep(i,M) {
        int t1, t2; cin >> t1 >> t2;
        t1--;
        t2--;
        v[t1][t2] = 1;
        v[t2][t1] = 1;
    
    }
#endif
#if 0
    vector<int> dist(N, -1);
    vector<int> m(N, 0);
    for (int j = 0; j < N; ++j) {
        if (m[j] == 1) continue;        
        queue<int> que;
        que.push(j);
        m[j] = 1;
        int num = 0;
        while (1) {
            if (que.empty()) break;
            int f = que.front();
            num++;
            que.pop();
            for (int i = 0; i < N; ++i) {
                if (v[f][i] == 1) {
                    if (m[i] != 1) {
                        que.push(i);
                        m[i] = 1;
                    }
                }
            }
        }
        dist[j] = num;
    }
    sort(dist.begin(), dist.end(), greater<ll>());
    cout << dist[0] << endl;
#endif
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF long long int(1e18 + 1)
#define MAX 200001
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;
vector<int> a[200001];
int main() {
  int n, m;
  cin >> n >> m;
  int x, y;
  vector<bool> done(n + 1, 0);
  rep(i, 0, m) {
    cin >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  int s;
  int ans = 0;
  rep(i, 1, n + 1) {
    if (done[i])
      continue;
    queue<int> que;
    done[i] = true;
    int total = 1;
    for (auto q : a[i])
      que.push(q);
    while (!que.empty()) {
      s = que.front();
      if (done[s]) {
        que.pop();
        continue;
      }
      done[s] = true;
      total++;
      for (auto qq : a[s])
        que.push(qq);
      que.pop();
    }
    ans = max(ans, total);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF long long int(1e18 + 1)
#define MAX 200001
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;
set<int> a[200000];
int main() {
  int n, m;
  cin >> n >> m;
  int x, y;
  rep(i, 0, m) {
    cin >> x >> y;
    a[x].insert(y);
    a[y].insert(x);
  }
  size_t ans = 0;
  rep(i, 1, n + 1) ans = max(ans, a[i].size() + 1);
  cout << ans << endl;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
int ky[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int kx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int ddy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ddx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
typedef long long ll;
#define pi pair
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define rep1(i, a, b) for (int i = a; i < b; i++)
#define rep2(i, a, b) for (int i = a; i <= b; i++)
#define INF 999999999999
const double PI = acos(-1.0);
int n, m;
vector<int> adj[200001];
int a, b;
set<pi<int, int>> s;
ll ans;
bool vis[200001];
ll cnt;
ll ret;

void dfs(int here) {
  if (vis[here])
    return;
  vis[here] = true;

  cnt++;
  int sz = adj[here].size();

  rep1(i, 0, sz) {
    int next = adj[here][i];
    dfs(next);
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n >> m;
  rep1(i, 0, m) {
    cin >> a >> b;
    swap(a, b);
    if (s.count({a, b}))
      continue;
    s.insert({a, b});

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

  rep2(i, 1, n) {
    cnt = 0;
    dfs(i);
    ans = max(ans, cnt);
  }

  // cout << ans << ' ' << ret << '\\n';
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
int ky[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int kx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int ddy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ddx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
typedef long long ll;
#define pi pair
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define rep1(i, a, b) for (int i = a; i < b; i++)
#define rep2(i, a, b) for (int i = a; i <= b; i++)
#define INF 999999999999
const double PI = acos(-1.0);
int n, m;
vector<int> adj[200001];
int a, b;
set<pi<int, int>> s;
ll ans;
bool vis[200001];
ll cnt;
ll ret;

void dfs(int here) {
  if (vis[here])
    return;
  vis[here] = true;

  cnt++;
  int sz = adj[here].size();

  rep1(i, 0, sz) {
    int next = adj[here][i];
    dfs(next);
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n >> m;
  rep1(i, 0, m) {
    cin >> a >> b;
    swap(a, b);
    if (s.count({a, b}))
      continue;
    else
      s.insert({a, b});

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

  rep2(i, 1, n) {
    cnt = 0;
    // memset(vis, false, sizeof(vis));
    dfs(i);
    if (cnt >= 2) {
      ans += cnt;
      ret++;
    }
  }

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

#include <iostream>
using namespace std;
const int maN = 2e5 + 5;
int parent[maN];
int sz[maN];
void make_set(int v) { parent[v] = v; }
int find_set(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
  int u = find_set(a);
  int v = find_set(b);
  if (u != v) {
    if (sz[u] < sz[v]) {
      // swap(u, v);
      parent[u] = v;
      sz[v] += sz[u];
    } else {
      parent[v] = u;
      sz[u] += sz[v];
    }
  }
}

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

  for (int i = 0; i <= n; ++i) {
    parent[i] = i;
    sz[i] = 1;
  }
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    union_sets(a, b);
  }
  int max_set = 1;
  for (int i = 0; i < n; ++i) {
    // cout << sz[i] << " ";
    if (max_set < sz[i])
      max_set = sz[i];
  }

  cout << max_set;

  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
using namespace std;
const int maN = 1e5;
int parent[maN];
int sz[maN];
void make_set(int v) { parent[v] = v; }
int find_set(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = find_set(parent[v]);
}

void union_sets(int a, int b) {
  int u = find_set(a);
  int v = find_set(b);
  if (u != v) {
    if (sz[u] < sz[v]) {
      // swap(u, v);
      parent[u] = v;
      sz[v] += sz[u];
    } else {
      parent[v] = u;
      sz[u] += sz[v];
    }
  }
}

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

  for (int i = 0; i <= n; ++i) {
    parent[i] = i;
    sz[i] = 1;
  }
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    union_sets(a, b);
  }
  int max_set = 1;
  for (int i = 0; i < n; ++i) {
    // cout << sz[i] << " ";
    if (max_set < sz[i])
      max_set = sz[i];
  }

  cout << max_set;

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

#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;

class UnionFind {
private:
  vector<int> parent, height, size;

public:
  UnionFind(const int node_size)
      : parent(node_size), height(node_size, 0), size(node_size, 1) {
    for (int i = 0; i < node_size; ++i) {
      parent[i] = i;
    }
  }
  int Find(const int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return parent[x] = Find(parent[x]);
    }
  }
  void Union(int x, int y) {
    x = Find(x), y = Find(y);
    if (x == y) {
      return;
    }
    if (height[x] < height[y]) {
      parent[x] = y;
      size[y] += size[x];
    } else {
      parent[y] = x;
      if (height[x] == height[y]) {
        ++height[x];
      }
      size[x] += size[y];
    }
  }
  int siz(int x) {
    x = Find(x);
    return size[x];
  }
};

int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.Union(a, b);
  }
  int ret = 0;
  for (int i = 0; i < n; ++i)
    if (i == uf.Find(i)) {
      ret = max(ret, uf.siz(i));
    }
  cout << ret << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;

template <int N> class UF {
public:
  vector<int> par, size;
  UF() : par(N), size(N, 1) {
    for (int i = 0; i < N; ++i)
      par[i] = i;
  };
  int operator[](int x) { return (x == par[x]) ? x : operator[](par[x]); }
  void operator()(int x, int y) {
    x = operator[](x);
    y = operator[](y);
    if (x == y)
      return;
    if (x > y)
      swap(x, y);
    par[y] = x;
    size[x] += size[y];
  }
  int siz(int x) {
    x = operator[](x);
    return size[x];
  }
};

UF<200050> uf{};
int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf(a, b);
  }
  int ret = 0;
  for (int i = 0; i < n; ++i)
    if (i == uf[i]) {
      ret = max(ret, uf.siz(i));
    }
  cout << ret << endl;
  return 0;
}
Problem p02573 (C++) — buggy submission, verdict: Runtime Error

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

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

int deg[100005];
vector<int> to[100005];

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);
  }
  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 <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 1; i <= (int)(n); i++)
ll INF = 1000000007;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m + 1), b(m + 1);
  vector<vector<int>> s(n + 1, vector<int>(n + 1));
  rep(i, m) {
    cin >> a[i] >> b[i];
    s[a[i]][b[i]]++;
  }
  int ans = 0;
  rep(i, n) {
    int sum = 0;
    rep(j, n) {
      sum += s[i][j];
      if (sum != 0) {
        ans++;
        break;
      }
    }
  }
  if (ans == n - 1)
    cout << ans + 1 << endl;
  else
    cout << ans << endl;
}