Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

// types
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;

// macros
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define REP1(i, n) for (int i = 1; i < ((int)n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define PB push_back
#define EB emplace_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

struct UnionFind {
  vll par;
  vll rank;

  UnionFind(ll n) {
    par = vll(n);
    rank = vll(n, 1);
    REP(i, n)
    par[i] = i;
  }

  ll find(ll x) {
    if (par[x] == x)
      return x;
    else {
      par[x] = find(par[x]);
      return par[x];
    }
  }

  void unite(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    else {
      if (rank[x] < rank[y])
        par[x] = y;
      else {
        par[y] = x;
        if (rank[x] == rank[y])
          rank[y]++;
      }
    }
  }

  bool same(ll x, ll y) { return find(x) == find(y); }

  ll size(ll x) { return rank[x]; }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, m;
  cin >> n >> m;

  vector<pll> xy(m);
  vll z(n);
  REP(i, m) {
    cin >> xy[i].FI >> xy[i].SE >> z[i];
    xy[i].FI--;
    xy[i].SE--;
  }

  UnionFind uf(n);
  sort(xy.begin(), xy.end());
  REP(i, m) {
    ll x = xy[i].FI;
    ll y = xy[i].SE;
    uf.unite(x, y);
  }

  vll a;
  REP(i, n) { a.push_back(uf.find(i)); }

  sort(a.begin(), a.end());
  auto it = unique(a.begin(), a.end());
  a.erase(it, a.end());

  cout << a.size() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

// types
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;

// macros
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define REP1(i, n) for (int i = 1; i < ((int)n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define PB push_back
#define EB emplace_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

struct UnionFind {
  vll par;
  vll rank;

  UnionFind(ll n) {
    par = vll(n);
    rank = vll(n, 1);
    REP(i, n)
    par[i] = i;
  }

  ll find(ll x) {
    if (par[x] == x)
      return x;
    else {
      par[x] = find(par[x]);
      return par[x];
    }
  }

  void unite(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    else {
      if (rank[x] < rank[y])
        par[x] = y;
      else {
        par[y] = x;
        if (rank[x] == rank[y])
          rank[y]++;
      }
    }
  }

  bool same(ll x, ll y) { return find(x) == find(y); }

  ll size(ll x) { return rank[x]; }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, m;
  cin >> n >> m;

  vector<pll> xy(m);
  vll z(m);
  REP(i, m) {
    cin >> xy[i].FI >> xy[i].SE >> z[i];
    xy[i].FI--;
    xy[i].SE--;
  }

  UnionFind uf(n);
  sort(xy.begin(), xy.end());
  REP(i, m) {
    ll x = xy[i].FI;
    ll y = xy[i].SE;
    uf.unite(x, y);
  }

  vll a;
  REP(i, n) { a.push_back(uf.find(i)); }

  sort(a.begin(), a.end());
  auto it = unique(a.begin(), a.end());
  a.erase(it, a.end());

  cout << a.size() << endl;
}