Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i, a, b) for (ll i = (a), __last_##i = (b); i < __last_##i; i++)
#define RFOR(i, a, b) for (ll i = (b)-1, __last_##i = (a); i >= __last_##i; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define vi vector<int>
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  REP(i, v.size()) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  REP(i, v.size()) {
    if (i)
      os << endl;
    os << v[i];
  }
  return os;
}

class UnionFind {
private:
  vector<int> par, rank, sizes;
  int tree;

public:
  explicit UnionFind(const int n) : tree(n) {
    par.resize(n);
    rank.resize(n);
    sizes.resize(n);
    rep(i, n) {
      par[i] = i;
      sizes[i] = 1;
    }
  }

  ~UnionFind() = default;

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

  int find(int x) { return root(x); }

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;

    if (rank[x] < rank[y])
      swap(x, y);
    par[y] = x;
    tree--;
    sizes[x] += sizes[y];
    if (rank[x] == rank[y])
      rank[x]++;
  }

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

  int size(int x) { return sizes[x]; }

  int sumTree() { return tree; }
};

class segmentTree {};

class ModInt {};

class topos {};

int main() {

  int n, m;
  cin >> n >> m;
  UnionFind u(n);

  rep(i, m) {
    int x, y, z;
    cin >> x >> y >> z;

    u.unite(x, y);
  }

  cout << u.sumTree() << endl;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i, a, b) for (ll i = (a), __last_##i = (b); i < __last_##i; i++)
#define RFOR(i, a, b) for (ll i = (b)-1, __last_##i = (a); i >= __last_##i; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define vi vector<int>
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  REP(i, v.size()) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  REP(i, v.size()) {
    if (i)
      os << endl;
    os << v[i];
  }
  return os;
}

class UnionFind {
private:
  vector<int> par, rank, sizes;
  int tree;

public:
  explicit UnionFind(const int n) : tree(n) {
    par.resize(n);
    rank.resize(n);
    sizes.resize(n);
    rep(i, n) {
      par[i] = i;
      sizes[i] = 1;
    }
  }

  ~UnionFind() = default;

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

  int find(int x) { return root(x); }

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;

    if (rank[x] < rank[y])
      swap(x, y);
    par[y] = x;
    tree--;
    sizes[x] += sizes[y];
    if (rank[x] == rank[y])
      rank[x]++;
  }

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

  int size(int x) { return sizes[x]; }

  int sumTree() { return tree; }
};

class segmentTree {};

class ModInt {};

class topos {};

int main() {

  int n, m;
  cin >> n >> m;
  UnionFind u(n);

  rep(i, m) {
    int x, y, z;
    cin >> x >> y >> z;

    u.unite(x - 1, y - 1);
  }

  cout << u.sumTree() << endl;
}