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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;

#define deb(x) cerr << #x << ":" << x << "\\n";
#define dvi(x)                                                                 \\
  for (ll i = 0; i < x.size(); i++)                                            \\
    cerr << #x << "[" << i << "]:" << x[i] << " ";
#define iter(v) for (auto it : v)
#define wt                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define rl(x)                                                                  \\
  ll x;                                                                        \\
  cin >> x;
#define rvi(x, n)                                                              \\
  vi x(n);                                                                     \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> x[i];
#define rs(x)                                                                  \\
  string x;                                                                    \\
  cin >> x;
#define wvi(x)                                                                 \\
  for (ll i = 0; i < x.size(); i++)                                            \\
    cout << x[i] << " ";                                                       \\
  cout << endl;
#define ws(x) cout << x << endl;
#define all(v) v.begin(), v.end()
#define pb push_back
#define ppb pop_back
#define pll pair<ll, ll>
#define fo(i, n) for (ll i = 0; i < n; i++)
#define forr(i, a, b) for (ll i = a; i < b; i++)
#define decsort(b) sort(all(b), greater<ll>())
#define yes cout << "YES\\n";
#define no cout << "NO\\n";
const ll N = 1e5;

map<long long, long long>
factorize(long long n) { // returns map containing factor and multiplicity, Eg:
                         // 60 = {{2,2},{3,1},{5,1}}
  map<long long, long long> ans;
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ans[i]++;
      n /= i;
    }
  }
  if (n > 1) {
    ans[n]++;
    n = 1;
  }
  return ans;
}

void FAST() { // FAST Input Output, DONT use both scnaf,prllf / cin,cout , Use
              // any ONE system
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

map<ll, ll> vis;
map<ll, set<ll>> gr;
ll dfs(ll i) {
  ll ans = 1;
  vis[i] = 1;
  for (auto to : gr[i]) {
    if (vis[to])
      continue;
    ans += dfs(to);
  }
  return ans;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FAST();
  rl(n) rl(m)

      ll l,
      r;
  while (m--) {
    cin >> l >> r;
    l--;
    r--;
    gr[l].insert(r);
    gr[r].insert(l);
  }

  ll mx = 0;
  for (ll i = 0; i < n; i++) {
    if (vis[i])
      continue;
    else {
      mx = max(mx, dfs(i));
    }
  }
  cout << mx;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;

#define deb(x) cerr << #x << ":" << x << "\\n";
#define dvi(x)                                                                 \\
  for (ll i = 0; i < x.size(); i++)                                            \\
    cerr << #x << "[" << i << "]:" << x[i] << " ";
#define iter(v) for (auto it : v)
#define wt                                                                     \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define rl(x)                                                                  \\
  ll x;                                                                        \\
  cin >> x;
#define rvi(x, n)                                                              \\
  vi x(n);                                                                     \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> x[i];
#define rs(x)                                                                  \\
  string x;                                                                    \\
  cin >> x;
#define wvi(x)                                                                 \\
  for (ll i = 0; i < x.size(); i++)                                            \\
    cout << x[i] << " ";                                                       \\
  cout << endl;
#define ws(x) cout << x << endl;
#define all(v) v.begin(), v.end()
#define pb push_back
#define ppb pop_back
#define pll pair<ll, ll>
#define fo(i, n) for (ll i = 0; i < n; i++)
#define forr(i, a, b) for (ll i = a; i < b; i++)
#define decsort(b) sort(all(b), greater<ll>())
#define yes cout << "YES\\n";
#define no cout << "NO\\n";
const ll N = 1e5;

map<long long, long long>
factorize(long long n) { // returns map containing factor and multiplicity, Eg:
                         // 60 = {{2,2},{3,1},{5,1}}
  map<long long, long long> ans;
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ans[i]++;
      n /= i;
    }
  }
  if (n > 1) {
    ans[n]++;
    n = 1;
  }
  return ans;
}

void FAST() { // FAST Input Output, DONT use both scnaf,prllf / cin,cout , Use
              // any ONE system
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

map<ll, bool> vis;
ll dfs(vector<set<ll>> gr, ll i) {
  ll ans = 1;
  vis[i] = 1;
  for (auto to : gr[i]) {
    if (vis[to])
      continue;
    ans += dfs(gr, to);
  }
  return ans;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FAST();
  rl(n) rl(m) vector<set<ll>> gr(n);
  ll l, r;
  while (m--) {
    cin >> l >> r;
    l--;
    r--;
    gr[l].insert(r);
    gr[r].insert(l);
  }

  ll mx = 0;
  for (ll i = 0; i < n; i++) {
    if (vis[i])
      continue;
    else {
      mx = max(mx, dfs(gr, i));
    }
  }
  cout << mx;
}