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

// #include "bits/stdc++.h"
#include <bits/stdc++.h>
using namespace std;
// using namespace atcoder;
using ll = long long;

template <typename T>
void print_vec(const vector<T> &v, bool is_reverse = false, ll num = 0) {
  if (num == 0)
    num = (ll)v.size();
  cout << endl;
  cout << "i=  ";
  for (ll i = 0; i < num; i++)
    cout << i << "   ";
  cout << endl;
  cout << "    ";
  if (is_reverse)
    for (ll i = num - 1; i >= 0; i--) {
      cout << v[i];
      if (i != 0)
        cout << "   ";
    }
  else
    for (ll i = 0; i < num; i++) {
      cout << v[i];
      if (i != num - 1)
        cout << "   ";
    }
  cout << endl;
}

// template <typename T>
// void print_pairvec(const vector<T> &v, ll num=0){
//   if(num == 0) num = (ll)v.size();
//   cout << endl; for(ll i=0; i<num; i++){ cout << v[i].first << " " <<
//   v[i].second << endl;}
// }
template <typename T> void print_pairvec(const T &_pair, ll num = 0) {
  cout << endl;
  for (pair<ll, int> x : _pair) {
    cout << x.first << " " << x.second << endl;
  }
}

template <typename T> void print_vec2(const vector<vector<T>> &v) {
  cout << endl;
  cout << "       ";
  for (ll i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (ll i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (ll j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

// UnionFind
// coding: https://youtu.be/TdR816rqc3s?t=726
// comment: https://youtu.be/TdR816rqc3s?t=6822
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};
int main() {
  ll n, m;
  cin >> n >> m;

  vector<ll> a(m), b(m);
  for (int i = 0; i < m; i++)
    cin >> a[i] >> b[i];

  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    uf.unite(a[i] - 1, b[i] - 1);
  }

  // vector<bool> is_root(n, false);
  // for(int i=0; i<n; i++){
  //   ll x = uf.find(i);
  //   if(x == -1) is_root[i] = true;
  //   else is_root[x] = true;
  // }

  // ll cnt = 0;
  // for(int i=0; i<n; i++) if(is_root[i]) cnt++;

  ll cnt = 0;
  for (int i = 0; i < n; i++)
    if (uf.d[i] < 0)
      cnt++;
  ll ans = cnt - 1;
  cout << ans << endl;

  return 0;
  // cout << fixed;//小数点を確実に10桁で表示するためのもの
  // cout << setprecision(10);
}


Error: 0

Fixed submission:

// #include "bits/stdc++.h"
#include <bits/stdc++.h>
using namespace std;
// using namespace atcoder;
using ll = long long;

template <typename T>
void print_vec(const vector<T> &v, bool is_reverse = false, ll num = 0) {
  if (num == 0)
    num = (ll)v.size();
  cout << endl;
  cout << "i=  ";
  for (ll i = 0; i < num; i++)
    cout << i << "   ";
  cout << endl;
  cout << "    ";
  if (is_reverse)
    for (ll i = num - 1; i >= 0; i--) {
      cout << v[i];
      if (i != 0)
        cout << "   ";
    }
  else
    for (ll i = 0; i < num; i++) {
      cout << v[i];
      if (i != num - 1)
        cout << "   ";
    }
  cout << endl;
}

// template <typename T>
// void print_pairvec(const vector<T> &v, ll num=0){
//   if(num == 0) num = (ll)v.size();
//   cout << endl; for(ll i=0; i<num; i++){ cout << v[i].first << " " <<
//   v[i].second << endl;}
// }
template <typename T> void print_pairvec(const T &_pair, ll num = 0) {
  cout << endl;
  for (pair<ll, int> x : _pair) {
    cout << x.first << " " << x.second << endl;
  }
}

template <typename T> void print_vec2(const vector<vector<T>> &v) {
  cout << endl;
  cout << "       ";
  for (ll i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (ll i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (ll j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

// UnionFind
// coding: https://youtu.be/TdR816rqc3s?t=726
// comment: https://youtu.be/TdR816rqc3s?t=6822
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};
int main() {
  ll n, m;
  cin >> n >> m;

  vector<ll> a(m), b(m);
  for (int i = 0; i < m; i++)
    cin >> a[i] >> b[i];

  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    uf.unite(a[i], b[i]);
  }

  // vector<bool> is_root(n, false);
  // for(int i=0; i<n; i++){
  //   ll x = uf.find(i);
  //   if(x == -1) is_root[i] = true;
  //   else is_root[x] = true;
  // }

  // ll cnt = 0;
  // for(int i=0; i<n; i++) if(is_root[i]) cnt++;

  ll cnt = 0;
  for (int i = 0; i < n; i++)
    if (uf.d[i] < 0)
      cnt++;
  ll ans = cnt - 1;
  cout << ans << endl;

  return 0;
  // cout << fixed;//小数点を確実に10桁で表示するためのもの
  // cout << setprecision(10);
}