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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// #include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep2(i, x) for (ll i = 1; i <= x; i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;

/*bool IsPrime(ll num) {
        if (num < 2) return false;
        else if (num == 2) return true;
        else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
        double sqrtNum = sqrt(num);
        for (int i = 3; i <= sqrtNum; i += 2) {
                if (num % i == 0) { // 素数ではない
                        return false;
                }
        }
        // 素数である
        return true;
}*/
/*ll gcd(ll a, ll b) {
        if (a < b)swap(a, b);
        if (a % b == 0)return b;
        return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
        return a / gcd(a, b) * b;
}*/
/*ll keta(ll n) {
        ll res = 0;
        while (n >= 1) {
                res += n % 10; n /= 10;
        }
        return res;
}*/
/*ll modpow(ll x, ll y, ll mod) {
        ll res = 1;
        while (y) {
                if (y % 2) { res *= x; res %= mod; }
                x = x * x % mod; y /= 2;
        }
        return res;
}*/
/*ll nCk(ll n, ll k) {
        ll a = 1, b = 1;
        for (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; }
        for (int h = 1; h <= k; h++) { b *= h; b %= mod; }
        return a * modpow(b, mod - 2) % mod;
}*/
// printf("%.10f\\n", n);

ll par[2134567];   // 親
ll depth[2134567]; // 木の深さ
// n要素で初期化
void init(ll n) {
  for (int i = 0; i <= n; i++) {
    par[i] = i;
    depth[i] = 0;
  }
}
// 木の根を求める
ll find(ll x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
// xとyの属する集合を併合
void unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  }
  if (depth[x] < depth[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (depth[x] == depth[y])
      depth[x]++;
  }
}
// xとyが同じ集合に属するか否か
bool same(ll x, ll y) { return find(x) == find(y); }
ll test[2345678];
signed main() {
  string s, t;
  ll n, m, k, a, b, c, cnt = 0, ans = 0;
  cin >> n >> m;
  init(n + m);
  rep(i, m) {
    cin >> a >> b;
    unite(a, b);
  }
  rep2(i, n) {
    test[find(i)]++;
    ans = max(ans, test[find(i)]);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// #include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep2(i, x) for (ll i = 1; i <= x; i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;

/*bool IsPrime(ll num) {
        if (num < 2) return false;
        else if (num == 2) return true;
        else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
        double sqrtNum = sqrt(num);
        for (int i = 3; i <= sqrtNum; i += 2) {
                if (num % i == 0) { // 素数ではない
                        return false;
                }
        }
        // 素数である
        return true;
}*/
/*ll gcd(ll a, ll b) {
        if (a < b)swap(a, b);
        if (a % b == 0)return b;
        return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
        return a / gcd(a, b) * b;
}*/
/*ll keta(ll n) {
        ll res = 0;
        while (n >= 1) {
                res += n % 10; n /= 10;
        }
        return res;
}*/
/*ll modpow(ll x, ll y, ll mod) {
        ll res = 1;
        while (y) {
                if (y % 2) { res *= x; res %= mod; }
                x = x * x % mod; y /= 2;
        }
        return res;
}*/
/*ll nCk(ll n, ll k) {
        ll a = 1, b = 1;
        for (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; }
        for (int h = 1; h <= k; h++) { b *= h; b %= mod; }
        return a * modpow(b, mod - 2) % mod;
}*/
// printf("%.10f\\n", n);

ll par[213456];   // 親
ll depth[213456]; // 木の深さ
// n要素で初期化
void init(ll n) {
  for (int i = 0; i <= n; i++) {
    par[i] = i;
    depth[i] = 0;
  }
}
// 木の根を求める
ll find(ll x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
// xとyの属する集合を併合
void unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  }
  if (depth[x] < depth[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (depth[x] == depth[y])
      depth[x]++;
  }
}
// xとyが同じ集合に属するか否か
bool same(ll x, ll y) { return find(x) == find(y); }
ll test[234567];
signed main() {
  string s, t;
  ll n, m, k, a, b, c, cnt = 0, ans = 0;
  cin >> n >> m;
  init(n + m);
  rep(i, m) {
    cin >> a >> b;
    unite(a, b);
  }
  rep2(i, n) {
    test[find(i)]++;
    ans = max(ans, test[find(i)]);
  }
  cout << ans << endl;
  return 0;
}