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

#include <bits/stdc++.h>
using namespace std;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
typedef long long ll;
typedef vector<int> VI;
typedef vector<set<int>> VVI;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef set<int> SI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<ll, ll> PLL;
typedef pair<ll, pair<ll, ll>> PLLL;

#define flash                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define MP make_pair
#define PB push_back
#define FF first
#define SS second
#define mod 1000000007
#define test                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)

void dfs(int n, VVI &al, VI &vis, int &c) {
  vis[n] = 1;
  c++;
  int s = al[n].size();
  auto it = al[n].begin();
  while (it != al[n].end()) {

    if (vis[*it] == 0) {
      dfs(*it, al, vis, c);
    }
    it++;
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  VVI al(n + 1);
  VI vis(n + 1);

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    al[x].insert(y);
    al[y].insert(x);
  }
  int ans = -1;
  for (int i = 1; i <= n; i++) {

    int c = 0;
    if (vis[i] == 0)
      dfs(i, al, vis, c);

    ans = max(ans, c);
  }
  cout << ans;

  return;
}

int main() {
  flash;

  // test
  // {
  // 	solve();
  // }
  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
typedef long long ll;
typedef vector<int> VI;
typedef vector<set<int>> VVI;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef set<int> SI;
typedef map<string, int> MSI;
typedef map<int, int> MII;
typedef pair<ll, ll> PLL;
typedef pair<ll, pair<ll, ll>> PLLL;

#define flash                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define MP make_pair
#define PB push_back
#define FF first
#define SS second
#define mod 1000000007
#define test                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)

void dfs(int n, VVI &al, VI &vis, int &c) {
  vis[n] = 1;
  c++;
  int s = al[n].size();
  auto it = al[n].begin();
  while (it != al[n].end()) {

    if (vis[*it] == 0) {
      dfs(*it, al, vis, c);
    }
    it++;
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  VVI al(n + 1);

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    al[x].insert(y);
    al[y].insert(x);
  }
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    VI vis(n + 1);
    int c = 0;
    dfs(i, al, vis, c);

    ans = max(ans, c);
  }
  cout << ans;

  return;
}

int main() {
  flash;

  // test
  // {
  // 	solve();
  // }
  solve();

  return 0;
}