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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;
typedef vector<bool> vb;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sq(x) ((x) * (x))
#define fo(i, n) for (int i = 0; i < n; i++)
#define fp(i, k, n) for (int i = k; i < n; i++)
#define Fo(i, n) for (int i = 1; i <= n; i++)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define deb(x) cout << #x << "=" << x << "\\n"
#define deb2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << "\\n"
#define fi first
#define se second
const double pi = 3.14159265358979323846;
#define uni(c) (c).resize(unique(all(c)) - (c).begin())
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL)
// pair <int, int> w[4] = {mp(-1, 0), mp(0, -1), mp(1, 0), mp(0, 1)};   // for
// matrix movement:
void cp() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
unordered_map<int, unordered_set<int>> a;
bool vis[200020];
int dfs(int x) {
  unordered_set<int> count;
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    int temp = q.front();
    q.pop();
    count.insert(temp);
    vis[temp] = 1;
    for (auto it : a[temp]) {
      if (!vis[it]) {
        q.push(it);
        vis[it] = 1;
      }
    }
  }
  return (int)count.size();
}
void solve() {
  int n, m;
  cin >> n >> m;
  int ans = 0;
  map<int, unordered_set<int>> ho;
  fo(i, m) {
    int x, y;
    cin >> x >> y;
    if (x > y)
      swap(x, y);
    if (ho[x].size() == 0) {
      a[y].insert(x);
      a[x].insert(y);
    } else if (ho[x].size() > 0 and ho[x].find(y) == ho[x].end()) {
      a[y].insert(x);
      a[x].insert(y);
    }
    ho[x].insert(y);
  }
  if (m == 0) {
    cout << 1 << "\\n";
    return;
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ans = max(ans, dfs(i));
    }
  }
  cout << ans;
}
int main() {
  fast;
  // cp();
  ll t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;
typedef vector<bool> vb;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sq(x) ((x) * (x))
#define fo(i, n) for (int i = 0; i < n; i++)
#define fp(i, k, n) for (int i = k; i < n; i++)
#define Fo(i, n) for (int i = 1; i <= n; i++)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define deb(x) cout << #x << "=" << x << "\\n"
#define deb2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << "\\n"
#define fi first
#define se second
const double pi = 3.14159265358979323846;
#define uni(c) (c).resize(unique(all(c)) - (c).begin())
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL)
// pair <int, int> w[4] = {mp(-1, 0), mp(0, -1), mp(1, 0), mp(0, 1)};   // for
// matrix movement:
void cp() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
unordered_map<int, unordered_set<int>> a;
bool vis[200020];
int dfs(int x) {
  unordered_set<int> count;
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    ll temp = q.front();
    q.pop();
    count.insert(temp);
    vis[temp] = 1;
    for (auto it : a[temp]) {
      if (!vis[it]) {
        q.push(it);
      }
    }
  }
  return (int)count.size();
}
void solve() {
  int n, m;
  cin >> n >> m;
  int ans = 0;
  map<int, unordered_set<int>> ho;
  fo(i, m) {
    int x, y;
    cin >> x >> y;
    if (x > y)
      swap(x, y);
    if (ho[x].size() == 0) {
      a[y].insert(x);
      a[x].insert(y);
    } else if (ho[x].size() > 0 and ho[x].find(y) == ho[x].end()) {
      a[y].insert(x);
      a[x].insert(y);
    }
    ho[x].insert(y);
  }
  if (m == 0) {
    cout << 1 << "\\n";
    return;
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ans = max(ans, dfs(i));
    }
  }
  cout << ans;
}
int main() {
  fast;
  // cp();
  ll t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}