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

#include <bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll, ll>
#define mod 1000000007 // 998244353
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define srt(v)                                                                 \\
  if (!v.empty())                                                              \\
  sort(v.begin(), v.end())
#define PI 3.141592653589793238
#define pq priority_queue<ll>
#define pqr priority_queue<ll, vector<ll>, greater<ll>()>
using namespace std;
ll pow_mod(ll a, ll b) {
  ll res = 1;
  while (b != 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
void solve() {
  ll n, m;

  cin >> n >> m;

  vector<ll> adj[n + 1];

  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;

    adj[x].pb(y);
    adj[y].pb(x);
  }

  bool mark[n + 1];
  memset(mark, false, sizeof(mark));

  ll cnt = 0;

  for (ll i = 1; i <= n; i++) {
    if (!mark[i]) {
      cnt++;
      queue<ll> q;

      q.push(i);

      mark[i] = true;

      while (!q.empty()) {
        ll x = q.front();
        mark[x] = true;
        q.pop();

        for (auto a : adj[x]) {
          if (!mark[a]) {
            mark[a] = true;
            q.push(a);
          }
        }
      }
    }
  }

  cnt--;

  cout << cnt << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // Start from Here.

  ll t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();

  // Good Bye!
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll, ll>
#define mod 1000000007 // 998244353
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define srt(v)                                                                 \\
  if (!v.empty())                                                              \\
  sort(v.begin(), v.end())
#define PI 3.141592653589793238
#define pq priority_queue<ll>
#define pqr priority_queue<ll, vector<ll>, greater<ll>()>
using namespace std;
ll pow_mod(ll a, ll b) {
  ll res = 1;
  while (b != 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
void solve() {
  ll n, m;

  cin >> n >> m;

  vector<ll> adj[n + 1];

  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;

    adj[x].pb(y);
    adj[y].pb(x);
  }

  bool mark[n + 1];
  memset(mark, false, sizeof(mark));

  ll cnt = 0;

  function<void(ll, ll)> dfs = [&](ll s, ll p) {
    mark[s] = true;

    for (auto a : adj[s]) {
      if (a == p)
        continue;

      dfs(a, s);
    }
  };

  for (ll i = 1; i <= n; i++) {
    if (!mark[i]) {
      cnt++;
      dfs(i, -1);
    }
  }

  cnt--;

  cout << cnt << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // Start from Here.

  ll t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();

  // Good Bye!
  return 0;
}