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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using vb = vector<bool>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = ll(b) - 1; i >= (a); i--)
#define TR(X)                                                                  \\
  ({                                                                           \\
    if (1)                                                                     \\
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \\
  })

vvl adj;

int main() {
  cin.sync_with_stdio(0);

  ll N, M;
  cin >> N >> M;

  adj.resize(N);

  FOR(i, 0, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;

    adj[a].pb(b);
    adj[b].pb(a);
  }

  ll largest = 0;

  vb visited(N);
  FOR(i, 0, N) {

    queue<ll> q;
    q.push(i);

    ll count = 0;
    while (sz(q)) {
      ll cur = q.front();
      q.pop();

      if (visited[cur]) {
        continue;
      }
      visited[cur] = true;

      count++;

      for (ll neigh : adj[cur]) {
        q.push(neigh);
      }
    }

    largest = max(largest, count);
  }

  cout << largest << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using vb = vector<bool>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = ll(b) - 1; i >= (a); i--)
#define TR(X)                                                                  \\
  ({                                                                           \\
    if (1)                                                                     \\
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \\
  })

vvl adj;

int main() {
  cin.sync_with_stdio(0);

  ll N, M;
  cin >> N >> M;

  adj.resize(N);

  FOR(i, 0, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;

    if (count(all(adj[a]), b)) {
      continue;
    }

    adj[a].pb(b);
    adj[b].pb(a);
  }

  ll largest = 0;

  vb visited(N);
  FOR(i, 0, N) {

    queue<ll> q;
    q.push(i);

    ll count = 0;
    while (sz(q)) {
      ll cur = q.front();
      q.pop();

      if (visited[cur]) {
        continue;
      }
      visited[cur] = true;

      count++;

      for (ll neigh : adj[cur]) {
        q.push(neigh);
      }
    }

    largest = max(largest, count);
  }

  cout << largest << endl;
}