Problem p02573 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize "trapv"
//-D_GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define pm(m)                                                                  \
  for (auto itr = (m).begin(); itr != (m).end(); itr++) {                      \
    itr->first << " " << itr->second << "\n";                                  \
  }
#define int long long int
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define ii pair<int, int>
#define Max 100000 + 5
#define IO                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define f first
#define s second
#define pb push_back
#define bug(x)                                                                 \
  cout << (x) << " ";                                                          \
  exit(0);
int mod = 1000000007;
int mod1 = 998244353;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int mx = 100001;
vector<int> g[mx];
vector<int> vis(mx);
int cnt = 0;
void dfs(int u) {

  vis[u] = 1;
  cnt++;
  for (auto v : g[u]) {
    if (!vis[v]) {
      dfs(v);
    }
  }
}
int32_t main() {
  IO
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);

      int n,
      m;
  cin >> n >> m;
  int ans = 0;

  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;

    g[u].pb(v);
    g[v].pb(u);
  }

  for (int i = 1; i <= n; i++) {
    cnt = 0;
    if (vis[i] == 0) {
      dfs(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans;
}

Fixed submission:
// #pragma GCC optimize "trapv"
//-D_GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define pm(m)                                                                  \
  for (auto itr = (m).begin(); itr != (m).end(); itr++) {                      \
    itr->first << " " << itr->second << "\n";                                  \
  }
#define int long long int
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define ii pair<int, int>
#define Max 100000 + 5
#define IO                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define f first
#define s second
#define pb push_back
#define bug(x)                                                                 \
  cout << (x) << " ";                                                          \
  exit(0);
int mod = 1000000007;
int mod1 = 998244353;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int mx = 200001;
vector<int> g[mx];
vector<int> vis(mx);
int cnt = 0;
void dfs(int u) {

  vis[u] = 1;
  cnt++;
  for (auto v : g[u]) {
    if (!vis[v]) {
      dfs(v);
    }
  }
}
int32_t main() {
  IO
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);

      int n,
      m;
  cin >> n >> m;
  int ans = 0;

  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;

    g[u].pb(v);
    g[v].pb(u);
  }

  for (int i = 1; i <= n; i++) {
    cnt = 0;
    if (vis[i] == 0) {
      dfs(i);
      ans = max(ans, cnt);
    }
  }
  cout << ans;
}