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

// #include <bits/stdc++.h>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <set>
#include <sstream>
#include <stack>
#include <string>
// #include <array>
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <map>
#include <memory>
#include <queue>
#include <vector>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned int UINT;
typedef unsigned long long ull;
typedef pair<int, int> pdi;
typedef pair<ll, int> pli;

int const maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
inline int lc(int x) { return x << 1; }
inline int rc(int x) { return x << 1 | 1; }

ll cnt[maxn];
int fa[maxn];

void init(int n) {
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
  }
}

int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }

void unio(int x, int y) {
  int fx = find(x);
  int fy = find(y);
  if (fx != fy) {
    fa[fx] = fy;
  }
}

int main(void) {
  FAST_IO;

  int n, tot = 0, m;
  ll ans = 0;
  cin >> n >> m;
  init(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    unio(x, y);
  }
  for (int i = 1; i <= n; i++) {
    int fx = find(i);
    if (fx == i) {
      tot++;
    }
    cnt[fx]++;
  }
  for (int i = 1; i <= n; i++) {
    ans = max(ans, cnt[i]);
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

// #include <bits/stdc++.h>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <set>
#include <sstream>
#include <stack>
#include <string>
// #include <array>
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <map>
#include <memory>
#include <queue>
#include <vector>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned int UINT;
typedef unsigned long long ull;
typedef pair<int, int> pdi;
typedef pair<ll, int> pli;

int const maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
inline int lc(int x) { return x << 1; }
inline int rc(int x) { return x << 1 | 1; }

ll cnt[maxn];
int fa[maxn];

void init(int n) {
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
  }
}

int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }

void unio(int x, int y) {
  int fx = find(x);
  int fy = find(y);
  if (fx != fy) {
    fa[fx] = fy;
  }
}

int main(void) {
  FAST_IO;

  int n, tot = 0, m;
  ll ans = 0;
  cin >> n >> m;
  init(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    unio(x, y);
  }
  for (int i = 1; i <= n; i++) {
    int fx = find(i);
    if (fx == i) {
      tot++;
    }
    cnt[fx]++;
  }
  // cout << tot <<endl;
  ll one = 0;
  for (int i = 1; i <= n; i++) {
    if (cnt[i] == 1) {
      one = 1;
      cnt[i]--;
      tot--;
    }
  }
  ll f = 0;
  for (int i = 1; i <= n; i++) {
    ans += cnt[i] / tot;
    f = max(f, cnt[i] % tot);
  }
  cout << ans + max(one, f) << endl;

  return 0;
}