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

#include <bits/stdc++.h>
using namespace std;
#define FASTio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
#define endl '\\n'
ll mod = 1e9 + 7;

class Graph {
  // No. of vertices
  int V;

  // Pointer to an array containing adjacency lists
  list<int> *adj;

  // A function used by DFS
  void DFSUtil(int v, bool visited[]);

public:
  // Constructor
  Graph(int V);

  void addEdge(int v, int w);
  int NumberOfconnectedComponents();
};

// Function to return the number of
// connected components in an undirected graph
int Graph::NumberOfconnectedComponents() {

  // Mark all the vertices as not visited
  bool *visited = new bool[V];

  // To store the number of connected components
  int count = 0;
  for (int v = 1; v <= V; v++)
    visited[v] = false;

  for (int v = 1; v <= V; v++) {
    if (visited[v] == false) {
      DFSUtil(v, visited);
      count += 1;
    }
  }

  return count;
}

void Graph::DFSUtil(int v, bool visited[]) {

  // Mark the current node as visited
  visited[v] = true;

  // Recur for all the vertices
  // adjacent to this vertex
  list<int>::iterator i;

  for (i = adj[v].begin(); i != adj[v].end(); ++i)
    if (!visited[*i])
      DFSUtil(*i, visited);
}

Graph::Graph(int V) {
  this->V = V;
  adj = new list<int>[V + 1];
}

// Add an undirected edge
void Graph::addEdge(int v, int w) {
  adj[v].push_back(w);
  adj[w].push_back(v);
}

int main() {
  FASTio;
#ifndef ONLINE_JUDGE // file start
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // file end
  int n, m, x, y, i;
  cin >> n >> m;
  Graph g(n);
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    g.addEdge(x, y);
  }
  cout << g.NumberOfconnectedComponents() - 1 << "\\n";
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FASTio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
#define endl '\\n'
ll mod = 1e9 + 7;

class Graph {
  int V;

  list<int> *adj;

  void DFSUtil(int v, bool visited[]);

public:
  // Constructor
  Graph(int V);

  void addEdge(int v, int w);
  int NumberOfconnectedComponents();
};

// Function to return the number of
// connected components in an undirected graph
int Graph::NumberOfconnectedComponents() {

  // Mark all the vertices as not visited
  bool *visited = new bool[V];

  // To store the number of connected components
  int count = 0;
  for (int v = 0; v < V; v++)
    visited[v] = false;

  for (int v = 0; v < V; v++) {
    if (visited[v] == false) {
      DFSUtil(v, visited);
      count += 1;
    }
  }

  return count;
}

void Graph::DFSUtil(int v, bool visited[]) {

  // Mark the current node as visited
  visited[v] = true;

  // Recur for all the vertices
  // adjacent to this vertex
  list<int>::iterator i;

  for (i = adj[v].begin(); i != adj[v].end(); ++i)
    if (!visited[*i])
      DFSUtil(*i, visited);
}

Graph::Graph(int V) {
  this->V = V;
  adj = new list<int>[V];
}

// Add an undirected edge
void Graph::addEdge(int v, int w) {
  adj[v].push_back(w);
  adj[w].push_back(v);
}

int main() {
  FASTio;
#ifndef ONLINE_JUDGE // file start
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // file end
  int n, m, x, y, i;
  cin >> n >> m;
  Graph g(n);
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    g.addEdge(x, y);
  }
  cout << g.NumberOfconnectedComponents() - 1 << "\\n";
  return 0;
}