Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename M, typename N>
istream &operator>>(istream &is, pair<M, N> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename M> istream &operator>>(istream &is, vector<M> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
template <typename M> istream &operator>>(istream &is, vector<vector<M>> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
template <typename M, typename N>
ostream &operator<<(ostream &os, const pair<M, N> &p) {
  os << p.first << ' ' << p.second << '\n';
  return os;
}
template <typename M> ostream &operator<<(ostream &os, const vector<M> &v) {
  for (auto it : v)
    os << it << ' ';
  cout << '\n';
  return os;
}
template <typename M>
ostream &operator<<(ostream &os, const vector<vector<M>> &v) {
  for (auto it : v)
    os << it;
  return os;
}

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mn(x) *min_element((x).begin(), (x).end())
#define mx(x) *max_element((x).begin(), (x).end())
#define acc(x) accumulate((x).begin(), (x).end(), 0ll)
#define eb emplace_back
#define el '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vs;

const ld pi = acos(-1);
const ll inf = (ll)1e9 + 10;
// const int mod=1000000007;
const int mod = 998244353;

int n, m, k;
const int N = 2e5 + 10;

vvi adj(N);
vb mark(N);

void dfs(int u, int p) {
  mark[u] = true;
  for (auto it : adj[u]) {
    if (it != p) {
      dfs(it, u);
    }
  }
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0, u, v; i < m; i++) {
    cin >> u >> v;
    adj[--u].pb(--v);
    adj[v].pb(u);
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    if (!mark[i]) {
      ans++;
      dfs(i, i);
    }
  }
  cout << ans << el;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename M, typename N>
istream &operator>>(istream &is, pair<M, N> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename M> istream &operator>>(istream &is, vector<M> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
template <typename M> istream &operator>>(istream &is, vector<vector<M>> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
template <typename M, typename N>
ostream &operator<<(ostream &os, const pair<M, N> &p) {
  os << p.first << ' ' << p.second << '\n';
  return os;
}
template <typename M> ostream &operator<<(ostream &os, const vector<M> &v) {
  for (auto it : v)
    os << it << ' ';
  cout << '\n';
  return os;
}
template <typename M>
ostream &operator<<(ostream &os, const vector<vector<M>> &v) {
  for (auto it : v)
    os << it;
  return os;
}

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mn(x) *min_element((x).begin(), (x).end())
#define mx(x) *max_element((x).begin(), (x).end())
#define acc(x) accumulate((x).begin(), (x).end(), 0ll)
#define eb emplace_back
#define el '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vs;

const ld pi = acos(-1);
const ll inf = (ll)1e9 + 10;
// const int mod=1000000007;
const int mod = 998244353;

int n, m, k;
const int N = 2e5 + 10;

vvi adj(N);
vb mark(N);

void dfs(int u, int p) {
  mark[u] = true;
  for (auto it : adj[u]) {
    if (it != p && !mark[it]) {
      dfs(it, u);
    }
  }
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0, u, v; i < m; i++) {
    cin >> u >> v;
    adj[--u].pb(--v);
    adj[v].pb(u);
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    if (!mark[i]) {
      ans++;
      dfs(i, i);
    }
  }
  cout << ans << el;

  return 0;
}