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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>

int MOD_BIG = 1000000007;
int MOD_BIG2 = 998244353;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using ll = long long;
using pint = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using std::vector;

#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)

//////////////////////////////////////

//////////////////////////////////////

int bfs(vector<std::set<int>> &G, int N, vector<int> &group, int s, int newg) {
  std::queue<int> q;
  if (group[s] != 0)
    return 1;
  auto F = G[s];
  for (auto itr = F.begin(); itr != F.end(); itr++) {
    q.push(*itr);
  }
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    if (group[now] != 0)
      continue;
    group[now] = newg;
    auto f = G[now];
    for (auto itr = f.begin(); itr != f.end(); itr++) {
      q.push(*itr);
    }
  }
  return 0;
}

int main() {

  int N, M;
  cin >> N >> M;
  if (M == 0) {
    cout << 1 << endl;
    return 0;
  }
  vector<std::set<int>> G(N);
  vector<int> group(N);

  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].insert(b);
    G[b].insert(a);
  }
  int newg = 1;
  REP(i, N) {
    auto ok = bfs(G, N, group, i, newg);
    if (ok == 0)
      newg++;
  }
  REP(i, N) {
    // cerr << group[i] << endl;
  }
  vector<int> num(N);
  REP(i, N) {
    if (group[i] != 0) {
      num[group[i] - 1]++;
    }
  }
  int ans = 0;
  REP(i, N) {
    // cerr << num[i] << endl;
    ans = std::max(ans, num[i]);
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>

int MOD_BIG = 1000000007;
int MOD_BIG2 = 998244353;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using ll = long long;
using pint = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using std::vector;

#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)

//////////////////////////////////////

//////////////////////////////////////

int bfs(vector<std::set<int>> &G, int N, vector<int> &group, int s, int newg) {
  std::queue<int> q;
  if (group[s] != 0)
    return 1;
  auto F = G[s];
  for (auto itr = F.begin(); itr != F.end(); itr++) {
    q.push(*itr);
  }
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    if (group[now] != 0)
      continue;
    group[now] = newg;
    auto f = G[now];
    for (auto itr = f.begin(); itr != f.end(); itr++) {
      q.push(*itr);
    }
  }
  return 0;
}

int main() {

  int N, M;
  cin >> N >> M;
  vector<std::set<int>> G(N);
  vector<int> group(N);

  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].insert(b);
    G[b].insert(a);
  }
  int newg = 1;
  REP(i, N) {
    auto ok = bfs(G, N, group, i, newg);
    if (ok == 0)
      newg++;
  }
  REP(i, N) {
    // cerr << group[i] << endl;
  }
  vector<int> num(N);
  REP(i, N) { num[group[i] - 1]++; }
  int ans = 0;
  REP(i, N) {
    // cerr << num[i] << endl;
    ans = std::max(ans, num[i]);
  }
  cout << ans << endl;

  return 0;
}