Problem p02573 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define pb push_back
#define mpr make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ll long long
#define ld long double
#define all(arr) arr.begin(), arr.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define INF 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846264338327950L
#define N 100005
#define mid(l, r) l + (r - l) / 2
#define vec vector<int>
#define vecl vector<ll>
#define umap unordered_map<ll, ll>
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define endl "\\n"
using namespace std;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}

unordered_map<int, vector<int>> mp;
int vis[1000005];
int ans = 0;

int dfs(int u) {

  int cnt = 1;
  vis[u] = 1;
  for (auto it : mp[u]) {
    if (vis[it] == 0) {
      cnt += dfs(it);
    }
  }

  return cnt;
}

void test_case() {

  int n, m;
  cin >> n;

  cin >> m;

  rep(i, m) {
    int a, b;
    cin >> a >> b;

    mp[a].pb(b);
    mp[b].pb(a);
  }

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

  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int t = 1;
  // cin >> t;
  while (t--) {
    test_case();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define pb push_back
#define mpr make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ll long long
#define ld long double
#define all(arr) arr.begin(), arr.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define INF 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846264338327950L
#define N 100005
#define mid(l, r) l + (r - l) / 2
#define vec vector<int>
#define vecl vector<ll>
#define umap unordered_map<ll, ll>
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define endl "\\n"
using namespace std;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}

unordered_map<int, vector<int>> mp;
int vis[N];
int ans = 0;

int dfs(int u) {

  int cnt = 1;
  vis[u] = 1;
  for (auto it : mp[u]) {
    if (vis[it] == 0) {
      cnt += dfs(it);
    }
  }

  return cnt;
}

void test_case() {

  int n, m;
  cin >> n;

  cin >> m;

  rep(i, m) {
    int a, b;
    cin >> a >> b;

    mp[a].pb(b);
    mp[b].pb(a);
  }

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

  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int t = 1;
  // cin >> t;
  while (t--) {
    test_case();
  }
}