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

#include <bits/stdc++.h>
#define rep(i, f, n) for (int i = (f); (i) < (n); i++)
#define repe(i, f, n) for (int i = (f); (i) <= (n); i++)
using namespace std;
using ll = long long;
#define MOD (ll)1000000007
#define PI 3.14159265359
#define debug(x) cout << #x << " :: " << x << "\\n";
#define debug2(x, y)                                                           \\
  cout << #x << " :: " << x << "\\t" << #y << " :: " << y << "\\n";
#define debug3(x, y, z)                                                        \\
  cout << #x << " :: " << x << "\\t" << #y << " :: " << y << "\\t" << #z         \\
       << " :: " << z << "\\n";
#define P pair<int, int>
#define dvec vector<vector<ll>>
// printf("%.10f\\n")
// cout << fixed << setprecision(10);
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
ll INF = 1e+18;
int iINF = 1e9;

struct UnionFind {
  vector<int> par, rank, sz;

  UnionFind(int n) : par(n), rank(n, 0), sz(n, 1) {
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }
  void init(int n) {
    par.resize(n);
    rank.resize(n);
    sz.resize(n);
    for (int i = 0; i < n; ++i)
      par[i] = i, rank[i] = 0, sz[i] = 1;
  }

  int root(int n) {
    if (par[n] == n)
      return n;
    else {
      return par[n] = root(par[n]);
    }
  }

  bool is_same(int a, int b) { return root(a) == root(b); }

  int size(int n) { return sz[root(n)]; }

  bool merge(int a, int b) {
    int rt_a = root(a);
    int rt_b = root(b);
    if (rt_a == rt_b)
      return false;
    if (rank[rt_a] < rank[rt_b])
      swap(rt_a, rt_b);
    if (rank[rt_a] == rank[rt_b])
      ++rank[rt_a];
    par[rt_b] = rt_a;
    sz[rt_a] += sz[rt_b];
    return true;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);

  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    uf.merge(a, b);
  }

  int ans = 0;
  rep(i, 0, N) chmax(ans, uf.size(i));

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, f, n) for (int i = (f); (i) < (n); i++)
#define repe(i, f, n) for (int i = (f); (i) <= (n); i++)
using namespace std;
using ll = long long;
#define MOD (ll)1000000007
#define PI 3.14159265359
#define debug(x) cout << #x << " :: " << x << "\\n";
#define debug2(x, y)                                                           \\
  cout << #x << " :: " << x << "\\t" << #y << " :: " << y << "\\n";
#define debug3(x, y, z)                                                        \\
  cout << #x << " :: " << x << "\\t" << #y << " :: " << y << "\\t" << #z         \\
       << " :: " << z << "\\n";
#define P pair<int, int>
#define dvec vector<vector<ll>>
// printf("%.10f\\n")
// cout << fixed << setprecision(10);
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
ll INF = 1e+18;
int iINF = 1e9;

struct UnionFind {
  vector<int> par, rank, sz;

  UnionFind(int n) : par(n), rank(n, 0), sz(n, 1) {
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }
  void init(int n) {
    par.resize(n);
    rank.resize(n);
    sz.resize(n);
    for (int i = 0; i < n; ++i)
      par[i] = i, rank[i] = 0, sz[i] = 1;
  }

  int root(int n) {
    if (par[n] == n)
      return n;
    else {
      return par[n] = root(par[n]);
    }
  }

  bool is_same(int a, int b) { return root(a) == root(b); }

  int size(int n) { return sz[root(n)]; }

  bool merge(int a, int b) {
    int rt_a = root(a);
    int rt_b = root(b);
    if (rt_a == rt_b)
      return false;
    if (rank[rt_a] < rank[rt_b])
      swap(rt_a, rt_b);
    if (rank[rt_a] == rank[rt_b])
      ++rank[rt_a];
    par[rt_b] = rt_a;
    sz[rt_a] += sz[rt_b];
    return true;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind uf(N);

  rep(i, 0, M) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    uf.merge(a, b);
  }

  int ans = 0;
  rep(i, 0, M) chmax(ans, uf.size(i));

  cout << ans << endl;
}