Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
/*
#include<algorithm>
#include<array>
#include<cassert>
#include<cmath>
#include<cstdlib>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<typeinfo>
#include<utility>
#include<vector>
*/
#define int long long int
#define double long double
using namespace std;
#define MOD 1000000007
#define INF 1000000000000000007
const int MAX_N = 1 << 17;
#define rep(i, n) for (int(i) = 0, i##_len = (n); (i) < i##_len; (i)++)
#define reps(i, x) for (int(i) = 1; (i) <= (int)(x); (i)++)
#define rrep(i, x) for (int(i) = ((int)(x)-1); (i) >= 0; (i)--)
#define rreps(i, x) for (int(i) = ((int)(x)); (i) > 0; (i)--)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define pb push_back
#define mp make_pair
#define bit(n) ((int)(1) << (n))
#define all(x) (x).begin(), (x).end()
#define debug(x) std::cout << #x << ": " << (x) << std::endl
#define nint int
using namespace std;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
typedef pair<int, int> pii;
typedef pair<double, double> dop;
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;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} aaaaaaa;

int par[100000 + 1];   // 親
int trank[100000 + 1]; // 木の深さ

// n要素で初期化
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    trank[i] = 0;
  }
}

// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// xとyの属する集合を併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (trank[x] < trank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (trank[x] == trank[y])
      trank[x]++;
  }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }

signed main() {
  int n, m;
  cin >> n >> m;
  int out = 0;
  init(n);
  std::vector<std::vector<int>> xyz(n, std::vector<int>(3));
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (z % 2 == 0) {
      z = 0;
    } else {
      z = 1;
    }
    x--;
    y--;
    xyz[i][0] = x;
    xyz[i][1] = y;
    xyz[i][2] = z;
    unite(x, y);
  }
  for (int i = 0; i < n; i++) {
    if (find(i) == i) {
      out++;
    }
  }
  cout << out << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
/*
#include<algorithm>
#include<array>
#include<cassert>
#include<cmath>
#include<cstdlib>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<typeinfo>
#include<utility>
#include<vector>
*/
#define int long long int
#define double long double
using namespace std;
#define MOD 1000000007
#define INF 1000000000000000007
const int MAX_N = 1 << 17;
#define rep(i, n) for (int(i) = 0, i##_len = (n); (i) < i##_len; (i)++)
#define reps(i, x) for (int(i) = 1; (i) <= (int)(x); (i)++)
#define rrep(i, x) for (int(i) = ((int)(x)-1); (i) >= 0; (i)--)
#define rreps(i, x) for (int(i) = ((int)(x)); (i) > 0; (i)--)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define pb push_back
#define mp make_pair
#define bit(n) ((int)(1) << (n))
#define all(x) (x).begin(), (x).end()
#define debug(x) std::cout << #x << ": " << (x) << std::endl
#define nint int
using namespace std;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
typedef pair<int, int> pii;
typedef pair<double, double> dop;
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;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} aaaaaaa;

int par[100000 + 1];   // 親
int trank[100000 + 1]; // 木の深さ

// n要素で初期化
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    trank[i] = 0;
  }
}

// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// xとyの属する集合を併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (trank[x] < trank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (trank[x] == trank[y])
      trank[x]++;
  }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }

signed main() {
  int n, m;
  cin >> n >> m;
  int out = 0;
  init(n);
  std::vector<std::vector<int>> xyz(m, std::vector<int>(3));
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (z % 2 == 0) {
      z = 0;
    } else {
      z = 1;
    }
    x--;
    y--;
    xyz[i][0] = x;
    xyz[i][1] = y;
    xyz[i][2] = z;
    unite(x, y);
  }
  for (int i = 0; i < n; i++) {
    if (find(i) == i) {
      out++;
    }
  }
  cout << out << endl;
  return 0;
}