Problem p02573 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <numeric>
#define ll long long
#define ull long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll, ll>
#define dd long long
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ren(i, n) for (ll i = n - 1; i >= 0; i--)
#define forn(i, a, b) for (ll i = a; i >= b; i--)
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld", &x)
#define clr(x, val) memset(x, val, sizeof(x))
#define pr(x) printf("%lld\\n", x)
#define gc getchar
#define pdd pair<dd, dd>
#define prec(x) cout << fixed << setprecision(x)
#define fre freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
using namespace std;
ll a[200005];
ll suff[200005];
ll par[200005];
map<ll, ll> help;
ll find_par(ll v) {
  if (v == par[v])
    return v;
  return find_par(par[v]);
}
void dsu_union(ll x, ll y) {
  ll a = find_par(x);
  ll b = find_par(y);

  if (a == b)
    return;

  if (help[a] > help[b]) {
    par[b] = a;
    help[a] += help[b];
    help[b] = 0;
  } else {
    help[b] += help[a];
    help[a] = 0;
    par[a] = b;
  }
}
int main() {
  boost;
  ll n, m;
  ll mod = 1e9 + 7;
  ll ans = 0;
  cin >> n >> m;
  for (ll i = 1; i <= n; i++)
    par[i] = i, help[i] = 1;

  while (m--) {
    ll x, y;
    cin >> x >> y;
    dsu_union(x, y);
  }
  ans = 0;

  for (ll i = 1; i <= n; i++)
    ans = max(ans, help[i]);

  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <numeric>
#define ll long long
#define ull long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll, ll>
#define dd long long
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ren(i, n) for (ll i = n - 1; i >= 0; i--)
#define forn(i, a, b) for (ll i = a; i >= b; i--)
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld", &x)
#define clr(x, val) memset(x, val, sizeof(x))
#define pr(x) printf("%lld\\n", x)
#define gc getchar
#define pdd pair<dd, dd>
#define prec(x) cout << fixed << setprecision(x)
#define fre freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
using namespace std;
ll a[200005];
ll suff[200005];
ll par[200005];

ll find_par(ll v) {
  if (v == par[v])
    return v;
  return find_par(par[v]);
}
void dsu_union(ll x, ll y) {
  ll a = find_par(x);
  ll b = find_par(y);

  if (a == b)
    return;

  par[a] = b;
}
int main() {
  ll n, m;
  ll mod = 1e9 + 7;
  ll ans = 0;
  cin >> n >> m;
  for (ll i = 1; i <= n; i++)
    par[i] = i;
  map<ll, ll> help;
  while (m--) {
    ll x, y;
    cin >> x >> y;
    dsu_union(x, y);
  }
  ans = 0;

  for (ll i = 1; i <= n; i++)
    help[find_par(i)]++;

  for (ll i = 1; i <= n; i++)
    ans = max(ans, help[i]);

  cout << ans;
}