Problem p02573 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define MP make_pair
#define PB push_back
#define SZ size()
#define B begin()
#define E end()
#define ll long long
#define REP(i, a, n) for (ll i = a; i < n; ++i)
#define RED(i, n, a) for (ll i = n; i >= a; --i)
#define m9 1000000007
#define endl "\n"
#define F first
#define S second
#define ld long double
#define eps 0.00000000001
#define MAX 1000000007
#define vll vector<ll>
#define pll pair<ll, ll>

using namespace std;

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
ll powc(ll a, ll b) {
  if (b < 0) {
    return -1;
  }
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res *= a;
    }
    a *= a;
    // res%=m9, a%=m9;
    b >>= 1;
  }
  return res;
}
vector<vector<ll>> graph(1e5 + 10);
vector<ll> visited(2e5 + 10, 0);
ll cnt = 0;
void dfs(ll in) {
  cnt++;
  visited[in] = 1;
  REP(i, 0, (ll)graph[in].SZ) {
    if (!visited[graph[in][i]]) {
      dfs(graph[in][i]);
    }
  }
}

/// target expert

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  REP(i, 0, m) {
    ll u, v;
    cin >> u >> v;
    graph[u].PB(v), graph[v].PB(u);
  }
  ll ans = 0;
  REP(i, 1, n + 1) {
    if (!visited[i]) {
      cnt = 0;
      dfs(i);
    }
    ans = max(ans, cnt);
  }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define MP make_pair
#define PB push_back
#define SZ size()
#define B begin()
#define E end()
#define ll long long
#define REP(i, a, n) for (ll i = a; i < n; ++i)
#define RED(i, n, a) for (ll i = n; i >= a; --i)
#define m9 1000000007
#define endl "\n"
#define F first
#define S second
#define ld long double
#define eps 0.00000000001
#define MAX 1000000007
#define vll vector<ll>
#define pll pair<ll, ll>

using namespace std;

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
ll powc(ll a, ll b) {
  if (b < 0) {
    return -1;
  }
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res *= a;
    }
    a *= a;
    // res%=m9, a%=m9;
    b >>= 1;
  }
  return res;
}
vector<vector<ll>> graph(2e5 + 10);
vector<ll> visited(2e5 + 10, 0);
ll cnt = 0;
void dfs(ll in) {
  cnt++;
  visited[in] = 1;
  REP(i, 0, (ll)graph[in].SZ) {
    if (!visited[graph[in][i]]) {
      dfs(graph[in][i]);
    }
  }
}

/// target expert

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  REP(i, 0, m) {
    ll u, v;
    cin >> u >> v;
    graph[u].PB(v), graph[v].PB(u);
  }
  ll ans = 0;
  REP(i, 1, n + 1) {
    if (!visited[i]) {
      cnt = 0;
      dfs(i);
    }
    ans = max(ans, cnt);
  }
  cout << ans;
}