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

/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;*/
#include <algorithm>
#include <bits/stdc++.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector<ll>
#define pii pair<ll, ll>
#define bc __builtin_popcountll
#define vvi vector<vi>
#define vpii vector<pair<ll, ll>>
#define mll unordered_map<ll, ll>
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN
#define fo(i, a, b) for (i = a; i <= b; i++)
#define f(i, n) for (ll i = 0; i < n; i++)
#define f1(i, n) for (ll i = 1; i <= n; i++)
#define fb(i, n) for (ll i = n - 1; i >= 0; i--)
#define fb1(i, n) for (ll i = n; i > 0; i--)
#define endd cout << '\\n';
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define fr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define all(x) x.begin(), x.end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define CASES                                                                  \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
typedef long long ll;
typedef unsigned long long ull;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
// indexed_set; typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
//*X.find_by_order()
// X.order_of_key()
struct cmp {
  bool operator()(ll a, ll b) { return (a > b); }
};
ll ceil(const ll &a, const ll &b) {
  if (a % b == 0)
    return a / b;
  return a / b + 1;
}
ll mod = 1000000007;
const ll N = 200005;
vi adj[N];
ll vis[N];
void dfs(ll u) {
  vis[u] = 1;
  for (ll v : adj[u]) {
    if (vis[v] == 0) {
      dfs(v);
    }
  }
}
int main() {
  fast;
  ll n, m;
  cin >> n >> m;
  clr(vis);
  f(i, m) {
    ll x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  ll c = 0;
  f1(i, n) {
    if (vis[i] == 0) {
      dfs(i);
      c++;
    }
  }
  cout << c - 1;
  return 0;
}

Error: WA

Fixed submission:

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;

// Graph class represents a undirected graph
// using adjacency list representation
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 = 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);
}

// Driver code
int main() {
  int n, m;
  cin >> n >> m;
  Graph g(n);
  int a, b;
  while (m--) {
    cin >> a >> b;
    g.addEdge(a, b);
  }

  cout << g.NumberOfconnectedComponents() - 1;

  return 0;
}