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

#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int MOD = 1e9 + 7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct edge {
  ll to, cost;
};

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

// 素数
vector<bool> IsPrime;
void sieve(size_t max) {
  if (max + 1 > IsPrime.size()) {
    IsPrime.resize(max + 1, true);
  }
  IsPrime[0] = false;
  IsPrime[1] = false;

  for (size_t i = 2; i * i <= max; ++i) {
    if (IsPrime[i]) {
      for (size_t j = 2; i * j <= max; ++j) {
        IsPrime[i * j] = false;
      }
    }
  }
}

// べき乗の余り
ll modpow(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1) {
      res = res * a % MOD;
    }
    a = a * a % MOD;
    n = n >> 1;
  }
  return res;
}

// コンビネーション
ll MAXN = 2e5 + 10;
vector<ll> inv(MAXN);
ll nCk(ll n, ll k) {
  ll res = 1;
  for (ll i = 1; i <= k; i++) {
    res = res * (n - i + 1) % MOD;
  }

  for (ll i = 1; i <= k; i++) {
    res = res * inv[i] % MOD;
  }
  return res;
}

// 回文
bool kai(string S) {
  bool flag = true;
  for (ll i = 0; i < S.size() / 2; ++i) {
    if (S[i] != S[S.size() - i - 1]) {
      flag = false;
      break;
    }
  }
  return flag;
}
// ---------------------------------------------

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> g[n];
  vector<ll> a(m), b(m);
  for (ll i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }

  ll ans = 1;
  vector<bool> d(n, false);
  for (ll i = 0; i < n; ++i) {
    ll sum = 1;
    if (d[i] == true or g[i].size() == 0)
      continue;
    d[i] = true;

    queue<ll> que;
    for (ll j = 0; j < g[i].size(); ++j) {
      if (d[g[i][j]] == false) {
        que.push(g[i][j]);
        d[g[i][j]] = true;
        sum++;
      }
    }
    while (!que.empty()) {
      ll x = que.front();
      que.pop();
      for (ll j = 0; j < g[x].size(); ++j) {
        if (d[g[x][j]] == false) {
          que.push(g[x][j]);
          d[g[x][j]] = true;
          sum++;
        }
      }
    }
    ans = max(ans, sum);
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int MOD = 1e9 + 7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct edge {
  ll to, cost;
};

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

// 素数
vector<bool> IsPrime;
void sieve(size_t max) {
  if (max + 1 > IsPrime.size()) {
    IsPrime.resize(max + 1, true);
  }
  IsPrime[0] = false;
  IsPrime[1] = false;

  for (size_t i = 2; i * i <= max; ++i) {
    if (IsPrime[i]) {
      for (size_t j = 2; i * j <= max; ++j) {
        IsPrime[i * j] = false;
      }
    }
  }
}

// べき乗の余り
ll modpow(ll a, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1) {
      res = res * a % MOD;
    }
    a = a * a % MOD;
    n = n >> 1;
  }
  return res;
}

// コンビネーション
ll MAXN = 2e5 + 10;
vector<ll> inv(MAXN);
ll nCk(ll n, ll k) {
  ll res = 1;
  for (ll i = 1; i <= k; i++) {
    res = res * (n - i + 1) % MOD;
  }

  for (ll i = 1; i <= k; i++) {
    res = res * inv[i] % MOD;
  }
  return res;
}

// 回文
bool kai(string S) {
  bool flag = true;
  for (ll i = 0; i < S.size() / 2; ++i) {
    if (S[i] != S[S.size() - i - 1]) {
      flag = false;
      break;
    }
  }
  return flag;
}
// ---------------------------------------------

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m), b(m);
  vector<ll> g[n];
  for (ll i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }

  ll ans = 0;
  vector<bool> d(n, false);
  for (ll i = 0; i < n; ++i) {
    ll sum = 1;
    if (d[i] == true or g[i].size() == 0)
      continue;
    d[i] = true;

    queue<ll> que;
    for (ll j = 0; j < g[i].size(); ++j) {
      if (d[g[i][j]] == true)
        continue;
      que.push(g[i][j]);
      d[g[i][j]] = true;
      sum++;
    }
    while (!que.empty()) {
      ll x = que.front();
      que.pop();
      for (ll j = 0; j < g[x].size(); ++j) {
        if (d[g[x][j]] == false) {
          que.push(g[i][j]);
          d[g[x][j]] = true;
          sum++;
        }
      }
    }
    ans = max(ans, sum);
  }
  cout << ans << endl;

  return 0;
}