Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

// for debugging
/*
g++ -D_GLIBCXX_ASSERTIONS -DDEBUG -ggdb3 -std=c++14
*/
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x)                                                                 \
  {                                                                            \
    ++recur_depth;                                                             \
    auto x_ = x;                                                               \
    --recur_depth;                                                             \
    cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":"           \
         << __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl;         \
  }
#else
#define dbg(x)
#endif
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream &>::type
operator<<(Ostream &os, const Cont &v) {
  os << "[";
  for (auto &x : v) {
    os << x << ", ";
  }
  return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream &operator<<(Ostream &os, const pair<Ts...> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}
// debugging ends here

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
// const int mxn = 2e5+99;
const int mxn = 1e5 + 100;
vi adj[mxn];
int vis[mxn];
int comp_size[mxn];

void dfs(int u, int comp, int p = -1) {
  comp_size[comp] += 1;
  vis[u] = 1;
  for (auto v : adj[u]) {
    if (!vis[v]) {
      dfs(v, comp, u);
    }
  }
}

void solve() {

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  int ans = 0;
  int comp = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, comp);
      ans = max(ans, comp_size[comp]);
      comp++;
    }
  }
  cout << ans << endl;
}

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

  // int t;cin >> t;while(t--)
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

// for debugging
/*
g++ -D_GLIBCXX_ASSERTIONS -DDEBUG -ggdb3 -std=c++14
*/
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x)                                                                 \
  {                                                                            \
    ++recur_depth;                                                             \
    auto x_ = x;                                                               \
    --recur_depth;                                                             \
    cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":"           \
         << __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl;         \
  }
#else
#define dbg(x)
#endif
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream &>::type
operator<<(Ostream &os, const Cont &v) {
  os << "[";
  for (auto &x : v) {
    os << x << ", ";
  }
  return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream &operator<<(Ostream &os, const pair<Ts...> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}
// debugging ends here

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
// const int mxn = 2e5+99;
const int mxn = 2e5 + 100;
vi adj[mxn];
int vis[mxn];
int comp_size[mxn];

void dfs(int u, int comp, int p = -1) {
  comp_size[comp] += 1;
  vis[u] = 1;
  for (auto v : adj[u]) {
    if (!vis[v]) {
      dfs(v, comp, u);
    }
  }
}

void solve() {

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  int ans = 0;
  int comp = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, comp);
      ans = max(ans, comp_size[comp]);
      comp++;
    }
  }
  cout << ans << endl;
}

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

  // int t;cin >> t;while(t--)
  solve();
}