Problem p02573 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORR(i, a, b) for (int i = a; i < b; i++)
#define fast                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define print(a)                                                               \
  for (auto x : a)                                                             \
    cout << x << ' ';                                                          \
  cout << endl;
#define printM(b)                                                              \
  for (auto y : b) {                                                           \
    print(y);                                                                  \
  }
#define printP(p) cout << p.first << ' ' << p.second << endl;
typedef long long int ll;
typedef unsigned long long int ull;

template <typename T> T cmax(T x, T y) { return (x > y) ? x : y; }

/*
5 4
1 2
2 3
4 1
3 5



1 2 4 3 5

a->b->c



*/

// User function template for C++

vector<int> parent;
int find(int x) { return parent[x] == x ? x : find(parent[x]); }

void solve() {

  int n, m, t1, t2;
  cin >> n >> m;
  parent.resize(n + 1, 0);

  FOR(i, n + 1)
  parent[i] = i;

  for (int i = 0; i < m; i++) {
    cin >> t1 >> t2;

    int x, y;
    x = find(t1);
    y = find(t2);
    if (x != y) {

      int mn = min(x, y);
      int mx = max(x, y);

      parent[mx] = mn;
    }
  }

  unordered_map<int, int> mp;

  int ans = 0;
  for (int i : parent) {
    int j = find(i);
    mp[j]++;
    ans = max(ans, mp[j]);
  }
  // print(parent);
  mp.clear();
  parent.clear();

  cout << ans;
}

int main() {

  fast;
  int t = 1;
  // cin>>t;
  while (t--) {

    solve();
    cout << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORR(i, a, b) for (int i = a; i < b; i++)
#define fast                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define print(a)                                                               \
  for (auto x : a)                                                             \
    cout << x << ' ';                                                          \
  cout << endl;
#define printM(b)                                                              \
  for (auto y : b) {                                                           \
    print(y);                                                                  \
  }
#define printP(p) cout << p.first << ' ' << p.second << endl;
typedef long long int ll;
typedef unsigned long long int ull;

template <typename T> T cmax(T x, T y) { return (x > y) ? x : y; }

/*
5 4
1 2
2 3
4 1
3 5



1 2 4 3 5

a->b->c



*/

// User function template for C++

vector<int> parent;
int find(int x) { return parent[x] == x ? x : parent[x] = find(parent[x]); }

void solve() {

  int n, m, t1, t2;
  cin >> n >> m;
  parent.resize(n + 1, 0);

  FOR(i, n + 1)
  parent[i] = i;

  for (int i = 0; i < m; i++) {
    cin >> t1 >> t2;

    int x, y;
    x = find(t1);
    y = find(t2);
    if (x != y) {

      int mn = min(x, y);
      int mx = max(x, y);

      parent[mx] = mn;
    }
  }

  unordered_map<int, int> mp;

  int ans = 0;
  for (int i : parent) {
    int j = find(i);
    mp[j]++;
    ans = max(ans, mp[j]);
  }
  // print(parent);
  mp.clear();
  parent.clear();

  cout << ans;
}

int main() {

  fast;
  int t = 1;
  // cin>>t;
  while (t--) {

    solve();
    cout << endl;
  }

  return 0;
}