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

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;
class UnionFind {
private:
  size_t num;
  vector<size_t> par;
  vector<size_t> sizes;
  vector<size_t> rank;

public:
  UnionFind(size_t num = 0) : num(num) {
    par.resize(num);
    sizes.resize(num);
    rank.resize(num);
    for (size_t i = 0; i < num; i++) {
      par[i] = i;
      sizes[i] = 1;
      rank[i] = 0;
    }
  }
  ~UnionFind() {}

  size_t root(size_t x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }

  void unite(size_t x, size_t y) {
    size_t rx = root(x);
    size_t ry = root(y);
    if (rx == ry)
      return;
    if (rank[rx] < rank[ry]) {
      par[rx] = ry;
    } else {
      par[ry] = rx;
      if (rank[rx] == rank[ry]) {
        rank[rx]++;
      }
    }
    size_t temp = sizes[rx] + sizes[ry];
    sizes[rx] = temp;
    sizes[ry] = temp;
  }

  size_t get_size(size_t x) { return sizes[root(x)]; }

  bool same(size_t x, size_t y) {
    size_t rx = root(x);
    size_t ry = root(y);
    return rx == ry;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  size_t n, m;
  cin >> n >> m;
  UnionFind u(n);
  rep(i, m) {
    size_t a, b;
    cin >> a >> b;
    a--;
    b--;
    u.unite(a, b);
  }

  size_t max_size = 0;
  rep(i, n) { max_size = max(max_size, u.get_size(i)); }
  cout << max_size << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;
class UnionFind {
private:
  size_t num;
  vector<size_t> par;
  vector<size_t> sizes;
  vector<size_t> rank;

public:
  UnionFind(size_t num = 0) : num(num) {
    sizes.resize(num);
    rank.resize(num);
    for (size_t i = 0; i < num; i++) {
      par[i] = i;
      sizes[i] = 1;
      rank[i] = 0;
    }
  }
  ~UnionFind() {}

  size_t root(size_t x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }

  void unite(size_t x, size_t y) {
    size_t rx = root(x);
    size_t ry = root(y);
    if (rx == ry)
      return;
    if (rank[rx] < rank[ry]) {
      par[rx] = ry;
    } else {
      par[ry] = rx;
      if (rank[rx] == rank[ry]) {
        rank[rx]++;
      }
    }
    size_t temp = sizes[rx] + sizes[ry];
    sizes[rx] = temp;
    sizes[ry] = temp;
  }

  size_t get_size(size_t x) { return sizes[root(x)]; }

  bool same(size_t x, size_t y) {
    size_t rx = root(x);
    size_t ry = root(y);
    return rx == ry;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  UnionFind u(n);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    u.unite(a, b);
  }

  ll max_size = 0;
  rep(i, n) { max_size = max(max_size, (ll)u.get_size(i)); }
  cout << max_size << endl;
  return 0;
}