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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9 + 7)
#define INF (1LL << 60)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n) {
  ll s = sqrt(n);
  ll r = 0;
  for (ll i = 2; i <= s; i++) {
    if ((n % i) == 0) {
      r = 0;
      do {
        r++;
        n = n / i;
      } while ((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if (n > s) {
    prim.push_back({n, 1});
  }
}

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
  vector<int> cnt;
  UnionFind(int N) : par(N), cnt(N, 1) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

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

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

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

void solve() {
  ll N, M;
  cin >> N >> M;

  UnionFind tree(N);

  set<pair<ll, ll>> vvp;
  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    tree.unite(a, b);
  }

  ll ans = 0;

  rep(i, N) {
    ans = max((long long)tree.cnt[i], ans);
    //    cerr << i << ":" << tree.cnt[i] << endl;
  }

  cout << ans << endl;
}

int main(void) {
  // ll t; cin >> t; rep(i, t)
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9 + 7)
#define INF (1LL << 60)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n) {
  ll s = sqrt(n);
  ll r = 0;
  for (ll i = 2; i <= s; i++) {
    if ((n % i) == 0) {
      r = 0;
      do {
        r++;
        n = n / i;
      } while ((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if (n > s) {
    prim.push_back({n, 1});
  }
}

using Graph = vector<vector<int>>;
vector<bool> seen;
ll dfs(Graph G, ll n) {
  ll cnt = 0;
  if (seen[n] == true)
    return 0;
  seen[n] = true;
  rep(i, G[n].size()) { cnt += dfs(G, G[n][i]); }
  return cnt + 1;
}

void solve() {
  ll N, M;
  cin >> N >> M;
  Graph G(N);
  seen.assign(N, false);
  set<pair<ll, ll>> vvp;
  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    if (vvp.find({a, b}) == vvp.end()) {
      G[a].push_back(b);
      G[b].push_back(a);
      vvp.insert({a, b});
      vvp.insert({b, a});
    }
  }

  ll ans = 0;

  rep(i, N) {
    ll cnt = dfs(G, i);
    ans = max(cnt, ans);
    //    cerr << i << ":" << cnt << endl;
  }

  cout << ans << endl;
}

int main(void) {
  // ll t; cin >> t; rep(i, t)
  solve();
}