Problem p02573 (C++) — buggy submission, verdict: Runtime Error

/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
// codeforces
#include <bits/stdc++.h>
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define mp make_pair
#define pb push_back
#define sz(v) ((int)v.size())
#define all(v) v.begin(), v.end()
void parseArray(ll *A, ll n) {
  for (ll K = 0; K < n; K++) {
    cin >> A[K];
  }
}
ll modInverse(ll a, ll b) {
  return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ld dist(ld x, ld y, ld a, ld b) {
  return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(ll *a, ll n) {
  for (ll k = 0; k < n; k++) {
    cerr << a[k] << " ";
  }
  cout << "\\n";
}
#define PI 3.14159265358979323846
#define FF first
#define SS second

#define M 222222
ll sze, numComp;
ll sz[M], id[M];
void build(ll N) {
  sze = numComp = N;
  for (ll k = 0; k < N; k++) {
    sz[k] = 1;
    id[k] = k;
  }
}

ll find(ll p) {
  ll root = p;
  while (root != id[root]) {
    root = id[root];
  }
  while (p != root) {
    ll temp = id[p];
    id[p] = root;
    p = temp;
  }
  return root;
}

bool connected(ll p, ll q) { return find(p) == find(q); }
ll componentSize(ll p) { return sz[find(p)]; }

void unify(ll p, ll q) {
  ll root1 = find(p);
  ll root2 = find(q);
  if (root1 == root2)
    return;
  if (sz[root1] < sz[root2]) {
    sz[root2] += sz[root1];
    id[root1] = root2;
  } else {
    sz[root1] += sz[root2];
    id[root2] = root1;
  }
  numComp--;
}

int main() {
  FASTIO;
  ll n, m;
  cin >> n >> m;
  build(n + 1);
  for (int k = 0; k < m; k++) {
    ll x, y;
    cin >> x >> y;
    unify(x, y);
  }
  ll ans = 0;
  for (int k = 0; k <= n; k++) {
    ans = max(ans, componentSize(k));
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
// codeforces
#include <bits/stdc++.h>
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define mp make_pair
#define pb push_back
#define sz(v) ((int)v.size())
#define all(v) v.begin(), v.end()
void parseArray(ll *A, ll n) {
  for (ll K = 0; K < n; K++) {
    cin >> A[K];
  }
}
ll modInverse(ll a, ll b) {
  return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ld dist(ld x, ld y, ld a, ld b) {
  return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(ll *a, ll n) {
  for (ll k = 0; k < n; k++) {
    cerr << a[k] << " ";
  }
  cout << "\\n";
}
#define PI 3.14159265358979323846
#define FF first
#define SS second

#define M 2222
ll sze, numComp;
ll sz[M], id[M];
void build(ll N) {
  sze = numComp = N;
  for (ll k = 0; k < N; k++) {
    sz[k] = 1;
    id[k] = k;
  }
}

ll find(ll p) {
  ll root = p;
  while (root != id[root]) {
    root = id[root];
  }
  while (p != root) {
    ll temp = id[p];
    id[p] = root;
    p = temp;
  }
  return root;
}

bool connected(ll p, ll q) { return find(p) == find(q); }
ll componentSize(ll p) { return sz[find(p)]; }

void unify(ll p, ll q) {
  ll root1 = find(p);
  ll root2 = find(q);
  if (root1 == root2)
    return;
  if (sz[root1] < sz[root2]) {
    sz[root2] += sz[root1];
    id[root1] = root2;
  } else {
    sz[root1] += sz[root2];
    id[root2] = root1;
  }
  numComp--;
}

int main() {
  FASTIO;
  ll n, m;
  cin >> n >> m;
  build(n + 1);
  for (int k = 0; k < m; k++) {
    ll x, y;
    cin >> x >> y;
    unify(x, y);
  }
  ll ans = 0;
  for (int k = 0; k <= n; k++) {
    ans = max(ans, componentSize(k));
  }
  cout << ans << endl;
  return 0;
}