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

#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define debug(var)                                                             \\
  do {                                                                         \\
    std::cout << #var << " : ";                                                \\
    view(var);                                                                 \\
  } while (0)
using ll = long long;
using ld = long double;
inline int num(const std::string &S, int idx) noexcept {
  return S.at(idx) - '0';
}
inline int ctoi(char c) noexcept { return c - '0'; }
inline bool isNumber(char c) noexcept {
  return ('0' <= c && c <= '9' ? true : false);
}
inline int safeCtoi(char c) {
  if (isNumber(c)) {
    return ctoi(c);
  } else {
    throw 1;
  }
}
inline char itoc(int i) noexcept { return i + '0'; }
inline unsigned int bitMask(int i) noexcept { return (1 << i); }
inline bool isTrue(unsigned int bit, int i) noexcept {
  return bit & bitMask(i);
}
inline bool isEven(int num) noexcept { return !num % 2; }
inline bool isOdd(int num) noexcept { return num % 2; }
template <typename T> inline short sgn(T num) noexcept {
  return (num == 0 ? 0 : (num > 0 ? 1 : -1));
}
template <typename T> inline bool isZero(T num) noexcept {
  return (num == 0 ? true : false);
}
template <typename T> inline bool isPositive(T num) noexcept {
  return (num > 0 ? true : false);
}
template <typename T> inline bool isNegative(T num) noexcept {
  return (num < 0 ? true : false);
}
template <typename T> void view(T e) noexcept { cout << e; }
template <typename T> void view(std::vector<T> v) noexcept {
  for (const auto &e : v)
    cout << e << " ";
}
template <typename T> void view(std::vector<std::vector<T>> vv) noexcept {
  for (const auto &v : vv)
    view(v);
}

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

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

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  UnionFind uf(n + 1);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }
  std::set<int> roots;
  for (int i = 1; i < n + 1; ++i) {
    roots.insert(uf.root(i));
  }

  cout << roots.size() - 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define debug(var)                                                             \\
  do {                                                                         \\
    std::cout << #var << " : ";                                                \\
    view(var);                                                                 \\
  } while (0)
using ll = long long;
using ld = long double;
inline int num(const std::string &S, int idx) noexcept {
  return S.at(idx) - '0';
}
inline int ctoi(char c) noexcept { return c - '0'; }
inline bool isNumber(char c) noexcept {
  return ('0' <= c && c <= '9' ? true : false);
}
inline int safeCtoi(char c) {
  if (isNumber(c)) {
    return ctoi(c);
  } else {
    throw 1;
  }
}
inline char itoc(int i) noexcept { return i + '0'; }
inline unsigned int bitMask(int i) noexcept { return (1 << i); }
inline bool isTrue(unsigned int bit, int i) noexcept {
  return bit & bitMask(i);
}
inline bool isEven(int num) noexcept { return !num % 2; }
inline bool isOdd(int num) noexcept { return num % 2; }
template <typename T> inline short sgn(T num) noexcept {
  return (num == 0 ? 0 : (num > 0 ? 1 : -1));
}
template <typename T> inline bool isZero(T num) noexcept {
  return (num == 0 ? true : false);
}
template <typename T> inline bool isPositive(T num) noexcept {
  return (num > 0 ? true : false);
}
template <typename T> inline bool isNegative(T num) noexcept {
  return (num < 0 ? true : false);
}
template <typename T> void view(T e) noexcept { cout << e; }
template <typename T> void view(std::vector<T> v) noexcept {
  for (const auto &e : v)
    cout << e << " ";
}
template <typename T> void view(std::vector<std::vector<T>> vv) noexcept {
  for (const auto &v : vv)
    view(v);
}

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

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

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    uf.unite(a, b);
  }
  std::set<int> roots;
  for (int i = 1; i < n; ++i) {
    roots.insert(uf.root(i));
  }
  cout << roots.size() << endl;
  return 0;
}