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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Sagor
 */

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define gap ' '
#define EPS 1e-9
#define P_INF 2e9 + 17
#define N_INF -2e9 - 17
#define MOD 1000000007
#define PI 2 * acos(0.0)
#define All(x) x.begin(), x.end()
#define Mem(Name, type) memset(Name, type, sizeof(Name))

template <typename T> T GCD(T a, T b) { return a ? GCD(b % a, a) : b; }

template <typename T> T LCM(T a, T b) { return (a * b) / GCD(a, b); }

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  for (auto ob : v)
    os << ob << " ";
  return os;
}

template <typename T, typename S>
std::ostream &operator<<(std::ostream &os, const std::map<T, S> &v) {
  for (auto ob : v)
    os << ob.first << " : " << ob.second << std::endl;
  return os;
}

using ld = double;
using ll = long long int;
using ul = unsigned long long int;
using namespace std;

class CConnectCities {
  vector<bool> visited;
  vector<vector<int>> G;

  void DFS(int cur, int par) {
    if (!visited[cur])
      visited[cur] = true;
    else
      return;
    for (int x : G[cur]) {
      if (x == par)
        continue;
      DFS(x, cur);
    }
  }

public:
  void solve(std::istream &in, std::ostream &out) {
    int n, m;
    in >> n >> m;
    G.resize(n);
    visited.resize(n);
    for (int i = 0; i < m; ++i) {
      int x, y;
      in >> x >> y;
      x--, y--;
      G[x].push_back(y);
      G[y].push_back(x);
    }
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      if (!visited[i]) {
        DFS(i, -1);
        ans++;
      }
    }
    out << ans - 1 << endl;
  }
};

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  CConnectCities solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: 0

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Sagor
 */

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define gap ' '
#define EPS 1e-9
#define P_INF 2e9 + 17
#define N_INF -2e9 - 17
#define MOD 1000000007
#define PI 2 * acos(0.0)
#define All(x) x.begin(), x.end()
#define Mem(Name, type) memset(Name, type, sizeof(Name))

template <typename T> T GCD(T a, T b) { return a ? GCD(b % a, a) : b; }

template <typename T> T LCM(T a, T b) { return (a * b) / GCD(a, b); }

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  for (auto ob : v)
    os << ob << " ";
  return os;
}

template <typename T, typename S>
std::ostream &operator<<(std::ostream &os, const std::map<T, S> &v) {
  for (auto ob : v)
    os << ob.first << " : " << ob.second << std::endl;
  return os;
}

using ld = double;
using ll = long long int;
using ul = unsigned long long int;
using namespace std;

class CConnectCities {
  vector<bool> visited;
  vector<vector<int>> G;

  void DFS(int cur, int par) {
    if (!visited[cur])
      visited[cur] = true;
    for (int x : G[cur]) {
      if (x == par)
        continue;
      DFS(x, cur);
    }
  }

public:
  void solve(std::istream &in, std::ostream &out) {
    int n, m;
    in >> n >> m;
    G.resize(n);
    visited.resize(n);
    for (int i = 0; i < m; ++i) {
      int x, y;
      in >> x >> y;
      x--, y--;
      G[x].push_back(y);
      G[y].push_back(x);
    }
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      if (!visited[i]) {
        DFS(0, -1);
        if (i != 0)
          ans++;
      }
    }
    out << ans << endl;
  }
};

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  CConnectCities solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}