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

#include <bits/stdc++.h>
using namespace std;

#define V vector
typedef long long ll;
typedef unsigned long long ull;

typedef V<int> vi;
typedef V<ll> vll;
typedef V<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define f(i, a) for (int i = 0; i < a; i++)
#define fll(i, a) for (ll i = 0; i < a; i++)
#define forab(i, a, b) for (int i = a; i < b; i++)
#define prec(x) cout << fixed << setprecision(x)
#define ff first
#define ss second

#define pb push_back
#define mp make_pair
#define numberofdigits(x) floor(log10(x)) + 1

const ll mod = 1e9 + 7;

ll binpow(ll a, long long b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}

vector<vector<int>> a;
vector<int> vis;

int dfs(int node) {
  vis[node] = 1;
  int ans = 1;
  for (auto child : a[node]) {
    if (vis[child] == 1)
      continue;

    ans += dfs(child);
  }
  return ans;
}
void solve() {

  int n, m;
  cin >> n >> m;

  a = vector<vector<int>>(n + 1);
  vis = vector<int>(n + 1, 0);

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

    a[u].pb(v), a[v].pb(u);
  }

  int cc = 0;
  vector<int> cntnodes;

  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      int c = dfs(i);
      cntnodes.pb(c);
    }
  }

  sort(cntnodes.begin(), cntnodes.end());

  /*for (auto c : cntnodes)
          cout << c << " ";
  cout << "\\n";*/

  int sz = cntnodes.size(), i = 0;

  while (i < sz) {

    if (cntnodes[i] == 0) {
      i++;
      continue;
    }

    int e = cntnodes[i];
    int j = i;

    while (j < sz) {
      cntnodes[j] -= e;
      if (cntnodes[j] == 0)
        i++;
      j++;
    }

    cc += e;
    if (cntnodes[sz - 1] == 0)
      break;
  }

  cout << cc << "\\n";
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;

  while (t--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define V vector
typedef long long ll;
typedef unsigned long long ull;

typedef V<int> vi;
typedef V<ll> vll;
typedef V<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define f(i, a) for (int i = 0; i < a; i++)
#define fll(i, a) for (ll i = 0; i < a; i++)
#define forab(i, a, b) for (int i = a; i < b; i++)
#define prec(x) cout << fixed << setprecision(x)
#define ff first
#define ss second

#define pb push_back
#define mp make_pair
#define numberofdigits(x) floor(log10(x)) + 1

const ll mod = 1e9 + 7;

ll binpow(ll a, long long b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}

vector<vector<int>> a;
vector<int> vis;

int dfs(int node) {
  vis[node] = 1;
  int ans = 1;
  for (auto child : a[node]) {
    if (vis[child] == 1)
      continue;

    ans += dfs(child);
  }
  return ans;
}
void solve() {

  int n, m;
  cin >> n >> m;

  a = vector<vector<int>>(n + 1);
  vis = vector<int>(n + 1, 0);

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

    a[u].pb(v), a[v].pb(u);
  }

  int cc = 0;
  vector<int> cntnodes;

  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      int c = dfs(i);
      cntnodes.pb(c);
    }
  }

  sort(cntnodes.begin(), cntnodes.end());

  /*for (auto c : cntnodes)
          cout << c << " ";
  cout << "\\n";*/

  int sz = cntnodes.size(), i = 0;

  while (i < sz) {

    if (cntnodes[i] == 0) {
      i++;
      continue;
    }

    int e = cntnodes[i];
    int j = i;

    while (j < n) {
      cntnodes[j] -= e;
      if (cntnodes[j] == 0)
        i++;
      j++;
    }

    cc += e;
    if (cntnodes[sz - 1] == 0)
      break;
  }

  cout << cc << "\\n";
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;

  while (t--)
    solve();
  return 0;
}