Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define F first
#define S second
#define migmig                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define sortv(X) sort(X.begin(), X.end());
#define ll long long
#define ld long double
#define pii pair<int, int>
#define bug(x) cout << "passed " << x << endl;
#define ashar(x, y, z)                                                         \
  cout << setprecision(x) << fixed << y;                                       \
  if (z == 1)                                                                  \
    cout << "\n";
#define ppi pair<pair<int, int>, int>
#define ordered_set                                                            \
  tree<pair<long long, long long>, null_type,                                  \
       less<pair<long long, long long>>, rb_tree_tag,                          \
       tree_order_statistics_node_update>

long long const inf = 3e18, linf = 2e9, mod = 1e9 + 7, inf2 = 1e12;
int const mxn = 5e5 + 10;
ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
int n, m, mark[mxn], now, ans;
vector<int> adj[mxn];
int dfs(int v) {
  now++;
  mark[v] = 1;
  for (auto u : adj[v]) {
    if (!mark[u])
      dfs(u);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      now = 0;
      dfs(i);
      ans = max(ans, now);
    }
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define F first
#define S second
#define migmig                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define sortv(X) sort(X.begin(), X.end());
#define ll long long
#define ld long double
#define pii pair<int, int>
#define bug(x) cout << "passed " << x << endl;
#define ashar(x, y, z)                                                         \
  cout << setprecision(x) << fixed << y;                                       \
  if (z == 1)                                                                  \
    cout << "\n";
#define ppi pair<pair<int, int>, int>
#define ordered_set                                                            \
  tree<pair<long long, long long>, null_type,                                  \
       less<pair<long long, long long>>, rb_tree_tag,                          \
       tree_order_statistics_node_update>

long long const inf = 3e18, linf = 2e9, mod = 1e9 + 7, inf2 = 1e12;
int const mxn = 5e5 + 10;
ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
int n, m, mark[mxn], now, ans;
vector<int> adj[mxn];
void dfs(int v) {
  now++;
  mark[v] = 1;
  for (auto u : adj[v]) {
    if (!mark[u])
      dfs(u);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      now = 0;
      dfs(i);
      ans = max(ans, now);
    }
  }
  cout << ans;
}