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

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define ll long long
#define mod 1000000007
#define fr first
#define sc second
#define vl vector<ll>
#define pll pair<ll, ll>
#define u_map unordered_map<ll, ll>

#define pi 3.141592653589793
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define printV(v)                                                              \\
  for (auto itr : v)                                                           \\
    cout << itr << " ";                                                        \\
  cout << endl;
#define printA(a, n)                                                           \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define printC cout << "yes" << endl;
#define printVP(v)                                                             \\
  for (auto itr : v)                                                           \\
    cout << itr.fr << " " << itr.sc << endl;
#define cinarr(a, n)                                                           \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];

vl parent;
vl rank1;

ll find(ll x) {
  if (parent[x] == -1)
    return x;
  else {
    parent[x] = find(parent[x]);
    return parent[x];
  }
}

void union1(ll x, ll y) {
  ll px = find(x);
  ll py = find(y);

  if (px == py)
    return;
  parent[py] = px;
  rank1[px] = rank1[px] + rank1[py];
}

void solve() {
  ll n, m;
  cin >> n >> m;

  parent = vl(n, -1);
  rank1 = vl(n, 1);

  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    union1(u, v);
  }

  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, rank1[i]);
  cout << ans << endl;
}

signed main() {
  ios;
  int testcases = 1;
  // cin >> testcases;

  while (testcases--) {
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define ll long long
#define mod 1000000007
#define fr first
#define sc second
#define vl vector<ll>
#define pll pair<ll, ll>
#define u_map unordered_map<ll, ll>

#define pi 3.141592653589793
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define printV(v)                                                              \\
  for (auto itr : v)                                                           \\
    cout << itr << " ";                                                        \\
  cout << endl;
#define printA(a, n)                                                           \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define printC cout << "yes" << endl;
#define printVP(v)                                                             \\
  for (auto itr : v)                                                           \\
    cout << itr.fr << " " << itr.sc << endl;
#define cinarr(a, n)                                                           \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];

vl parent;
vl rank1;

ll find(ll x) {
  if (parent[x] == -1)
    return x;
  else
    return find(parent[x]);
}

void union1(ll x, ll y) {
  ll px = find(x);
  ll py = find(y);
  if (px == py)
    return;
  parent[py] = px;
  rank1[px] = rank1[px] + rank1[py];
}

void solve() {
  ll n, m;
  cin >> n >> m;

  parent = vl(n, -1);
  rank1 = vl(n, 1);

  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    union1(u, v);
  }

  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, rank1[i]);
  cout << ans << endl;
}

signed main() {
  ios;
  int testcases = 1;
  // cin >> testcases;

  while (testcases--) {
    solve();
  }

  return 0;
}