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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator> // std::back_inserter
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
const double PI = acos(-1);
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
const int mod = 1000000007;
const ll inf = 1e18 + 1;
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); };
// fixed << setprecision(2)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

/*コンビネーション*/

// ll f[101010], rf[101010];
// ll inv(ll x) {
//	ll res = 1;
//	ll k = mod - 2;
//	ll y = x;
//	while (k) {
//		if (k & 1) res = (res * y) % mod;
//		y = (y * y) % mod;
//		k /= 2;
//	}
//	return res;
// }
// void init() {
//	f[0] = 1;
//	for (ll i = 1; i < 101010;i++) f[i] = (f[i - 1] * i) % mod;
//	for(ll i = 0; i < 101010; i++) rf[i] = inv(f[i]);
// }
////---------------------------------------------------------------------------------------------------
// ll C(int n, int k) {
//	ll a = f[n]; // = n!
//	ll b = rf[n - k]; // = (n-k)!
//	ll c = rf[k]; // = k!
//
//	ll bc = (b * c) % mod;
//
//	return (a * bc) % mod;
// }

/*コンビネーション*/

/*Union Find*/
#define MAX_N 200000
#define MAX_M 200000
vector<ll> par(MAX_N);
vector<ll> ran(MAX_N);
void init(ll N) {
  rep(i, N) {
    par[i] = i;
    ran[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 (ran[x] < ran[y]) {
    par[x] = y;

  } else {
    par[y] = x;
    if (ran[x] == ran[y])
      ran[x]++;
  }
}
bool same(ll x, ll y) { return find(x) == find(y); }
/*Union Find*/
// 小文字=大文字+32
int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(M), B(M);
  init(N);
  rep(i, M) cin >> A[i] >> B[i];
  rep(i, M) { unite(A[i], B[i]); }
  vector<ll> m(N);
  rep(i, N) { m[find(i)]++; }
  sort(m.begin(), m.end());
  reverse(m.begin(), m.end());
  cout << m[0];
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const int mod = 1000000007;
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); };
// fixed << setprecision(2)
bool Array_check(ll y, ll x, ll d, ll w) {
  if (x < 0 || x >= w || y < 0 || y >= d) {
    return false;
  } else {
    return true;
  }
}

// 小文字=大文字+32
struct friends {

  int key;
  int next;
};

int main() {
  int n;
  int m;
  cin >> n >> m;
  vector<friends> a(m + m);
  queue<ll> q;
  int ha[200000];
  vector<bool> check(n, true);
  rep(i, 200000) { ha[i] = -1; }
  rep(i, m + m) {
    cin >> a[i].key;
    a[i].key--;
    a[i].next = ha[a[i].key];
    ha[a[i].key] = i;
  }
  int i = 0;
  int v;
  int ans = 0; // グループの人数
  int tmp = 1;
  while (i != n) {
    q.push(i);
    tmp = 1;
    if (check[i]) {
      while (!q.empty()) {
        v = ha[q.front()];
        q.pop();
        while (v != -1) {
          if (check[a[v].key]) {
            check[a[v].key] = false;
            tmp++;
            q.push(v);
          }
          v = a[v].next;
        }
      }
      ans = max(ans, tmp);
    } else {
      q.pop();
    }
    i++;
  }
  cout << ans;
}