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

#include "bits/stdc++.h"
#define rep(i, b) for (ll i = 0; i < b; i++)
#define ll long long
using namespace std;
/*--Input//////////////////////////////////////////////////*/
inline void IN(void) { return; }
template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}
#define SS(T, ...)                                                             \\
  T __VA_ARGS__;                                                               \\
  IN(__VA_ARGS__);
#define SV(type, c, n)                                                         \\
  vector<type> c(n);                                                           \\
  for (auto &i : c)                                                            \\
    cin >> i;
/*--Output/////////////////////////////////////////////////*/
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
inline int p(void) {
  cout << endl;
  return 0;
}
template <class Head> int p(Head &&head) {
  cout << head;
  p();
  return 0;
}
template <class Head, class... Tail> int p(Head &&head, Tail &&...tail) {
  cout << head << " ";
  p(forward<Tail>(tail)...);
  return 0;
}
struct ProconInit {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;

  ProconInit() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(IOS_PREC);
    if (AUTOFLUSH)
      cout << unitbuf;
  }
} PROCON_INIT;

#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr << #x << " " << x << '\\n';
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL << (n))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool cmp(pair<ll, ll> a, pair<ll, ll> b) { return a.second < b.second; }

ll MOD = 1000000007;

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool same(int x, int y) { return root(x) == root(y); }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return -par[root(x)]; }
};
int main() {
  vector<pair<int, int>> Q;
  SS(int, n, m)
  UnionFind uf(n);
  rep(i, m) {
    SS(int, a, b);
    a--;
    b--;
    uf.unite(a, b);
  }
  int now = 0;
  rep(i, n) Q.push_back(make_pair(uf.root(i), uf.size(i)));
  sort(rbegin(Q), rend(Q));
  for (auto V : Q) {
    if (now != V.first)
      n -= V.second - 1;
    now = V.first;
  }
  p(n - 1);
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, b) for (ll i = 0; i < b; i++)
#define ll long long
using namespace std;
/*--Input//////////////////////////////////////////////////*/
inline void IN(void) { return; }
template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}
#define SS(T, ...)                                                             \\
  T __VA_ARGS__;                                                               \\
  IN(__VA_ARGS__);
#define SV(type, c, n)                                                         \\
  vector<type> c(n);                                                           \\
  for (auto &i : c)                                                            \\
    cin >> i;
/*--Output/////////////////////////////////////////////////*/
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
inline int p(void) {
  cout << endl;
  return 0;
}
template <class Head> int p(Head &&head) {
  cout << head;
  p();
  return 0;
}
template <class Head, class... Tail> int p(Head &&head, Tail &&...tail) {
  cout << head << " ";
  p(forward<Tail>(tail)...);
  return 0;
}
struct ProconInit {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;

  ProconInit() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(IOS_PREC);
    if (AUTOFLUSH)
      cout << unitbuf;
  }
} PROCON_INIT;

#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr << #x << " " << x << '\\n';
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL << (n))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool cmp(pair<ll, ll> a, pair<ll, ll> b) { return a.second < b.second; }

ll MOD = 1000000007;

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool same(int x, int y) { return root(x) == root(y); }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
int main() {
  vector<pair<int, int>> Q;
  SS(int, n, m)
  UnionFind uf(n);
  rep(i, m) {
    SS(int, a, b);
    uf.unite(a, b);
  }
  int now = 0;
  for (int i = 1; i <= n; i++)
    Q.push_back(make_pair(uf.root(i), uf.size(i)));
  sort(rbegin(Q), rend(Q));
  for (auto V : Q) {
    if (now != V.first)
      n -= V.second - 1;
    now = V.first;
  }
  p(n - 1);
}