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

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define in insert
#define sz(a) (int)(a).size()
#define all(x) (x).begin(), (x).end()
#define fore(i, a, b) for (ll i = a; i < b; i += 1)
#define forr(i, a) for (ll i = a; i >= 0; i -= 1)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define ford(i, a) for (auto i = a.begin(); i != a.end(); i++)
#define F first
#define S second
#define clr(a, val) memset(a, val, sizeof(a))
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define endl "\\n"
#define printV(arr)                                                            \\
  fore(x, 0, sz(arr)) cout << arr[x] << " ";                                   \\
  cout << endl;
#define printR(arr, a, b)                                                      \\
  fore(x, a, b) cout << arr[x] << " ";                                         \\
  cout << endl;
#define setp(x) fixed << setprecision(x)

using namespace std;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef vector<ii> vii;
typedef vector<lll> vlll;
typedef pair<string, int> si;
typedef pair<int, string> is;
typedef vector<string> vs;
const int MAX = 2e5 + 10;
const int MOD = 1000000007;
const double pi = acos(-1);
const int dx[] = {0, 1, -1, 0};
const int dy[] = {-1, 0, 0, 1};
const int dxc[] = {0, 2, -2, 0, 2, 2, -2, -2};
const int dyc[] = {-2, 0, 0, 2, -2, 2, -2, 2};

ll min(ll a, ll b) {
  if (a > b)
    return b;
  return a;
}
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}

ll gcd(ll a, ll b) {
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
bool isPrime(ll n) {
  if (n <= 1)
    return false;
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;
  for (ll i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}

set<ll> gen(ll n) {
  set<ll> divs;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      divs.insert(i);
      divs.insert(n / i);
    }
  }
  return divs;
}

map<ll, int> fact(ll n) {
  map<ll, int> F;
  for (ll p = 2; p * p <= n; p++) {
    while (n % p == 0) {
      F[p]++;
      n /= p;
    }
  }
  if (n > 1)
    F[n]++;
  return F;
}

ll sum(ll n) { return (n * (n + 1)) / 2; }

ll binaryExp(ll a, ll b, ll c) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll temp = binaryExp(a, b / 2, c);
    return (temp * temp) % c;
  } else {
    ll temp = binaryExp(a, b - 1, c);
    return (temp * a) % c;
  }
}

bool isPal(string &cad) {
  int p1 = sz(cad) - 1;
  fore(i, 0, sz(cad) / 2) {
    if (cad[i] != cad[p1])
      return false;
    p1--;
  }
  return true;
}

double dis(double x1, double y1, double x2, double y2) {
  double ff = pow(x2 - x1, 2) + pow(y2 - y1, 2);
  return sqrt(ff);
}

int parent[MAX];
int tam[MAX];
int canSets;

void init(int n) {
  canSets = n;
  fore(i, 0, n + 1) {
    parent[i] = i;
    tam[i] = 1;
  }
}
int find(int i) {
  parent[i] = (parent[i] == i) ? i : find(parent[i]);
  return parent[i];
}
void unionFind(int x, int y) {
  x = find(x);
  y = find(y);
  if (x != y) {
    canSets--;
    parent[x] = y;
    tam[y] += tam[x];
  }
}
int sizeOfSet(int i) { return tam[find(i)]; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  init(n);
  fore(i, 0, m) {
    int a, b;
    cin >> a >> b;
    unionFind(a, b);
  }
  int res = 0;
  fore(i, 1, n + 1) res = max(res, sizeOfSet(i));
  cout << res << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define in insert
#define sz(a) (int)(a).size()
#define all(x) (x).begin(), (x).end()
#define fore(i, a, b) for (ll i = a; i < b; i += 1)
#define forr(i, a) for (ll i = a; i >= 0; i -= 1)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define ford(i, a) for (auto i = a.begin(); i != a.end(); i++)
#define F first
#define S second
#define clr(a, val) memset(a, val, sizeof(a))
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define endl "\\n"
#define printV(arr)                                                            \\
  fore(x, 0, sz(arr)) cout << arr[x] << " ";                                   \\
  cout << endl;
#define printR(arr, a, b)                                                      \\
  fore(x, a, b) cout << arr[x] << " ";                                         \\
  cout << endl;
#define setp(x) fixed << setprecision(x)

using namespace std;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef vector<ii> vii;
typedef vector<lll> vlll;
typedef pair<string, int> si;
typedef pair<int, string> is;
typedef vector<string> vs;
const int MAX = 1e5 + 10;
const int MOD = 1000000007;
const double pi = acos(-1);
const int dx[] = {0, 1, -1, 0};
const int dy[] = {-1, 0, 0, 1};
const int dxc[] = {0, 2, -2, 0, 2, 2, -2, -2};
const int dyc[] = {-2, 0, 0, 2, -2, 2, -2, 2};

ll min(ll a, ll b) {
  if (a > b)
    return b;
  return a;
}
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}

ll gcd(ll a, ll b) {
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
bool isPrime(ll n) {
  if (n <= 1)
    return false;
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;
  for (ll i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}

set<ll> gen(ll n) {
  set<ll> divs;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      divs.insert(i);
      divs.insert(n / i);
    }
  }
  return divs;
}

map<ll, int> fact(ll n) {
  map<ll, int> F;
  for (ll p = 2; p * p <= n; p++) {
    while (n % p == 0) {
      F[p]++;
      n /= p;
    }
  }
  if (n > 1)
    F[n]++;
  return F;
}

ll sum(ll n) { return (n * (n + 1)) / 2; }

ll binaryExp(ll a, ll b, ll c) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll temp = binaryExp(a, b / 2, c);
    return (temp * temp) % c;
  } else {
    ll temp = binaryExp(a, b - 1, c);
    return (temp * a) % c;
  }
}

bool isPal(string &cad) {
  int p1 = sz(cad) - 1;
  fore(i, 0, sz(cad) / 2) {
    if (cad[i] != cad[p1])
      return false;
    p1--;
  }
  return true;
}

double dis(double x1, double y1, double x2, double y2) {
  double ff = pow(x2 - x1, 2) + pow(y2 - y1, 2);
  return sqrt(ff);
}
int padre[100010];

void init() { fore(i, 1, 200010) padre[i] = i; }
int find(int x) {
  if (x == padre[x])
    return x;
  else
    return padre[x] = find(padre[x]);
}
void Union(int x, int y) {
  int u = find(x), v = find(y);
  padre[v] = u;
}
bool same(int x, int y) { return find(x) == find(y); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  init();
  int n, m;
  cin >> n >> m;
  fore(i, 0, m) {
    int a, b;
    cin >> a >> b;
    if (b < a)
      swap(a, b);
    Union(a, b);
  }
  map<int, int> gg;
  fore(i, 1, n + 1) gg[padre[i]]++;
  int res = 1;
  ford(i, gg) res = max(res, i->S);
  cout << res << endl;

  return 0;
}