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

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
/****************************/
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define qi queue<int>
#define mii map<int, int>
#define msi map<string, int>
/****************************/
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep3(i, a, n) for (ll i = (a); i < (n); i++)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define pi 3.1415926535897932384626433832795
#define zero(n) memset(n, 0, sizeof(n));
#define SZ(n) n.size()
#define ss st.st
#define sn st.nd
#define mod 1000000007
/****************************/
using namespace std;
int n, m;
vi g[200010];
bool vis[200010];
ll ans = 0, res = 0;
;
void dfs(int x) {
  vis[x] = true;
  res++;
  rep(i, g[x].size()) {
    if (!vis[g[x][i]]) {
      dfs(g[x][i]);
    }
  }
  return;
}
int main() {
  zero(vis);
  cin >> n >> m;
  ll now = 0;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  rep(i, n) {
    if (!vis[i]) {
      res = 0;
      dfs(i);
      ans = max(ans, res);
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
/****************************/
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define qi queue<int>
#define mii map<int, int>
#define msi map<string, int>
/****************************/
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep3(i, a, n) for (ll i = (a); i < (n); i++)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define pi 3.1415926535897932384626433832795
#define zero(n) memset(n, 0, sizeof(n));
#define SZ(n) n.size()
#define ss st.st
#define sn st.nd
#define mod 1000000007
/****************************/
using namespace std;
int n, m;
vi g[200010];
bool vis[200010];
ll ans = 0, res = 0;
;
void dfs(int x) {
  vis[x] = true;
  res++;
  rep(i, g[x].size()) {
    if (!vis[g[x][i]]) {
      dfs(g[x][i]);
    }
  }
  return;
}
int main() {
  zero(vis);
  cin >> n >> m;
  ll now = 0;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    bool flaga = false, flagb = false;
    rep(j, g[a].size()) {
      if (g[a][j] == b) {
        flaga = true;
        break;
      }
    }
    if (!flaga)
      g[a].pb(b);
    rep(j, g[b].size()) {
      if (g[b][j] == a) {
        flagb = true;
        break;
      }
    }
    if (!flagb)
      g[b].pb(a);
  }
  rep(i, n) {
    if (!vis[i]) {
      res = 0;
      dfs(i);
      ans = max(ans, res);
    }
  }
  cout << ans;
  return 0;
}