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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
ll mod = 1000000007;
const int inf = 2147483647;
const ll MAX_N = 2100000;
const ll MAX_W = 2010000;

/*************ここから**********************/
class union_find {
public:
  ll N;
  ll par[MAX_N];
  ll rank[MAX_N];

  union_find(void) { init(); };
  union_find(ll n) : N(n) { init(); }
  void init() {
    for (int i = 0; i < N; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }
  // xの始点を探す
  ll find(ll x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  // グループを結合
  void unite(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  // 始点の親が同じか
  bool same(ll x, ll y) { return find(x) == find(y); }
  // 引数xの含まれるグループを表示
  void print_tree(ll x) {
    string str;
    str = "{";
    for (int i = 0; i < N; i++) {
      if (same(x, i)) {
        str = str + to_string(i) + ",";
      }
    }
    str[str.size() - 1] = '}';
    cout << str << endl;
  }
  // すべてのグループを表示
  void print_all(void) {
    vector<bool> is_printed(N, false);
    for (int i = 0; i < N; i++) {
      if (!is_printed[i]) {
        print_tree(i);
        for (int j = 0; j < N; j++) {
          if (same(i, j))
            is_printed[j] = true;
        }
      }
    }
    return;
  }
};

/**************ここまで*************************/
int main(void) {
  ll n, m;
  cin >> n >> m;
  union_find uf(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }
  vector<ll> group(n);
  for (int i = 0; i < n; i++) {
    group[uf.find(i)]++;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, group[i]);
    //    cout << "i= " <<i << "group:" << group[i] << endl;
  }
  // uf.print_all();
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
ll mod = 1000000007;
const int inf = 2147483647;
const int MAX_N = 21000;
const int MAX_W = 20100;

/*************ここから**********************/
class union_find {
public:
  ll N;
  ll par[MAX_N];
  ll rank[MAX_N];

  union_find(void) { init(); };
  union_find(ll n) : N(n) { init(); }
  void init() {
    for (int i = 0; i < N; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }
  // xの始点を探す
  ll find(ll x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  // グループを結合
  void unite(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  // 始点の親が同じか
  bool same(ll x, ll y) { return find(x) == find(y); }
  // 引数xの含まれるグループを表示
  void print_tree(ll x) {
    string str;
    str = "{";
    for (int i = 0; i < N; i++) {
      if (same(x, i)) {
        str = str + to_string(i) + ",";
      }
    }
    str[str.size() - 1] = '}';
    cout << str << endl;
  }
  // すべてのグループを表示
  void print_all(void) {
    vector<bool> is_printed(N, false);
    for (int i = 0; i < N; i++) {
      if (!is_printed[i]) {
        print_tree(i);
        for (int j = 0; j < N; j++) {
          if (same(i, j))
            is_printed[j] = true;
        }
      }
    }
    return;
  }
};

/**************ここまで*************************/
int main(void) {
  ll n, m;
  cin >> n >> m;
  union_find uf(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }
  vector<ll> group(n);
  for (int i = 0; i < n; i++) {
    group[uf.find(i)]++;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, group[i]);
    //    cout << "i= " <<i << "group:" << group[i] << endl;
  }
  // uf.print_all();
  cout << ans << endl;
}