Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <vector>

#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;

class UnionFind {
  vector<int> par;
  vector<int> myrank;
  vector<int> size;

public:
  UnionFind(int n) {
    par.resize(n);
    myrank.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      myrank[i] = 0;
      size[i] = 1;
    }
  }

  // 木の根を求める
  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 (myrank[x] < myrank[y]) {
      par[x] = y;
      size[y] += size[x];
    } else {
      par[y] = x;
      size[x] += size[y];
      if (myrank[x] == myrank[y])
        myrank[x]++;
    }
  }

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

  // xが属する集合の大きさを返す
  int getSize(int x) { return size[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);

  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    uf.unite(x, y);
  }

  vector<bool> res(n, false);
  int count = 0;
  for (int i = 0; i < n; i++) {
    int p = uf.find(i);
    if (!res[p]) {
      count++;
      res[p] = true;
    }
  }

  cout << count << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <vector>

#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;

class UnionFind {
  vector<int> par;
  vector<int> myrank;
  vector<int> size;

public:
  UnionFind(int n) {
    par.resize(n);
    myrank.resize(n);
    size.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      myrank[i] = 0;
      size[i] = 1;
    }
  }

  // 木の根を求める
  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 (myrank[x] < myrank[y]) {
      par[x] = y;
      size[y] += size[x];
    } else {
      par[y] = x;
      size[x] += size[y];
      if (myrank[x] == myrank[y])
        myrank[x]++;
    }
  }

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

  // xが属する集合の大きさを返す
  int getSize(int x) { return size[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);

  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    uf.unite(x, y);
  }

  vector<bool> res(n, false);
  int count = 0;
  for (int i = 0; i < n; i++) {
    int p = uf.find(i);
    if (!res[p]) {
      count++;
      res[p] = true;
    }
  }

  cout << count << endl;

  return 0;
}