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

#include <bits/stdc++.h>

using namespace std;

#define G getline
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define T top()
#define E end()
#define EM empty()
#define V vector
#define Q queue
#define DQ deque
#define PQ priority_queue
#define ST stack
#define FI first
#define SE second
#define PI acos(-1)
#define PS push
#define PP pop()
#define PSF push_front
#define PSB push_back
#define PPF pop_front()
#define PPB pop_back()
#define LL long long
#define ULL unsigned long long
#define PII pair<int, int>
#define PSI pair<string, int>
#define PIS pair<int, string>
#define PLI pair<LL, int>
#define PLL pair<LL, LL>
#define MII map<int, int>
#define MSI map<string, int>
#define MIS map<int, string>
#define MLI map<LL, int>
#define FAST()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

// int dx[]={-1,1,0,0};
// int dy[]={0,0,-1,1};
// int dx[]={-1,0,1,-1,1,-1,0,1};
// int dy[]={1,1,1,0,0,-1,-1,-1};

const int N = 1e5 + 5;
vector<int> adj[N];
bool visited[N];

void DFS(int cur, int par) {
  if (visited[cur])
    return;
  visited[cur] = 1;
  for (int x : adj[cur]) {
    if (x ^ par)
      DFS(x, cur);
  }
}

int main() {
  FAST();
  int i, m, n, x, y, ans = 0;
  cin >> n >> m;
  while (m--) {
    cin >> x >> y;
    adj[x].emplace_back(y);
    adj[y].emplace_back(x);
  }
  for (i = 1; i <= n; i++) {
    if (!visited[i]) {
      ++ans;
      DFS(i, i);
    }
  }
  cout << ans - 1;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define G getline
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define T top()
#define E end()
#define EM empty()
#define V vector
#define Q queue
#define DQ deque
#define PQ priority_queue
#define ST stack
#define FI first
#define SE second
#define PI acos(-1)
#define PS push
#define PP pop()
#define PSF push_front
#define PSB push_back
#define PPF pop_front()
#define PPB pop_back()
#define LL long long
#define ULL unsigned long long
#define PII pair<int, int>
#define PSI pair<string, int>
#define PIS pair<int, string>
#define PLI pair<LL, int>
#define PLL pair<LL, LL>
#define MII map<int, int>
#define MSI map<string, int>
#define MIS map<int, string>
#define MLI map<LL, int>
#define FAST()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

// int dx[]={-1,1,0,0};
// int dy[]={0,0,-1,1};
// int dx[]={-1,0,1,-1,1,-1,0,1};
// int dy[]={1,1,1,0,0,-1,-1,-1};

const int N = 1e5 + 5;
vector<int> adj[N];
bool visited[N];

void DFS(int cur, int par) {
  visited[cur] = 1;
  for (int x : adj[cur]) {
    if (x ^ par)
      DFS(x, cur);
  }
}

int main() {
  FAST();
  int i, m, n, x, y, ans = 0;
  cin >> n >> m;
  while (m--) {
    cin >> x >> y;
    adj[x].emplace_back(y);
    adj[y].emplace_back(x);
  }
  for (i = 1; i <= n; i++) {
    if (!visited[i]) {
      ++ans;
      DFS(i, i);
    }
  }
  cout << ans - 1;
  return 0;
}