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

#include <bits/stdc++.h>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

class UnionFind {
public:
  vector<int> root;
  vector<int> rank;
  vector<int> sizev;

  UnionFind();

  // 1-indexでアクセスすること想定
  UnionFind(int n) {

    for (int i = 0; i <= n; i++) {
      root.push_back(i);
      rank.push_back(1);
      sizev.push_back(1);
    }
  }

  int find(int n) {

    vector<int> update;
    int count = 0;

    while (n != root[n]) {

      update.push_back(n);
      n = root[n];
      count++;
    }

    for (int i = 0; i < count; i++) {
      root[update[i]] = n;
    }

    return n;
  }

  int size(int n) { return sizev[find(n)]; }

  void merge(int a, int b) {

    a = find(a);
    b = find(b);

    if (a != b) {

      if (rank[a] < rank[b]) {
        root[a] = b;
        sizev[b] += sizev[a];
      } else {
        root[b] = a;
        sizev[a] += sizev[b];

        if (rank[a] == rank[b]) {
          rank[a]++;
        }
      }
    }
  }
};

int main() {

  int n, m;
  cin >> n >> m;

  UnionFind uf(n); // 1-index

  rep(i, m) {
    int a, b;
    cin >> a >> b;

    uf.merge(a, b);
  }

  int ans = 0;
  vector<int> root(n + 1);

  rep(i, n) { root[uf.find(i + 1)]++; }

  rep(i, n) {
    if (root[i + 1] > ans) {
      ans = root[i + 1];
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

class UnionFind {
public:
  vector<int> root;
  vector<int> rank;
  vector<int> sizev;

  UnionFind();

  // 1-indexでアクセスすること想定
  UnionFind(int n) {

    for (int i = 0; i <= n; i++) {
      root.push_back(i);
      rank.push_back(1);
      sizev.push_back(1);
    }
  }

  int find(int n) {

    vector<int> update;
    int count = 0;

    while (n != root[n]) {

      update.push_back(n);
      n = root[n];
      count++;
    }

    for (int i = 0; i < count; i++) {
      root[update[i]] = n;
    }

    return n;
  }

  int size(int n) { return sizev[find(n)]; }

  void merge(int a, int b) {

    a = find(a);
    b = find(b);

    if (a != b) {

      if (rank[a] < rank[b]) {
        root[a] = b;
        sizev[b] += sizev[a];
      } else {
        root[b] = a;
        sizev[a] += sizev[b];

        if (rank[a] == rank[b]) {
          rank[a]++;
        }
      }
    }
  }
};

int main() {

  int n, m;
  cin >> n >> m;

  UnionFind uf(n); // 1-index

  rep(i, m) {
    int a, b;
    cin >> a >> b;

    uf.merge(a, b);
  }

  int groupNum = 0;
  int count = 0;
  vector<int> root(n + 1);

  rep(i, n) { root[uf.find(i + 1)]++; }

  rep(i, n) {
    if (root[i + 1] > 1) {
      groupNum++;
      count += root[i + 1];
    }
  }

  cout << (count + groupNum - 1) / groupNum << endl;

  return 0;
}