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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ACC                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mod 1000000007
#define MAXN 10000001
// string alpha="abcdefghijklmnopqrstuvwxyz";
// int dx[]={1,-1,0,0};
// int dy[]={0,0,1,-1};
// int dx8[]={1,-1,0,0,1,1,-1,-1};
// int dy8[]={0,0,1,-1,1,-1,1,-1};
/*int spf[MAXN];
void sieve()
{
        int i,j;
        for(i=0;i<MAXN;i++)
        spf[i]=i;
        for(i=4;i<MAXN;i+=2)
        spf[i]=2;
        for(i=3;i*i<MAXN;i++)
        {
                if(spf[i]==i)
                {
                        for(j=i*i;j<MAXN;j+=i)
                        {
                                if(spf[j]==j)spf[j]=i;
                        }
                }
        }
}*/
/*bool isvowel(char c)
{
    switch(c)
    {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':return true;
        default : return false;
    }
}*/
/*bool isValid(int x,int y,int n,int m)
{
    if(x<0||x>n-1||y<0||y>m-1)return false;
    return true;
}*/
/*
bool comp(pair<int,int> x,pair<int,int> y)
{
    if(x.first==y.first)
        return x.second<y.second;
    return x.first>y.first;
}*/
int n, m;
int c;

vector<int> vr[200001];
int vis[200001];
void bfs(int node) {
  vis[node] = 1;
  c++;

  for (int i : vr[node]) {
    if (vis[i] == 0) {

      bfs(i);
    }
  }
}

void solve() {

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    vr[a].pb(b);
    vr[b].pb(a);
  }
  int cc = INT_MIN;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      c = 0;

      bfs(i);
      cc = max(cc, c);
    }
  }
  cout << cc + 1 - 1 << endl;
}
int32_t main() {
  ACC int T;
  T = 1;
  // cin>>T;
  // sieve();
  while (T--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ACC                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mod 1000000007
#define MAXN 10000001
// string alpha="abcdefghijklmnopqrstuvwxyz";
// int dx[]={1,-1,0,0};
// int dy[]={0,0,1,-1};
// int dx8[]={1,-1,0,0,1,1,-1,-1};
// int dy8[]={0,0,1,-1,1,-1,1,-1};
/*int spf[MAXN];
void sieve()
{
        int i,j;
        for(i=0;i<MAXN;i++)
        spf[i]=i;
        for(i=4;i<MAXN;i+=2)
        spf[i]=2;
        for(i=3;i*i<MAXN;i++)
        {
                if(spf[i]==i)
                {
                        for(j=i*i;j<MAXN;j+=i)
                        {
                                if(spf[j]==j)spf[j]=i;
                        }
                }
        }
}*/
/*bool isvowel(char c)
{
    switch(c)
    {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':return true;
        default : return false;
    }
}*/
/*bool isValid(int x,int y,int n,int m)
{
    if(x<0||x>n-1||y<0||y>m-1)return false;
    return true;
}*/
/*
bool comp(pair<int,int> x,pair<int,int> y)
{
    if(x.first==y.first)
        return x.second<y.second;
    return x.first>y.first;
}*/
vector<int> v[200001];
int vis[200001];
int dis[200001];
int ans;
void bfs(int x) {
  vis[x] = 1;
  dis[x] = 1;
  ans = max(ans, dis[x]);
  int i = 2;
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (int child : v[p]) {
      if (!vis[child]) {
        vis[child] = 1;
        dis[child] = i++;
        ans = max(ans, dis[child]);
        // cout<<
        q.push(child);
      }
    }
  }
}
int32_t main() {
  ACC int T;
  T = 1;
  // cin>>T;
  // sieve();
  while (T--) {
    int n, i, m;
    cin >> n >> m;
    set<pair<int, int>> s;
    for (i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      if (x > y)
        swap(x, y);
      s.insert({x, y});
    }
    for (auto j : s) {
      memset(vis, 0, sizeof(vis));
      bfs(j.first);
      if (!vis[j.second]) {
        // cout<<j.first<<","<<j.second<<"\\n";
        v[j.first].pb(j.second);
        v[j.second].pb(j.first);
      }
    }
    memset(vis, 0, sizeof(vis));
    memset(dis, 0, sizeof(dis));
    ans = 0;
    for (i = 1; i <= n; i++) {
      if (!vis[i]) {
        bfs(i);
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}