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

#define _USE_MATH_DEFINES

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

constexpr ll MOD = 1000000007;

#ifndef ONLINE_JUDGE
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto &x : obj)
    o << " (" << x.first << " : " << x.second << ")"
      << ",";
  o << " }";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const multiset<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
  o << "[";
  for (int i = 0; i < (int)obj.size(); ++i)
    o << (i > 0 ? ", " : "") << obj[i];
  o << "]";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
  o << "(" << obj.first << ", " << obj.second << ")";
  return o;
}
template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
void print_sim_py(void) { cout << endl; }
template <class Head> void print_sim_py(Head &&head) {
  cout << head;
  print_sim_py();
}
template <class Head, class... Tail>
void print_sim_py(Head &&head, Tail &&...tail) {
  cout << head << " ";
  print_sim_py(forward<Tail>(tail)...);
}

#define print(...) print_sim_py(__VA_ARGS__);
#else
#define print(...) ;
#endif

template <class T, class U>
void dfs(ll x, vector<vector<T>> &g, vector<U> &seen,
         function<void(void)> func = nullptr) {
  for (ll i = 0; i < g[x].size(); i++) {
    if (g[x][i] && !seen[i]) {
      seen[i] = !seen[i];
      if (func)
        func();
      dfs(i, g, seen, func);
    } else {
      continue;
    }
  }
}

template <class T, class U>
void dfs(ll x, vector<list<T>> &g, vector<U> &seen,
         function<void(void)> func = nullptr) {
  for (auto &i : g[x]) {
    if (!seen[i]) {
      seen[i] = !seen[i];
      if (func)
        func();
      dfs(i, g, seen, func);
    } else {
      continue;
    }
  }
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, m;
  ll ans = 0;

  cin >> n >> m;

  // vector<vector<char>> f(n,vector<char>(n,0));
  vector<list<ll>> f(n);
  vector<ll> seen(n, 0);

  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    f[x - 1].push_back(y - 1);
    f[y - 1].push_back(x - 1);
  }

  for (ll i = 0; i < n; i++) {
    if (!seen[i]) {
      ll cnt = 0;
      dfs(i, f, seen, [&]() { cnt++; });
      // print(i,seen, cnt)
      ans = max(ans, cnt);
    }
  }
  ans = max(ans, 1ll);

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

constexpr ll MOD = 1000000007;

#ifndef ONLINE_JUDGE
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto &x : obj)
    o << " (" << x.first << " : " << x.second << ")"
      << ",";
  o << " }";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const multiset<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
  o << "[";
  for (int i = 0; i < (int)obj.size(); ++i)
    o << (i > 0 ? ", " : "") << obj[i];
  o << "]";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
  o << "(" << obj.first << ", " << obj.second << ")";
  return o;
}
template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
void print_sim_py(void) { cout << endl; }
template <class Head> void print_sim_py(Head &&head) {
  cout << head;
  print_sim_py();
}
template <class Head, class... Tail>
void print_sim_py(Head &&head, Tail &&...tail) {
  cout << head << " ";
  print_sim_py(forward<Tail>(tail)...);
}

#define print(...) print_sim_py(__VA_ARGS__);
#else
#define print(...) ;
#endif

template <class T, class U>
void dfs(ll x, vector<vector<T>> &g, vector<U> &seen, ll &count) {
  for (ll i = 0; i < g[x].size(); i++) {
    if (g[x][i] && !seen[i]) {
      seen[i] = !seen[i];
      count++;
      dfs(i, g, seen, count);
    } else {
      continue;
    }
  }
}

template <class T, class U>
void dfs(ll x, vector<list<T>> &g, vector<U> &seen, ll &count) {
  for (auto &i : g[x]) {
    if (!seen[i]) {
      seen[i] = !seen[i];
      count++;
      dfs(i, g, seen, count);
    } else {
      continue;
    }
  }
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, m;
  ll ans = 0;

  cin >> n >> m;

  // vector<vector<char>> f(n,vector<char>(n,0));
  vector<list<char>> f(n);
  vector<char> seen(n, 0);

  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    f[x - 1].push_back(y - 1);
    f[y - 1].push_back(x - 1);
  }

  for (ll i = 0; i < n; i++) {
    if (!seen[i]) {
      ll cnt = 0;
      dfs(i, f, seen, cnt);
      // print(i,seen, cnt)
      ans = max(ans, cnt);
    }
  }

  cout << ans << endl;

  return 0;
}