Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define po pop_back
#define fi first
#define se second
#define mxn 100005
#define ll long long int
#define ldb long double
#define fr(i, a, b, k) for (int i = a; i < b; i += k)
#define frr(i, a, b, k) for (int i = a; i > b; i -= k)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define deg(x) cout << endl << #x << " = " << (x) << endl;
#define casep(x) cout << "Case #" << (x) << ": ";
#define fast                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \
  srand(time(NULL));

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

typedef pair<int, int> pp;
typedef vector<int> vi;
typedef vector<pp> vpp;
typedef vector<vi> vvi;

const ll inf = LLONG_MAX;

ll power_mod(int a, int n, int mod) {
  ll p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
ll power(ll a, ll n) {
  if (n == 0) {
    return 1;
  }
  ll p = power(a, n / 2);
  p = (p * p);
  if (n % 2 == 1) {
    p = (p * a);
  }
  return p;
}
int gcd(int a, int b) { return __gcd(a, b); }
int lcm(int a, int b) { return a * b / __gcd(a, b); }
vector<vi> adj;
bool vis[mxn];

ll dfs(ll x) {

  vis[x] = 1;
  ll n = 1;
  for (auto u : adj[x]) {
    if (vis[u])
      continue;
    vis[u] = 1;
    n += dfs(u);
  }

  return n;
}

void run(ll test) {

  ll n, m;
  cin >> n >> m;
  adj = vector<vi>(n);
  memset(vis, 0, n + 1);

  fr(i, 0, m, 1) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    if (find(adj[x].begin(), adj[x].end(), y) == adj[x].end()) {
      adj[x].pb(y);
      adj[y].pb(x);
    }
  }

  ll ans = 0;
  fr(i, 0, n, 1) {
    if (!vis[i]) {
      ll nodes = dfs(i);
      ans = max(nodes, ans);
    }
  }

  cout << ans;
}

int main() {

  fast;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    run(i);
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define po pop_back
#define fi first
#define se second
#define mxn 200005
#define ll long long int
#define ldb long double
#define fr(i, a, b, k) for (int i = a; i < b; i += k)
#define frr(i, a, b, k) for (int i = a; i > b; i -= k)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define deg(x) cout << endl << #x << " = " << (x) << endl;
#define casep(x) cout << "Case #" << (x) << ": ";
#define fast                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \
  srand(time(NULL));

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

typedef pair<int, int> pp;
typedef vector<int> vi;
typedef vector<pp> vpp;
typedef vector<vi> vvi;

const ll inf = LLONG_MAX;

ll power_mod(int a, int n, int mod) {
  ll p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
ll power(ll a, ll n) {
  if (n == 0) {
    return 1;
  }
  ll p = power(a, n / 2);
  p = (p * p);
  if (n % 2 == 1) {
    p = (p * a);
  }
  return p;
}
int gcd(int a, int b) { return __gcd(a, b); }
int lcm(int a, int b) { return a * b / __gcd(a, b); }
vector<vi> adj;
bool vis[mxn];

ll dfs(ll x) {

  vis[x] = 1;
  ll n = 1;
  for (auto u : adj[x]) {
    if (vis[u])
      continue;
    vis[u] = 1;
    n += dfs(u);
  }

  return n;
}

void run(ll test) {

  ll n, m;
  cin >> n >> m;
  adj = vector<vi>(n);
  memset(vis, 0, n + 1);

  fr(i, 0, m, 1) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    if (find(adj[x].begin(), adj[x].end(), y) == adj[x].end()) {
      adj[x].pb(y);
      adj[y].pb(x);
    }
  }

  ll ans = 0;
  fr(i, 0, n, 1) {
    if (!vis[i]) {
      ll nodes = dfs(i);
      ans = max(nodes, ans);
    }
  }

  cout << ans;
}

int main() {

  fast;
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    run(i);
    cout << endl;
  }
  return 0;
}