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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N + 5);
  vector<int> B(N + 5);
  Graph G(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].pb(b);
    G[b].pb(a);
    // --A[i];
    // --B[i];
    // G[A[i]].pb(B[i]);
    // G[B[i]].pb(A[i]);
  }
  rep(i, N) {
    std::sort(G[i].begin(), G[i].end());
    G[i].erase(std::unique(G[i].begin(), G[i].end()), G[i].end());
  }

  // int i = 0;
  //     {
  //          for(auto itr =  G[i].begin(); itr !=  G[i].end(); ++itr)
  //          {
  //              cout << *itr << " ";
  //          }
  //          cout << endl;
  //     }
  // return 0;

  //  // グラフ入力受取
  // Graph G(N);
  // for (int i = 0; i < M; ++i) {
  //     G[A[i]].push_back(B[i]);
  //     G[B[i]].push_back(A[i]);
  // }

// 頂点 s をスタートとした探索
#if 1
  // 頂点 s をスタートとした探索
  int ans = 0;
  int ax = 0;
  vector<int> dist(N, -1);
  queue<int> que;
  int count = 0;
  for (int v = 0; v < N; ++v) {
    ax = 1;
    if (dist[v] != -1)
      continue; // v が探索済みならスルー
    dist[v] = 0, que.push(v);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      // for (auto nv : G[v])
      if (!G[v].empty()) {
        for (auto itr = G[v].begin(); itr != G[v].end(); ++itr) {
          int nv = *itr;
          // cout << nv << "," << v << endl;

          if (dist[nv] == -1) {
            dist[nv] = dist[v] + 1;
            que.push(nv);
            ++ax;
          }
        }
      }
    }
    chmax(ans, ax);
    ++count;
  }
  cout << ans << endl;
#endif
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<set<int>>;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  vector<int> B(N);
  Graph G(N);
  rep(i, M) {
    cin >> A[i] >> B[i];
    --A[i];
    --B[i];
    G[A[i]].insert(B[i]);
    G[B[i]].insert(A[i]);
  }

  //  // グラフ入力受取
  // Graph G(N);
  // for (int i = 0; i < M; ++i) {
  //     G[A[i]].push_back(B[i]);
  //     G[B[i]].push_back(A[i]);
  // }

  // 頂点 s をスタートとした探索
  int ans = 0;
  int ax = 0;
  vector<int> dist(N, -1);
  queue<int> que;
  int count = 0;
  for (int v = 0; v < N; ++v) {
    ax = 1;
    if (dist[v] != -1)
      continue; // v が探索済みならスルー
    dist[v] = 0, que.push(v);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (auto nv : G[v]) {
        if (dist[nv] == -1) {
          dist[nv] = dist[v] + 1;
          que.push(nv);
          ++ax;
        }
      }
    }
    chmax(ans, ax);
    ++count;
  }
  cout << ans << endl;

  return 0;
}