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

// -- Nitin
#include <bits/stdc++.h>
#define pb push_back
#define mkp make_pair
#define llu unsigned long long int
#define ll long long
#define ld long double
#define foi(a, n) for (int i = a; i < (int)n; i++)
#define foj(a, n) for (int j = a; j < (int)n; j++)
#define foin(a, n) for (int i = n; i >= a; i--)
#define fojn(a, n) for (int j = n; j >= a; j--)
#define bug(x) cerr << #x << " is " << x << endl;
#define isp2(x) x && (!(x & (x - 1)))
#define ln '\\n'
#define ee 1000000007
#define eem 998244353
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define cases                                                                  \\
  int tests;                                                                   \\
  cin >> tests;                                                                \\
  while (tests--)

using namespace std;

// Union find
#define MAXN 200002
int parent[MAXN], sz[MAXN];
int fset(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = fset(parent[v]);
}
void mkset(int v) {
  parent[v] = v;
  sz[v] = 1;
}
void usets(int a, int b) {
  a = fset(a);
  b = fset(b);
  if (a != b) {
    if (sz[a] < sz[b])
      swap(a, b);
    parent[b] = a;
    sz[a] += sz[b];
  }
}

int main() {
  fio;
  ll n, m, k;

  //~ cases

  {
    cin >> n >> m;
    ll a, b;
    for (int i = 1; i <= n; i++) {
      mkset(i);
    }
    foi(0, m) {
      cin >> a >> b;
      usets(a, b);
    }
    ll mx = 0;
    for (int i = 1; i <= n; i++) {
      if (parent[i] == i)
        mx = max(mx, (ll)sz[i]);
    }
    cout << mx;
    cout << "\\n";
  }
}


Error: 0

Fixed submission:

// -- Nitin
#include <bits/stdc++.h>
#define pb push_back
#define mkp make_pair
#define llu unsigned long long int
#define ll long long
#define ld long double
#define foi(a, n) for (int i = a; i < (int)n; i++)
#define foj(a, n) for (int j = a; j < (int)n; j++)
#define foin(a, n) for (int i = n; i >= a; i--)
#define fojn(a, n) for (int j = n; j >= a; j--)
#define bug(x) cerr << #x << " is " << x << endl;
#define isp2(x) x && (!(x & (x - 1)))
#define ln '\\n'
#define ee 1000000007
#define eem 998244353
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define cases                                                                  \\
  int tests;                                                                   \\
  cin >> tests;                                                                \\
  while (tests--)

using namespace std;

// Union find
#define MAXN 100001
int parent[MAXN], sz[MAXN];
int fset(int v) {
  if (v == parent[v])
    return v;
  return parent[v] = fset(parent[v]);
}
void mkset(int v) {
  parent[v] = v;
  sz[v] = 1;
}
void usets(int a, int b) {
  a = fset(a);
  b = fset(b);
  if (a != b) {
    if (sz[a] < sz[b])
      swap(a, b);
    parent[b] = a;
    sz[a] += sz[b];
  }
}

int main() {
  fio;
  ll n, m, k;

  //~ cases

  {
    cin >> n >> m;
    ll a, b;
    for (int i = 1; i <= n; i++) {
      mkset(i);
    }
    foi(0, m) {
      cin >> a >> b;
      usets(a, b);
    }
    map<ll, ll> s;
    ll mx = 0;
    for (int i = 1; i <= n; i++) {
      s[parent[i]]++;
      mx = max(s[parent[i]], mx);
    }
    cout << mx;
    cout << "\\n";
  }
}