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

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

typedef long long int ll;
typedef pair<ll, ll> p_ll;
typedef vector<pair<ll, ll>>
    vec_p; // vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i)

#define ture ture
#define flase false
#define falg flag

#define REP(i, x) for (ll i = 0; i < (ll)(x); i++)
#define REPS(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define RREP(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (ll i = ((ll)(x)); i > 0; i--)
#define all(x) (x).begin(), (x).end()

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1; // llの最大9*10^18
const int INF = pow(2, 30) - 1;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

vec_p memo;

struct UnionFind {
  vector<long long> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  long long root(
      long long x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(long long x, long long y) { // xとyの木を併合
    long long rx = root(x);              // xの根をrx
    long long ry = root(y);              // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(long long x,
            long long y) { // 2つのデータx, yが属する木が同じならtrueを返す
    long long rx = root(x);
    long long ry = root(y);
    return rx == ry;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll N, M;
  string S;
  cin >> N >> M;

  UnionFind tree(N + 1);

  REP(i, M) {
    ll a, b;
    cin >> a >> b;
    tree.unite(a, b);
  }

  vector<ll> count(N + 1);
  REP(i, N + 1) { count.at(i) = 0; }

  REPS(i, N) {
    ll tmp = tree.root(i);

    count.at(tmp)++;
  }

  sort(count.begin(), count.end());
  reverse(count.begin(), count.end());

  cout << count.at(0) << endl;
}


Error: TLE

Fixed submission:

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

typedef long long int ll;
typedef pair<ll, ll> p_ll;
typedef vector<pair<ll, ll>>
    vec_p; // vector<pair<ll, ll>> pairs(n) ,pairs.at(i) = make_pair(i*i, i)

#define ture ture
#define flase false
#define falg flag

#define REP(i, x) for (ll i = 0; i < (ll)(x); i++)
#define REPS(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define RREP(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (ll i = ((ll)(x)); i > 0; i--)
#define all(x) (x).begin(), (x).end()

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1; // llの最大9*10^18
const int INF = pow(2, 30) - 1;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

vec_p memo;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll N, M;
  string S;
  cin >> N >> M;

  if (M == 0) {
    cout << 1 << endl;
  } else {
    vec_p pairs(M);
    ll tmp1, tmp2;
    REP(i, M) {
      cin >> tmp1 >> tmp2;
      if (tmp1 > tmp2) {
        swap(tmp1, tmp2);
      }
      pairs.at(i) = make_pair(tmp1, tmp2);
    }

    sort(pairs.begin(), pairs.end());

    vector<ll> group(N);

    REP(i, N) { group.at(i) = 0; }

    ll number = 1;

    REP(i, M) {
      ll A = pairs.at(i).first;
      ll B = pairs.at(i).second;

      if (group.at(A - 1) == 0 && group.at(B - 1) == 0) {
        group.at(A - 1) = number;
        group.at(B - 1) = number;
        number++;
      } else if (group.at(B - 1) == 0) {
        group.at(B - 1) = group.at(A - 1);
      } else if (group.at(A - 1) == 0) {
        group.at(A - 1) = group.at(B - 1);
      } else if (group.at(A - 1) == group.at(B - 1)) {
      } else {
        ll tmpA = group.at(A - 1);
        ll tmpB = group.at(B - 1);
        if (tmpA > tmpB) {
          swap(tmpA, tmpB);
        }
        memo.push_back(pair<ll, ll>(tmpA, tmpB));
      }
    }

    REP(i, N) {
      if (group.at(i) == 0) {
        group.at(i) = number;
        number++;
      }
    }

    vector<ll> count_num(number);

    REP(i, count_num.size()) {
      count_num.at(i) = count(group.begin(), group.end(), i + 1);
    }

    if (memo.size() > 0) {
      REP(i, memo.size()) {
        ll tmpA1 = memo.at(i).first;
        ll tmpB1 = memo.at(i).second;

        ll tmpC = count_num.at(tmpB1 - 1);
        count_num.at(tmpA1 - 1) += tmpC;
        count_num.at(tmpB1 - 1) -= tmpC;
      }
    }

    ll count1 = 0;
    ll max_c = 0;

    REP(i, count_num.size()) {
      if (count_num.at(i) > 0) {
        count1++;
      }
      max_c = max(max_c, count_num.at(i));
    }

    cout << max_c << endl;
  }
}