Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 2e5 + 10;
const ll MOD = 17;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void cal_factor() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = (factor[u - 1] * u) % MOD;
  }
}
ll C(ll n, ll k) {
  return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *
         qpow(factor[k], MOD - 2) % MOD;
}
int fa[MAXN];
int cnt[MAXN];
void init() {
  for (int u = 0; u < MAXN; u++) {
    fa[u] = u;
    cnt[u] = 1;
  }
}
int rfind(int r) {
  if (fa[r] == r)
    return r;
  int x = rfind(fa[r]);
  fa[r] = x;
  return x;
}
int runion(int a, int b) {
  int ta = rfind(a), tb = rfind(b);
  if (ta != tb) {
    fa[tb] = ta;
    cnt[ta] += cnt[tb];
  }
}
void solve() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  init();
  int n, m;
  cin >> n >> m;
  set<pair<int, int>> ex;
  for (int u = 0; u < m; u++) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    if (ex.count({a, b}))
      continue;
    ex.insert({a, b});
    runion(a, b);
  }
  vector<int> sz;
  int verify = 0;
  for (int u = 1; u <= n; u++) {
    if (fa[u] == u) {
      sz.push_back(cnt[u]);
      verify += cnt[u];
    }
  }
  // assert(verify==n);
  sort(sz.begin(), sz.end());
  int nsz = sz.size();
  int delta = 0;
  int grs = 0;
  for (int u = 0; u < nsz; u++) {
    if (delta + sz[u] <= 0)
      continue;
    int t = sz[u] + delta;
    grs += t;
    delta -= t;
  }
  cout << grs;
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 2e5 + 10;
const ll MOD = 17;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void cal_factor() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = (factor[u - 1] * u) % MOD;
  }
}
ll C(ll n, ll k) {
  return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *
         qpow(factor[k], MOD - 2) % MOD;
}
int fa[MAXN];
int cnt[MAXN];
void init() {
  for (int u = 0; u < MAXN; u++) {
    fa[u] = u;
    cnt[u] = 1;
  }
}
int rfind(int r) {
  if (fa[r] == r)
    return r;
  int x = rfind(fa[r]);
  fa[r] = x;
  return x;
}
void runion(int a, int b) {
  int ta = rfind(a), tb = rfind(b);
  if (ta != tb) {
    fa[tb] = ta;
    cnt[ta] += cnt[tb];
  }
}
void solve() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  init();
  int n, m;
  cin >> n >> m;
  set<pair<int, int>> ex;
  for (int u = 0; u < m; u++) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    if (ex.count({a, b}))
      continue;
    ex.insert({a, b});
    runion(a, b);
  }
  vector<int> sz;
  int verify = 0;
  for (int u = 1; u <= n; u++) {
    if (fa[u] == u) {
      sz.push_back(cnt[u]);
      verify += cnt[u];
    }
  }
  // assert(verify==n);
  sort(sz.begin(), sz.end());
  int nsz = sz.size();
  int delta = 0;
  int grs = 0;
  for (int u = 0; u < nsz; u++) {
    if (delta + sz[u] <= 0)
      continue;
    int t = sz[u] + delta;
    grs += t;
    delta -= t;
  }
  cout << grs;
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}