Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h> // include all standard C++ libraries

using namespace std;

// Loops
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define _rrep(i, n) rrepi(i, n, 0)
#define rrepi(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)

#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define each(xi, x) for (auto &&xi : x)
// Note: we can use rep(i,N) or rep(i,from,to)

// typedef
using ll = long long;
template <class T> using vec = vector<T>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;

// Constants

// Shorter repr for frequently used terms
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second

// Algorithms
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(all(v)), v.end())
#define perm(c)                                                                \
  sort(all(c));                                                                \
  for (bool c##p = 1; c##p; c##p = next_permutation(all(c)))

template <class T> pair<T, size_t> max(vector<T> &x) {
  auto it = max_element(all(x));
  return mp(*it, it - x.begin());
}
template <class T> pair<T, size_t> min(vector<T> &x) {
  auto it = min_element(all(x));
  return mp(*it, it - x.begin());
}

template <class T> inline bool chmax(T &maxval, const T &newval) {
  if (maxval < newval) {
    maxval = newval;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &minval, const T &newval) {
  if (minval > newval) {
    minval = newval;
    return 1;
  }
  return 0;
}

// Utilities
// Grid world utilities
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
#define inside(H, W, y, x) 0 <= (x) && (x) < (W) && 0 <= (y) && (y) < (H)

inline int in() {
  int x;
  cin >> x;
  return x;
} // read int from cin
inline ll IN() {
  ll x;
  cin >> x;
  return x;
} // read ll from cin

// Debug
#ifdef LOCAL
#include "dump.hpp"
#define debug(x) cerr << #x << ": " << x << '\n'
#else
#define dump(...)
#define debug(x)
#endif

// Paste snippets here!!
struct UnionFind {
  int n;
  std::vector<int> par;
  std::vector<int> rank;
  UnionFind(int n_) : par(n_), rank(n_, 0), n(n_) {
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }
  int root(int x) { return par[x] == x ? x : (par[x] = root(par[x])); }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
};

//

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false); // Magic for faster cin
  // 連結成分の数を数えるだけ

  int N, M;
  cin >> N >> M;
  vi X(N), Y(N);
  int z;

  UnionFind uf(N);
  rep(i, M) {
    cin >> X[i] >> Y[i] >> z;
    uf.unite(X[i] - 1, Y[i] - 1);
  }
  unordered_set<int> S;
  rep(i, N) { S.insert(uf.root(i)); }
  cout << S.size() << endl;

  // vector<vector<int>> G(N);
  // vector<int> c(N,-1);

  // rep(i,M){
  //   G[X[i]-1].push_back(Y[i]-1);
  //   G[Y[i]-1].push_back(X[i]-1);
  // }

  // int m=1;
  // rep(i,N){
  //   if(c[i]>=0) continue;
  //   c[i] = m;
  //   stack<int> s;
  //   s.push(i);
  //   while(!s.empty()){
  //     int t = s.top(); s.pop();
  //     for(int p : G[t]){
  //       if(c[p]>=0) continue;
  //       c[p] = m; s.push(p);
  //     }
  //   }
  //   m++;
  // }
  // cout << m-1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h> // include all standard C++ libraries

using namespace std;

// Loops
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define _rrep(i, n) rrepi(i, n, 0)
#define rrepi(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)

#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define each(xi, x) for (auto &&xi : x)
// Note: we can use rep(i,N) or rep(i,from,to)

// typedef
using ll = long long;
template <class T> using vec = vector<T>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;

// Constants

// Shorter repr for frequently used terms
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second

// Algorithms
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(all(v)), v.end())
#define perm(c)                                                                \
  sort(all(c));                                                                \
  for (bool c##p = 1; c##p; c##p = next_permutation(all(c)))

template <class T> pair<T, size_t> max(vector<T> &x) {
  auto it = max_element(all(x));
  return mp(*it, it - x.begin());
}
template <class T> pair<T, size_t> min(vector<T> &x) {
  auto it = min_element(all(x));
  return mp(*it, it - x.begin());
}

template <class T> inline bool chmax(T &maxval, const T &newval) {
  if (maxval < newval) {
    maxval = newval;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &minval, const T &newval) {
  if (minval > newval) {
    minval = newval;
    return 1;
  }
  return 0;
}

// Utilities
// Grid world utilities
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
#define inside(H, W, y, x) 0 <= (x) && (x) < (W) && 0 <= (y) && (y) < (H)

inline int in() {
  int x;
  cin >> x;
  return x;
} // read int from cin
inline ll IN() {
  ll x;
  cin >> x;
  return x;
} // read ll from cin

// Debug
#ifdef LOCAL
#include "dump.hpp"
#define debug(x) cerr << #x << ": " << x << '\n'
#else
#define dump(...)
#define debug(x)
#endif

// Paste snippets here!!
struct UnionFind {
  int n;
  std::vector<int> par;
  std::vector<int> rank;
  UnionFind(int n_) : par(n_), rank(n_, 0), n(n_) {
    for (int i = 0; i < n; ++i)
      par[i] = i;
  }
  int root(int x) { return par[x] == x ? x : (par[x] = root(par[x])); }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
};

//

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false); // Magic for faster cin
  // 連結成分の数を数えるだけ

  int N, M;
  cin >> N >> M;
  vi X(M), Y(M);
  int z;

  UnionFind uf(N);
  rep(i, M) {
    cin >> X[i] >> Y[i] >> z;
    uf.unite(X[i] - 1, Y[i] - 1);
  }
  unordered_set<int> S;
  rep(i, N) { S.insert(uf.root(i)); }
  cout << S.size() << endl;

  // vector<vector<int>> G(N);
  // vector<int> c(N,-1);

  // rep(i,M){
  //   G[X[i]-1].push_back(Y[i]-1);
  //   G[Y[i]-1].push_back(X[i]-1);
  // }

  // int m=1;
  // rep(i,N){
  //   if(c[i]>=0) continue;
  //   c[i] = m;
  //   stack<int> s;
  //   s.push(i);
  //   while(!s.empty()){
  //     int t = s.top(); s.pop();
  //     for(int p : G[t]){
  //       if(c[p]>=0) continue;
  //       c[p] = m; s.push(p);
  //     }
  //   }
  //   m++;
  // }
  // cout << m-1 << endl;
  return 0;
}