Problem p03045 (C++)

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

using ull = unsigned long long;
using ll = long long;
#define repi(n) for (int i = 0; i < (n); i++)
#define repj(n) for (int j = 0; j < (n); j++)
#define repk(n) for (int k = 0; k < (n); k++)
#define repl(n) for (int l = 0; l < (n); l++)
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define repr(i, a, b) for (auto i = (a); i < (b); i++)
#define repv(itr) for (auto &&v : (itr))
#define updatemax(t, v) (t = max((t), (v)))
#define updatemin(t, v) (t = min((t), (v)))
const int dx[] = {-1, 0, 0, 1, -1, -1, 1, 1};
const int dy[] = {0, -1, 1, 0, -1, 1, -1, 1};
const double PI = atan(1.0) * 4;
template <typename T> T minptr(T begin, T end) {
  T re = begin;
  for (T i = begin + 1; i != end; i++) {
    if (*i < *re)
      re = i;
  }
  return re;
}
template <typename T> T maxptr(T begin, T end) {
  T re = begin;
  for (T i = begin + 1; i != end; i++) {
    if (*i > *re)
      re = i;
  }
  return re;
}
int __vmax(int x) { return INT_MAX; }
double __vmax(double x) { return 1e+300; }
ll __vmax(ll x) { return LLONG_MAX; }
int __vmin(int x) { return INT_MIN; }
double __vmin(double x) { return -1e+300; }
ll __vmin(ll x) { return LLONG_MIN; }
template <typename T> T gcd(T a, T b) {
  return b == 0 ? a : b == 1 ? 1 : gcd(b, a % b);
}
template <typename T> T ib_binary_search(T begin, T end, function<bool(T)> f) {
  if (!f(end))
    return end;
  while (abs(end - begin) > 1) {
    T m = (begin + end) / 2;
    if (f(m)) {
      end = m;
    } else {
      begin = m;
    }
  }
  return end;
}
ll modpow(ll a, ll b, ll m) {
  ll re = 1, k = 1;
  while (k <= b) {
    if (b & k) {
      re *= a;
      re %= m;
    }
    k = k << 1;
    a *= a;
    a %= m;
  }
  return re;
}
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
ll modbinomial(ll n, ll k, ll m) {
  k = min(k, n - k);
  if (k < 0)
    return 0;
  ll re = 1;
  for (ll i = 0; i < k; i++) {
    re *= n - i;
    re %= m;
    re *= modinv(i + 1, m);
    re %= m;
  }
  return re;
}
template <typename T>
vector<T> lis(T begin, T end, bool allowequal = false, bool lds = false) {
  using V = typename iterator_traits<T>::value_type;
  int n = end - begin;
  vector<V> a(n, lds ? __vmin(*begin) : __vmax(*begin));
  vector<int> id(n);
  if (lds && allowequal) {
    for (int i = 0; i < n; i++) {
      id[i] = n - 1 -
              (lower_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
      a[id[i]] = begin[i];
    }
  } else if (lds) {
    for (int i = 0; i < n; i++) {
      id[i] = n - 1 -
              (upper_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
      a[id[i]] = begin[i];
    }
  } else if (allowequal) {
    for (int i = 0; i < n; i++) {
      id[i] = upper_bound(a.begin(), a.end(), begin[i]) - a.begin();
      a[id[i]] = begin[i];
    }
  } else {
    for (int i = 0; i < n; i++) {
      id[i] = lower_bound(a.begin(), a.end(), begin[i]) - a.begin();
      a[id[i]] = begin[i];
    }
  }
  int m = *maxptr(id.begin(), id.end());
  vector<T> re(m + 1);
  for (int i = n - 1; i >= 0; i--) {
    if (id[i] == m)
      re[m--] = begin + i;
  }
  return re;
}
template <typename T> class segtree {
private:
  int n;
  function<T(T, T)> f;
  T e;
  vector<T> data;
  void _updateP(int i) {
    data[i] = f(data[i * 2 + 1], data[i * 2 + 2]);
    if (i)
      _updateP((i - 1) / 2);
  }
  T _calc(int begin, int end, int node, int nodeBegin, int nodeEnd) {
    if (end <= nodeBegin || nodeEnd <= begin) {
      return e;
    } else if (begin <= nodeBegin && nodeEnd <= end) {
      return data[node];
    } else {
      int m = (nodeBegin + nodeEnd) / 2;
      T left = _calc(begin, end, node * 2 + 1, nodeBegin, m);
      T right = _calc(begin, end, node * 2 + 2, m, nodeEnd);
      return f(left, right);
    }
  }

public:
  segtree(int n_, function<T(T, T)> f_, T e_, T fill) {
    n = pow(2, ceil(log2(n_)));
    f = f_;
    e = e_;
    data.resize(n * 2 - 1);
    for (int i = 0; i < n * 2 - 1; i++)
      data[i] = fill;
  }
  segtree(int n_, function<T(T, T)> f_, T e_) : segtree(n_, f_, e_, e_) {}
  T value(int i) { return data[n - 1 + i]; }
  void update(int i, T value) {
    data[n - 1 + i] = value;
    if (n)
      _updateP((n - 2 + i) / 2);
  }
  T calc(int begin, int end) { return _calc(begin, end, 0, 0, n); }
  static T max(T a, T b) { return std::max(a, b); }
  static T min(T a, T b) { return std::min(a, b); }
  static T sum(T a, T b) { return a + b; }
};

int parent[100000];
int grp(int x) {
  if (parent[x] == x)
    return x;
  return (parent[x] = grp(parent[x]));
}
void uni(int a, int b) { parent[grp(b)] = a; }
bool mm[100000];
int main() {
  int n, m;
  cin >> n >> m;
  repi(n) parent[i] = i;
  repi(m) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    uni(x, y);
  }
  int count = 0;
  repi(n) {
    int g = grp(i);
    if (!mm[g]) {
      mm[g] = true;
      count++;
    }
  }
  cout << count << endl;
}


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

using ull = unsigned long long;
using ll = long long;
#define repi(n) for (int i = 0; i < (n); i++)
#define repj(n) for (int j = 0; j < (n); j++)
#define repk(n) for (int k = 0; k < (n); k++)
#define repl(n) for (int l = 0; l < (n); l++)
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define repr(i, a, b) for (auto i = (a); i < (b); i++)
#define repv(itr) for (auto &&v : (itr))
#define updatemax(t, v) (t = max((t), (v)))
#define updatemin(t, v) (t = min((t), (v)))
const int dx[] = {-1, 0, 0, 1, -1, -1, 1, 1};
const int dy[] = {0, -1, 1, 0, -1, 1, -1, 1};
const double PI = atan(1.0) * 4;
template <typename T> T minptr(T begin, T end) {
  T re = begin;
  for (T i = begin + 1; i != end; i++) {
    if (*i < *re)
      re = i;
  }
  return re;
}
template <typename T> T maxptr(T begin, T end) {
  T re = begin;
  for (T i = begin + 1; i != end; i++) {
    if (*i > *re)
      re = i;
  }
  return re;
}
int __vmax(int x) { return INT_MAX; }
double __vmax(double x) { return 1e+300; }
ll __vmax(ll x) { return LLONG_MAX; }
int __vmin(int x) { return INT_MIN; }
double __vmin(double x) { return -1e+300; }
ll __vmin(ll x) { return LLONG_MIN; }
template <typename T> T gcd(T a, T b) {
  return b == 0 ? a : b == 1 ? 1 : gcd(b, a % b);
}
template <typename T> T ib_binary_search(T begin, T end, function<bool(T)> f) {
  if (!f(end))
    return end;
  while (abs(end - begin) > 1) {
    T m = (begin + end) / 2;
    if (f(m)) {
      end = m;
    } else {
      begin = m;
    }
  }
  return end;
}
ll modpow(ll a, ll b, ll m) {
  ll re = 1, k = 1;
  while (k <= b) {
    if (b & k) {
      re *= a;
      re %= m;
    }
    k = k << 1;
    a *= a;
    a %= m;
  }
  return re;
}
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
ll modbinomial(ll n, ll k, ll m) {
  k = min(k, n - k);
  if (k < 0)
    return 0;
  ll re = 1;
  for (ll i = 0; i < k; i++) {
    re *= n - i;
    re %= m;
    re *= modinv(i + 1, m);
    re %= m;
  }
  return re;
}
template <typename T>
vector<T> lis(T begin, T end, bool allowequal = false, bool lds = false) {
  using V = typename iterator_traits<T>::value_type;
  int n = end - begin;
  vector<V> a(n, lds ? __vmin(*begin) : __vmax(*begin));
  vector<int> id(n);
  if (lds && allowequal) {
    for (int i = 0; i < n; i++) {
      id[i] = n - 1 -
              (lower_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
      a[id[i]] = begin[i];
    }
  } else if (lds) {
    for (int i = 0; i < n; i++) {
      id[i] = n - 1 -
              (upper_bound(a.rbegin(), a.rend(), begin[i]) - 1 - a.rbegin());
      a[id[i]] = begin[i];
    }
  } else if (allowequal) {
    for (int i = 0; i < n; i++) {
      id[i] = upper_bound(a.begin(), a.end(), begin[i]) - a.begin();
      a[id[i]] = begin[i];
    }
  } else {
    for (int i = 0; i < n; i++) {
      id[i] = lower_bound(a.begin(), a.end(), begin[i]) - a.begin();
      a[id[i]] = begin[i];
    }
  }
  int m = *maxptr(id.begin(), id.end());
  vector<T> re(m + 1);
  for (int i = n - 1; i >= 0; i--) {
    if (id[i] == m)
      re[m--] = begin + i;
  }
  return re;
}
template <typename T> class segtree {
private:
  int n;
  function<T(T, T)> f;
  T e;
  vector<T> data;
  void _updateP(int i) {
    data[i] = f(data[i * 2 + 1], data[i * 2 + 2]);
    if (i)
      _updateP((i - 1) / 2);
  }
  T _calc(int begin, int end, int node, int nodeBegin, int nodeEnd) {
    if (end <= nodeBegin || nodeEnd <= begin) {
      return e;
    } else if (begin <= nodeBegin && nodeEnd <= end) {
      return data[node];
    } else {
      int m = (nodeBegin + nodeEnd) / 2;
      T left = _calc(begin, end, node * 2 + 1, nodeBegin, m);
      T right = _calc(begin, end, node * 2 + 2, m, nodeEnd);
      return f(left, right);
    }
  }

public:
  segtree(int n_, function<T(T, T)> f_, T e_, T fill) {
    n = pow(2, ceil(log2(n_)));
    f = f_;
    e = e_;
    data.resize(n * 2 - 1);
    for (int i = 0; i < n * 2 - 1; i++)
      data[i] = fill;
  }
  segtree(int n_, function<T(T, T)> f_, T e_) : segtree(n_, f_, e_, e_) {}
  T value(int i) { return data[n - 1 + i]; }
  void update(int i, T value) {
    data[n - 1 + i] = value;
    if (n)
      _updateP((n - 2 + i) / 2);
  }
  T calc(int begin, int end) { return _calc(begin, end, 0, 0, n); }
  static T max(T a, T b) { return std::max(a, b); }
  static T min(T a, T b) { return std::min(a, b); }
  static T sum(T a, T b) { return a + b; }
};

int parent[100000];
int grp(int x) {
  if (parent[x] == x)
    return x;
  return parent[x] = grp(parent[x]);
}
void uni(int a, int b) { parent[grp(a)] = grp(b); }
bool mm[100000];
int main() {
  int n, m;
  cin >> n >> m;
  repi(n) parent[i] = i;
  repi(m) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    uni(x, y);
  }
  int count = 0;
  repi(n) {
    int g = grp(i);
    if (!mm[g]) {
      mm[g] = true;
      count++;
    }
  }
  cout << count << endl;
}