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

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define JAM(x) cout << #x << " = " << x << "\\n"
#define II()                                                                   \\
  ({                                                                           \\
    int a;                                                                     \\
    read(a);                                                                   \\
    a;                                                                         \\
  })
#define LL()                                                                   \\
  ({                                                                           \\
    ll a;                                                                      \\
    read(a);                                                                   \\
    a;                                                                         \\
  })
#define DD()                                                                   \\
  ({                                                                           \\
    double a;                                                                  \\
    scanf("%lf", &a);                                                          \\
    a;                                                                         \\
  })

// Fast Reader
template <class T> inline bool read(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-')
      sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PII;
typedef vector<PII> VPI;

const int INF = 2e9;
const int MX = 100005;
const int MOD = 1000000007;
const double PI = acos(-1.0);

long long int power(long long int a, long long int b) {
  a %= MOD;
  long long int res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
bool isprime(int n) {
  if (n == 1)
    return 0;
  int i;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

/*_______________________________________________*/
vector<int> par, cont, rnk;
int findp(int x) {
  if (par[x] == -1)
    return x;
  return findp(par[x]);
}

void merge(int x, int y) {
  int px, py;
  px = findp(x);
  py = findp(y);
  if (px == py)
    return;
  if (rnk[px] < rnk[py]) {
    cont[py] += cont[px];
    par[px] = py;
  } else {
    cont[px] += cont[py];
    par[py] = px;
  }
  if (rnk[px] == rnk[py])
    rnk[px]++;
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, i;
  cin >> n >> m;
  cont.resize(n + 1, 1);
  par.resize(n + 1, -1);
  rnk.resize(n + 1, 0);
  for (i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    merge(x, y);
  }
  int ans = 0;
  for (i = 1; i <= n; i++) {
    ans = max(cont[i], ans);
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define JAM(x) cout << #x << " = " << x << "\\n"
#define II()                                                                   \\
  ({                                                                           \\
    int a;                                                                     \\
    read(a);                                                                   \\
    a;                                                                         \\
  })
#define LL()                                                                   \\
  ({                                                                           \\
    ll a;                                                                      \\
    read(a);                                                                   \\
    a;                                                                         \\
  })
#define DD()                                                                   \\
  ({                                                                           \\
    double a;                                                                  \\
    scanf("%lf", &a);                                                          \\
    a;                                                                         \\
  })

// Fast Reader
template <class T> inline bool read(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-')
      sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar())
    x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PII;
typedef vector<PII> VPI;

const int INF = 2e9;
const int MX = 100005;
const int MOD = 1000000007;
const double PI = acos(-1.0);

long long int power(long long int a, long long int b) {
  a %= MOD;
  long long int res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
bool isprime(int n) {
  if (n == 1)
    return 0;
  int i;
  for (i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

/*_______________________________________________*/
vector<int> par, cont;
int findp(int x) {
  if (par[x] == -1)
    return x;
  return findp(par[x]);
}

void merge(int x, int y) {
  int px, py;
  px = findp(x);
  py = findp(y);
  if (px == py)
    return;
  cont[py] += cont[px];
  par[px] = py;
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, i;
  cin >> n >> m;
  cont.resize(n + 1, 1);
  par.resize(n + 1, -1);
  for (i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    merge(x, y);
  }
  int ans = 0;
  for (i = 1; i <= n; i++) {
    ans = max(cont[i], ans);
  }
  cout << ans;
}