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

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string>
#define ll long long
#define F first
#define S second
#define pb push_back
#define ld long double
#define pll pair<long long, long long>
#define pld pair<long double, long double>
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
ll mod = 1000000007;
ll INF = 1e18;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll fastexp(ll x, ll n) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result *= x;
    x *= x;
    n = n / 2;
  }
  return result;
}

ll modularExp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    n = n / 2;
  }
  return (result) % mod;
}
bool isPrime(ll n) {
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
bool mycomp(pll &a, pll &b) {
  if (a.F != b.F)
    return a.F < b.F;
  else
    return a.S < b.S;
}
set<ll> v[200005];
vector<ll> visited(200005);
ll dfs(ll node) {
  visited[node] = 1;
  ll cnt = 1;
  for (auto it : v[node]) {
    if (!visited[it])
      cnt += dfs(it);
  }
  return cnt;
}
void solve() {
  ll n, m;
  cin >> n >> m;
  ll i, j;
  for (i = 1; i <= m; i++) {
    ll x, y;
    cin >> x >> y;
    v[x].insert(y);
    v[y].insert(x);
  }
  ll ans = 0;
  for (i = 1; i <= n; i++) {
    if (!visited[i])
      ans = max(ans, dfs(i));
  }
  cout << ans;
}

int main() {
  ios;
  ll t;
  //    cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string>
#define ll long long
#define F first
#define S second
#define pb push_back
#define ld long double
#define pll pair<long long, long long>
#define pld pair<long double, long double>
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
ll mod = 1000000007;
ll INF = 1e18;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll fastexp(ll x, ll n) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result *= x;
    x *= x;
    n = n / 2;
  }
  return result;
}

ll modularExp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    n = n / 2;
  }
  return (result) % mod;
}
bool isPrime(ll n) {
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
bool mycomp(pll &a, pll &b) {
  if (a.F != b.F)
    return a.F < b.F;
  else
    return a.S < b.S;
}
vector<ll> par(200005), sz(200005, 1);
ll froot(ll x) {
  ll y = x;
  while (x != par[x])
    x = par[x];
  par[y] = x;
  return x;
}
void solve() {
  ll n, m;
  cin >> n >> m;
  ll i, j;
  for (i = 1; i <= n; i++)
    par[i] = i;
  for (i = 1; i <= m; i++) {
    ll x, y;
    cin >> x >> y;
    ll rx = froot(x);
    ll ry = froot(y);
    if (rx != ry) {
      par[rx] = ry;
      sz[ry] += sz[rx];
    }
  }
  ll ans = 0;
  for (i = 1; i <= n; i++) {
    if (i == froot(i))
      ans = max(ans, sz[i]);
  }
  cout << ans;
}

int main() {
  ios;
  ll t;
  //    cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
}