prithivMLmods/Coder-Stat huggingface dataset:
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>

#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;
const int INF = pow(2, 29);
const int MOD = 1000000007;

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) { rep(i, N) par[i] = i; }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);
  rep(i, M) {
    int X, Y, Z;
    cin >> X >> Y >> Z;
    tree.unite(X, Y);
  }
  int r[N];
  rep(i, N) r[i] = tree.root(i);
  sort(r, r + N);
  int result = 1;
  rep(i, N - 1) if (r[i] != r[i + 1]) result++;
  cout << result << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>

#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;
const int INF = pow(2, 29);
const int MOD = 1000000007;

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) { rep(i, N) par[i] = i; }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  UnionFind tree(N);
  rep(i, M) {
    int X, Y, Z;
    cin >> X >> Y >> Z;
    X--;
    Y--;
    tree.unite(X, Y);
  }
  int r[N];
  rep(i, N) r[i] = tree.root(i);
  sort(r, r + N);
  int result = 1;
  rep(i, N - 1) if (r[i] != r[i + 1]) result++;
  cout << result << endl;
  return 0;
}
Problem p03045 (C++)

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

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}

const int N = 1e5 + 10;
struct Edge {
  int v, next;
} edge[N];
bool vis[N];
int n, m, cnt, head[N];
inline void add(int u, int v) {
  edge[++cnt].v = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
  edge[++cnt].v = u;
  edge[cnt].next = head[v];
  head[v] = cnt++;
}
void dfs(int u) {
  vis[u] = 1;
  for (int i = head[u]; i; i = edge[i].next) {
    int v = edge[i].v;
    if (!vis[v])
      dfs(v);
  }
}

int main() {
  n = read(), m = read();
  for (int i = 0; i < m; i++) {
    int a = read(), b = read();
    int c = read();
    add(a, b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ans++;
      dfs(i);
    }
  }
  printf("%d\n", ans);
  return 0;
}

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

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}

const int N = 1e5 + 10;
struct Edge {
  int v, next;
} edge[N * 4];
bool vis[N];
int n, m, cnt, head[N];
inline void add(int u, int v) {
  edge[++cnt].v = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
  edge[++cnt].v = u;
  edge[cnt].next = head[v];
  head[v] = cnt++;
}
void dfs(int u) {
  vis[u] = 1;
  for (int i = head[u]; i; i = edge[i].next) {
    int v = edge[i].v;
    if (!vis[v])
      dfs(v);
  }
}

int main() {
  n = read(), m = read();
  for (int i = 0; i < m; i++) {
    int a = read(), b = read();
    int c = read();
    add(a, b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      ans++;
      dfs(i);
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

class DisjointSet {
public:
  vector<int> rank, p;

  DisjointSet() {}
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++)
      makeSet(i);
  }

  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  } // 自身がparentのグループを作る

  bool same(int x, int y) {
    return findSet(x) == findSet(y); // x,yが同じグループに属しているか判定
  }

  void unite(int x, int y) {
    link(findSet(x), findSet(y));
  } // xとyの属するsetを結合する

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  } // rankが大きい方の下にrankが小さいものをくっつける

  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  } // 再帰的にxの親を見つけている
};

int main() {
  int n, m, ans = 0;
  bool flag = 0;
  cin >> n >> m;
  ans = n;
  int x, y, z;
  DisjointSet ds = DisjointSet(n);
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    if (ds.same(x, y) == false) {
      ds.unite(x, y);
      ans--;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

class DisjointSet {
public:
  vector<int> rank, p;

  DisjointSet() {}
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++)
      makeSet(i);
  }

  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  } // 自身がparentのグループを作る

  bool same(int x, int y) {
    return findSet(x) == findSet(y); // x,yが同じグループに属しているか判定
  }

  void unite(int x, int y) {
    link(findSet(x), findSet(y));
  } // xとyの属するsetを結合する

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  } // rankが大きい方の下にrankが小さいものをくっつける

  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  } // 再帰的にxの親を見つけている
};

int main() {
  int n, m, ans = 0;
  bool flag = 0;
  cin >> n >> m;
  ans = n;
  int x, y, z;
  DisjointSet ds = DisjointSet(n);
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    if (ds.same(x - 1, y - 1) == false) {
      ds.unite(x - 1, y - 1);
      ans--;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())

#define ll long long
#define ALL(a) (a).begin(), (a).end()

#define pb(a) push_back(a)

int n, m;
vector<int> ki(100000);
vector<int> kz(100000, 1);

int root(int w) {
  int ww = w;
  vector<int> kidmy(100000);
  int lfg = 0;
  REP(ii, 10) {
    if (ki[ww] == ww) {
      break;
    } else {
      kidmy[lfg] = ww;
      lfg++;
      ww = ki[ww];
    }
  }

  REP(ii, lfg) { ki[kidmy[ii]] = ww; }

  // if (kz[ww]<lfg)kz[ww]=lfg;

  return ww;
}

int main() {
  int ans = 0;
  cin >> n >> m;

  vector<int> x(m), y(m), z(m);
  REP(i, m) cin >> x[i] >> y[i] >> z[i];
  REP(i, 100000) ki[i] = i;
  REP(i, m) { // 結合させていく
    int pidx = root(x[i] - 1);
    int pidy = root(y[i] - 1);
    if (pidx != pidy) {
      if (kz[pidy] > kz[pidx]) {
        ki[pidx] = pidy;
        kz[pidy] += kz[pidx];
      } else {
        ki[pidy] = pidx;
        kz[pidx] += kz[pidy];
      }
    }
  }

  REP(i, n) {
    if (ki[i] == i)
      ans++;
  }

  cout << ans << endl;
  return 0;
}

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

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REP1(i, n) for (ll i = 1; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())

#define ll long long
#define ALL(a) (a).begin(), (a).end()

#define pb(a) push_back(a)

int n, m;
vector<int> ki(100000);
vector<int> kz(100000, 1);

int root(int w) {
  int ww = w;
  vector<int> kidmy(10);
  int lfg = 0;
  REP(ii, 10) {
    if (ki[ww] == ww) {
      break;
    } else {
      kidmy[lfg] = ww;
      lfg++;
      ww = ki[ww];
    }
  }

  REP(ii, lfg) { ki[kidmy[ii]] = ww; }

  // if (kz[ww]<lfg)kz[ww]=lfg;

  return ww;
}

int main() {
  int ans = 0;
  cin >> n >> m;

  vector<int> x(m), y(m), z(m);
  REP(i, m) cin >> x[i] >> y[i] >> z[i];
  REP(i, 100000) ki[i] = i;
  REP(i, m) { // 結合させていく
    int pidx = root(x[i] - 1);
    int pidy = root(y[i] - 1);
    if (pidx != pidy) {
      if (kz[pidy] > kz[pidx]) {
        ki[pidx] = pidy;
        kz[pidy] += kz[pidx];
      } else {
        ki[pidy] = pidx;
        kz[pidx] += kz[pidy];
      }
    }
  }

  REP(i, n) {
    if (ki[i] == i)
      ans++;
  }

  cout << ans << endl;
  return 0;
}
Problem p03045 (C++)

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

using namespace std;

struct DSU {
  int n;
  vector<int> ps;
  vector<int> rank;
  int num_components;
  DSU(int n) : n(n), ps(n), num_components(n) {
    for (int i = 0; i < n; ++i) {
      ps[i] = i;
      rank[i] = 0;
    }
  }
  int parent(int i) {
    if (ps[i] == i) {
      return i;
    }
    return ps[i] = parent(ps[i]);
  }
  int merge(int i, int j) {
    i = parent(i);
    j = parent(j);
    if (i != j) {
      if (rank[i] <= rank[j]) {
        swap(i, j);
      }
      if (rank[i] == rank[j]) {
        ++rank[i];
      }
      ps[j] = i;
      --num_components;
      return true;
    }
    return false;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> ps(n);
  for (int i = 0; i < n; ++i) {
    ps[i] = i;
  }
  DSU dsu(n);
  for (int i = 0; i < m; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    --x;
    --y;
    dsu.merge(x, y);
  }
  cout << dsu.num_components << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

struct DSU {
  int n;
  vector<int> ps;
  vector<int> rank;
  int num_components;
  DSU(int n) : n(n), ps(n), num_components(n), rank(n) {
    for (int i = 0; i < n; ++i) {
      ps[i] = i;
      rank[i] = 0;
    }
  }
  int parent(int i) {
    if (ps[i] == i) {
      return i;
    }
    return ps[i] = parent(ps[i]);
  }
  int merge(int i, int j) {
    i = parent(i);
    j = parent(j);
    if (i != j) {
      if (rank[i] <= rank[j]) {
        swap(i, j);
      }
      if (rank[i] == rank[j]) {
        ++rank[i];
      }
      ps[j] = i;
      --num_components;
      return true;
    }
    return false;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> ps(n);
  for (int i = 0; i < n; ++i) {
    ps[i] = i;
  }
  DSU dsu(n);
  for (int i = 0; i < m; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    --x;
    --y;
    dsu.merge(x, y);
  }
  cout << dsu.num_components << endl;
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

using namespace std;

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[ry] = rx;
  };

  bool same(int x, int y) {
    if (root(x) == root(y))
      return true;
    return false;
  }
};

void Main() {
  int N, M;
  std::cin >> N >> M;
  UnionFind uf(N);

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

  std::map<int, int> m;
  int res = 0;

  for (int n = 0; n < N; n++) {
    if (!m[uf.root(n)]) {
      m[uf.root(n)] = 1;
      res++;
    }
  }

  cout << res << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

struct UnionFind {
  vector<int> par;

  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[ry] = rx;
  };

  bool same(int x, int y) {
    if (root(x) == root(y))
      return true;
    return false;
  }
};

void Main() {
  int N, M;
  std::cin >> N >> M;
  UnionFind uf(N);

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

  std::map<int, int> m;
  int res = 0;

  for (int n = 0; n < N; n++) {
    if (!m[uf.root(n)]) {
      m[uf.root(n)] = 1;
      res++;
    }
  }

  cout << res << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

int n, m, ans;
vector<int> x, y, z, p;

int find(int x) {
  while (x != p[x])
    x = p[x];
  return x;
}
void merge(int x, int y) {
  x--, y--;
  int rootx = find(x);
  int rooty = find(y);
  if (rooty != rootx) {
    p[rootx] = rooty;
    ans--;
  }
}
int solve() {
  rep(i, m) merge(x[i], y[i]);
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  scanf("%d%d", &n, &m);
  rep(i, m) {
    int xi, yi, zi;
    scanf("%d%d%d", &xi, &yi, &zi);
    x.push_back(xi);
    y.push_back(yi);
    z.push_back(zi);
  }
  p.resize(n);
  rep(i, n) { p[i] = i; }
  ans = n;
  cout << solve() << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

int n, m, ans;
vector<int> x, y, z, p;

int find(int x) {
  if (p[x] == x)
    return x;
  return p[x] = find(p[x]);
}
void merge(int x, int y) {
  x--, y--;
  int rootx = find(x);
  int rooty = find(y);
  if (rooty != rootx) {
    p[rootx] = rooty;
    ans--;
  }
}
int solve() {
  rep(i, m) merge(x[i], y[i]);
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  scanf("%d%d", &n, &m);
  rep(i, m) {
    int xi, yi, zi;
    scanf("%d%d%d", &xi, &yi, &zi);
    x.push_back(xi);
    y.push_back(yi);
    z.push_back(zi);
  }
  p.resize(n);
  rep(i, n) { p[i] = i; }
  ans = n;
  cout << solve() << endl;
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

#define all(x) x.begin(), x.end()

const ll mod = 1e9 + 7;
const ll INF = 1e9;
const ll MAXN = 1e9;

struct union_find {
  vector<int> par, r;

  union_find(int n) : par(n), r(n) { init(n); }

  void init(int n) {
    for (int i = 0; i < n; i++)
      par[i] = i;
    for (int i = 0; i < n; i++)
      r[i] = 0;
  }

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

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

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n), y(n);
  for (int i = 0; i < m; i++) {
    int z;
    cin >> x[i] >> y[i] >> z;
  }
  union_find uf(n);
  for (int i = 0; i < m; i++) {
    x[i]--;
    y[i]--;
    if (!uf.same(x[i], y[i]))
      uf.unite(x[i], y[i]);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (uf.par[i] == i)
      ans++;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

#define all(x) x.begin(), x.end()

const ll mod = 1e9 + 7;
const ll INF = 1e9;
const ll MAXN = 1e9;

struct union_find {
  vector<int> par, r;

  union_find(int n) : par(n), r(n) { init(n); }

  void init(int n) {
    for (int i = 0; i < n; i++)
      par[i] = i;
    for (int i = 0; i < n; i++)
      r[i] = 0;
  }

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

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

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m), y(m);
  for (int i = 0; i < m; i++) {
    int z;
    cin >> x[i] >> y[i] >> z;
  }
  union_find uf(n);
  for (int i = 0; i < m; i++) {
    x[i]--;
    y[i]--;
    if (!uf.same(x[i], y[i]))
      uf.unite(x[i], y[i]);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (uf.par[i] == i)
      ans++;
  }
  cout << ans << endl;

  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>

using namespace std;
const int a = 1e5 + 1000;
int f[a];

int findf(int q) {
  if (f[q] < 0) {
    return q;
  } else {
    return findf(f[q]);
  }
}
int main() {
  int N;
  int M;
  cin >> N >> M;
  for (int i = 0; i < a; i++) {
    f[i] = -1;
  }
  for (int i = 0; i < M; i++) {
    int X;
    int Y;
    int Z;
    cin >> X >> Y >> Z;
    int fx;
    int fy;
    fx = findf(X);
    fy = findf(Y);
    if (fx != fy) {
      f[fy] = fx;
    }
  }
  int b = 0;
  for (int i = 1; i <= N; i++) {
    if (f[i] < 0) {
      b++;
    }
  }
  cout << b;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;
const int a = 1e5 + 1000;
int f[a];

int findf(int q) {
  if (f[q] < 0) {
    return q;
  } else {
    f[q] = findf(f[q]);
    return f[q];
  }
}
int main() {
  int N;
  int M;
  cin >> N >> M;
  for (int i = 0; i < a; i++) {
    f[i] = -1;
  }
  for (int i = 0; i < M; i++) {
    int X;
    int Y;
    int Z;
    cin >> X >> Y >> Z;
    int fx;
    int fy;
    fx = findf(X);
    fy = findf(Y);
    if (fx != fy) {
      f[fy] = fx;
    }
  }
  int b = 0;
  for (int i = 1; i <= N; i++) {
    if (f[i] < 0) {
      b++;
    }
  }
  cout << b;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxN = 2e5 + 7;
const int maxV = 90010;
// const int INF = 1e9+9;
const ll INF = (1ll) << 60;
const int MOD = 1e9 + 7;
int fa[maxN];
int get(int x) {
  if (x == fa[x])
    return fa[x];
  else
    fa[x] = get(fa[x]);
  return fa[x];
}
int add(int x, int y) {
  int u = fa[x];
  int v = fa[y];
  if (u != v)
    fa[u] = v;
}
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    add(x, y);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (get(i) == i)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxN = 2e5 + 7;
const int maxV = 90010;
// const int INF = 1e9+9;
const ll INF = (1ll) << 60;
const int MOD = 1e9 + 7;
int fa[maxN];
int get(int x) {
  if (x == fa[x])
    return fa[x];
  else
    fa[x] = get(fa[x]);
  return fa[x];
}
int add(int x, int y) {
  int u = get(x);
  int v = get(y);
  if (u != v)
    fa[u] = v;
}
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    add(x, y);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (get(i) == i)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
const int maxn = 1e5 + 7;
int pre[maxn];
bool you[maxn];
int Find(int x) {
  int p, tmp;
  p = x;
  while (x != pre[x])
    x = pre[x];
  while (p != x) {
    tmp = pre[x];
    pre[x] = x;
    p = tmp;
  }
  return x;
}
void join(int x, int y) {
  int fx = Find(x);
  int fy = Find(y);
  if (fx != fy)
    pre[fx] = fy;
}
int main() {
  int n, m, sum = 0, jian = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    pre[i] = i;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (a == b && you[a] == false) {
      jian++;
      you[a] = true;
    }
    join(a, b);
  }
  for (int i = 1; i <= n; i++)
    if (pre[i] == i)
      sum++;
  printf("%d\n", sum - jian);
  printf("\n");
}

Fixed submission:
#include <stdio.h>
const int maxn = 1e5 + 7;
int pre[maxn];
bool you[maxn];
int Find(int x) {
  if (pre[x] == x)
    return x;
  else
    return pre[x] = Find(pre[x]);
}
void join(int x, int y) {
  int fx = Find(x);
  int fy = Find(y);
  if (fx != fy)
    pre[fx] = fy;
}
int main() {
  int n, m, sum = 0, jian = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    pre[i] = i;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (a == b && you[a] == false) {
      jian++;
      you[a] = true;
    }
    join(a, b);
  }
  for (int i = 1; i <= n; i++)
    if (pre[i] == i)
      sum++;
  printf("%d\n", sum - jian);
  printf("\n");
}
Problem p03045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int fa[100007];
int find_(int x) {
  if (fa[x] != x)
    return find_(fa[x]);
  return fa[x];
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    int xx = find_(x);
    int yy = find_(y);
    if (xx != yy)
      fa[xx] = yy;
  }
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (fa[i] == i)
      cnt++;
  }
  printf("%d", cnt);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int fa[100007];
int find_(int x) {
  if (fa[x] != x)
    return fa[x] = find_(fa[x]);
  return fa[x];
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    int xx = find_(x);
    int yy = find_(y);
    if (xx != yy)
      fa[xx] = yy;
  }
  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (fa[i] == i)
      cnt++;
  }
  printf("%d", cnt);
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
const int siz = 1e5 + 7;
vector<int> par;
int getparent(int x) {
  if (par[x] == x)
    return x;
  return par[x] = getparent(par[x]);
}
void dsu(int a, int b) {
  a = getparent(a);
  b = getparent(b);
  if (a != b) {
    par[b] = a;
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  par.resize(n + 1);
  for (int i = 0; i <= n; i++) {
    par[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int u, v, z;
    cin >> u >> v >> z;
    dsu(u, v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (par[i] == i)
      ans++;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
const int siz = 1e5 + 7;
vector<int> par;
int getparent(int x) {
  if (par[x] == x)
    return x;
  return par[x] = getparent(par[x]);
}
void dsu(int a, int b) {
  a = getparent(a);
  b = getparent(b);
  if (a != b) {
    par[b] = a;
  }
}
int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  par.resize(n + 1);
  for (int i = 0; i <= n; i++) {
    par[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int u, v, z;
    cin >> u >> v >> z;
    dsu(u, v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (par[i] == i)
      ans++;
  }
  cout << ans << endl;
}
Problem p03045 (Python)

Buggy submission (status: Runtime Error):
# union-findで行けそう。

n, m = map(int, input().split())
par = [-1] * (n + 1)


def find(a):
    if par[a] < 0:
        return a
    else:
        par[a] = find(par[a])
        return par[a]


def unite(a, b):
    a = find(a)
    b = find(b)
    if a == b:
        return False
    else:
        if par[a] < par[b]:
            a, b = b, a
        par[x] += par[y]
        par[y] = x
        return True


for i in range(m):
    x, y, z = map(int, input().split())
    unite(x, y)
cnt = 0
for i in range(n):
    if par[i + 1] < 0:
        cnt += 1
print(cnt)


Fixed submission:
# union-findで行けそう。

n, m = map(int, input().split())
par = [-1] * (n + 1)


def find(a):
    if par[a] < 0:
        return a
    else:
        par[a] = find(par[a])
        return par[a]


def unite(a, b):
    a = find(a)
    b = find(b)
    if a == b:
        return False
    else:
        if par[a] < par[b]:
            a, b = b, a
        par[a] += par[b]
        par[b] = a
        return True


for i in range(m):
    x, y, z = map(int, input().split())
    unite(x, y)
cnt = 0
for i in range(n):
    if par[i + 1] < 0:
        cnt += 1
print(cnt)
Problem p03045 (Python)

Buggy submission (status: Runtime Error):
N, M = [int(_) for _ in input().split()]
XYZ = [[int(_) for _ in input().split()] for _ in range(M)]

UF = list(range(N + 1))


def find(x):
    if UF[x] != x:
        UF[x] = find(UF[x])
    return UF[x]


def unite(x, y):
    UF[find(x)] = find(y)


def same(x, y):
    return find(x) == find(y)


for x, y, z in XYZ:
    unite(x, y)
s = set()
for i in range(1, N + 1):
    s.add(find(i))
print(len(set(s)))


Fixed submission:
import sys

sys.setrecursionlimit(100000)
N, M = [int(_) for _ in input().split()]
XYZ = [[int(_) for _ in input().split()] for _ in range(M)]

UF = list(range(N + 1))


def find(x):
    if UF[x] != x:
        UF[x] = find(UF[x])
    return UF[x]


def unite(x, y):
    UF[find(x)] = find(y)


def same(x, y):
    return find(x) == find(y)


for x, y, z in XYZ:
    unite(x, y)
s = set()
for i in range(1, N + 1):
    s.add(find(i))
print(len(set(s)))
Problem p03045 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())

p = list(range(n))


def find(x):
    if p[x] != x:
        p[x] = find(p[x])
    return p[x]


def union(x, y):
    x, y = find(x), find(y)
    p[y] = x


for _ in range(m):
    x, y, z = [int(i) - 1 for i in input().split()]
    if find(x) != find(y):
        union(x, y)

ans = set()
for i in range(n):
    find(i)
print(len(set(p)))


Fixed submission:
n, m = map(int, input().split())

p = list(range(n))


def find(x):
    if p[x] != x:
        p[x] = find(p[x])
    return p[x]


def union(x, y):
    x, y = find(x), find(y)
    p[x] = p[y] = min(x, y)


for _ in range(m):
    x, y, z = [int(i) - 1 for i in input().split()]
    if find(x) != find(y):
        union(x, y)

ans = set()
for i in range(n):
    find(i)
print(len(set(p)))
Problem p03045 (Python)

Buggy submission (status: Runtime Error):
import sys

# 再帰上限の変更(デフォルトは1000)
sys.setrecursionlimit(10**20)

N, M = map(int, input().split())
XYZ = [list(map(int, input().split())) for i in range(M)]

# XがわかればYがわかるので、あるAiがわかったときどこまでわかるか?をグラフにしてDFSで探索

graph = [[] for i in range(N + 1)]

seen = [0] * (N + 1)

for X, Y, Z in XYZ:
    graph[X].append(Y)
    graph[Y].append(X)


def dfs(a):
    if seen[a] == 1:
        return
    else:
        seen[a] = 1
        for i in graph[a]:
            dfs(i)


ans = 0

for i in range(1, N + 1):
    if seen[i] == 1:
        continue
    else:
        dfs(i)
        ans += 1

print(ans)


Fixed submission:
import sys

# 再帰上限の変更(デフォルトは1000)
# 大きくしすぎるとREするらしい
sys.setrecursionlimit(10**9)

N, M = map(int, input().split())
XYZ = [list(map(int, input().split())) for i in range(M)]

# XがわかればYがわかるので、あるAiがわかったときどこまでわかるか?をグラフにしてDFSで探索

graph = [[] for i in range(N + 1)]

seen = [0] * (N + 1)

for X, Y, Z in XYZ:
    graph[X].append(Y)
    graph[Y].append(X)


def dfs(a):
    if seen[a] == 1:
        return
    else:
        seen[a] = 1
        for i in graph[a]:
            dfs(i)


ans = 0

for i in range(1, N + 1):
    if seen[i] == 1:
        continue
    else:
        dfs(i)
        ans += 1

print(ans)
Problem p03045 (C++)

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

int N, M;
vector<int> path[100001];
int pathed[100000] = {0};

void dfs(int now) {
  // cout << now << endl;
  pathed[now] = 1;
  for (int i(0); i < path[now].size(); i++) {
    int next = path[now][i];
    if (pathed[next] == 0)
      dfs(next);
  }
}

int main() {
  cin >> N >> M;
  for (int i(0); i < M; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    path[x].push_back(y);
    path[y].push_back(x);
  }
  ll ans(0);
  for (int i(1); i <= N; i++) {
    if (pathed[i] == 0) {
      dfs(i);
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 998244353;

int N, M;
vector<int> path[100001];
int pathed[100001] = {0};

void dfs(int now) {
  // cout << now << endl;
  pathed[now] = 1;
  for (int i(0); i < path[now].size(); i++) {
    int next = path[now][i];
    if (pathed[next] == 0)
      dfs(next);
  }
}

int main() {
  cin >> N >> M;
  for (int i(0); i < M; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    path[x].push_back(y);
    path[y].push_back(x);
  }
  ll ans(0);
  for (int i(1); i <= N; i++) {
    if (pathed[i] == 0) {
      dfs(i);
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
struct UnionFind {
  vector<ll> par, rank;
  UnionFind(int size) : par(size), rank(size, 1) {
    rep(i, size) { par[i] = i; }
  }
  ll find(ll x) // 根を返す
  {
    if (par[x] == x)
      return x;
    else {
      par[x] = find(par[x]);
      return par[x];
    }
  }
  bool unite(ll x, ll y) // もともと同じならfalseを、そうでなければtrueを返す
  {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y]) {
      swap(x, y);
    }
    par[y] = x; // 大きいほうに小さいほうを繋げる
    if (rank[x] == rank[y])
      rank[x]++;
    return true;
  }
  bool same(ll x, ll y) { return find(x) == find(y); }
};
int main() {
  int N, M;
  cin >> N >> M;
  vint X(N), Y(N), Z(N);
  UnionFind uf(N);
  rep(i, M) {
    cin >> X[i] >> Y[i] >> Z[i];
    uf.unite(X[i] - 1, Y[i] - 1);
  }
  vector<bool> Num(N);
  int ans = 0;
  rep(i, N) {
    if (!Num[(uf.find(i))]) {
      Num[(uf.find(i))] = true;
      ans++;
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
struct UnionFind {
  vector<ll> par, rank;
  UnionFind(int size) : par(size), rank(size, 1) {
    rep(i, size) { par[i] = i; }
  }
  ll find(ll x) // 根を返す
  {
    if (par[x] == x)
      return x;
    else {
      par[x] = find(par[x]);
      return par[x];
    }
  }
  bool unite(ll x, ll y) // もともと同じならfalseを、そうでなければtrueを返す
  {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y]) {
      swap(x, y);
    }
    par[y] = x; // 大きいほうに小さいほうを繋げる
    if (rank[x] == rank[y])
      rank[x]++;
    return true;
  }
  bool same(ll x, ll y) { return find(x) == find(y); }
};
int main() {
  int N, M;
  cin >> N >> M;
  vint X(M), Y(M), Z(M);
  UnionFind uf(N);
  rep(i, M) {
    cin >> X[i] >> Y[i] >> Z[i];
    uf.unite(X[i] - 1, Y[i] - 1);
  }
  vector<bool> Num(N);
  int ans = 0;
  rep(i, N) {
    if (!Num[(uf.find(i))]) {
      Num[(uf.find(i))] = true;
      ans++;
    }
  }
  cout << ans;
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Senky_Bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
ll an[100005];
ll id[100005];
ll vis[100005];
ll final[100005];
ll f1[100005];
ll root(int a) {
  while (id[a] != a) {
    if (f1[a] != 0) {
      return f1[a];
    }
    a = id[a];
  }
  return a;
}
void union1(int a, int b) {
  ll a1 = root(a);
  ll b1 = root(b);
  id[a1] = id[b1];
}
signed main() {
  Senky_Bansal IIIT_ALLAHABAD

      for (int i = 1; i < 100005; i++) id[i] = i;
  ll n, m;
  cin >> n >> m;
  map<ll, ll> mp, mp1;
  ll ans = 0;
  ll a[m], b[m], c[m];
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    mp[a[i]]++;
    mp[b[i]]++;
    union1(a[i], b[i]);
  }
  for (int i = 0; i < m; i++) {
    mp1[root(a[i])]++;
    mp1[root(b[i])]++;
  }
  cout << mp1.size() + n - mp.size() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Senky_Bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
ll an[100005];
ll id[100005];
ll vis[100005];
ll final[100005];
ll f1[100005];
ll root(int a) {
  if (a == id[a])
    return a;
  return id[a] = root(id[a]);
}
void union1(int a, int b) {
  ll a1 = root(a);
  ll b1 = root(b);
  id[a1] = id[b1];
}
signed main() {
  Senky_Bansal IIIT_ALLAHABAD

      for (int i = 1; i < 100005; i++) id[i] = i;
  ll n, m;
  cin >> n >> m;
  map<ll, ll> mp, mp1;
  ll ans = 0;
  ll a[m], b[m], c[m];
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    mp[a[i]]++;
    mp[b[i]]++;
    union1(a[i], b[i]);
  }
  for (int i = 0; i < m; i++) {
    mp1[root(a[i])]++;
    mp1[root(b[i])]++;
  }
  cout << mp1.size() + n - mp.size() << endl;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }

  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }

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

  int size(int x) { return -d[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int x, y, z;
    cin >> x >> y >> z;
    uf.unite(x, y);
  }
  vector<int> A(n, 0);
  int ans = 0;
  rep(i, n) {
    int x = uf.find(i);
    if (A[x] == 0)
      ans++;
    A[x]++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }

  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }

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

  int size(int x) { return -d[find(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    uf.unite(x, y);
  }
  vector<int> A(n, 0);
  int ans = 0;
  rep(i, n) {
    int x = uf.find(i);
    if (A[x] == 0)
      ans++;
    A[x]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)

using Node = pair<int, bool>; // 親ノード番号(1始まり,0は親なし), 探索済みフラグ
using NodeList = vector<Node>;

int rootIndex(int index, NodeList &nodes) { // 根のノード番号を探す
  if (nodes[index].first == 0)              // 親がない場合はこれが根
    return index;
  else
    return rootIndex(nodes[index].first, nodes); // 親がある場合はさらに探索
}

bool isNewTree(int index, NodeList &nodes) {
  if (nodes[index].second) { // このノード自身が探索済みの場合
    return false;
  } else {                      // 未探索ノードの場合
    nodes[index].second = true; // 探索済みにする
    auto parent = nodes[index].first;
    if (parent == 0) { // 根の場合は新しい木
      return true;
    } else {
      return isNewTree(parent, nodes); // 根でなければさらに親を探索
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  NodeList nodes(n + 1, make_pair(0, false));
  rep(i, m) {
    int x, y, z;
    cin >> x >> y >> z;
    auto rx = rootIndex(x, nodes);
    auto ry = rootIndex(y, nodes);
    if (rx != ry) {        // 根ノードが異なる場合
      nodes[rx].first = y; // xの木の根を y の子に挿して tree を unite
    }
  }
  int ans = 0;
  reps(i, n) {
    if (isNewTree(i, nodes))
      ans++; // 新しい木を発見するたびにカウント
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)

using Node = pair<int, bool>; // 親ノード番号(1始まり,0は親なし), 探索済みフラグ
using NodeList = vector<Node>;

int rootIndex(int index, NodeList &nodes) { // 根のノード番号を探す
  if (nodes[index].first == 0)              // 親がない場合はこれが根
    return index;
  else {
    auto root =
        rootIndex(nodes[index].first, nodes); // 親がある場合はさらに探索
    nodes[index].first = root;                // 親をつなぎ替え
    return root;
  }
}

bool isNewTree(int index, NodeList &nodes) {
  if (nodes[index].second) { // このノード自身が探索済みの場合
    return false;
  } else {                      // 未探索ノードの場合
    nodes[index].second = true; // 探索済みにする
    auto parent = nodes[index].first;
    if (parent == 0) { // 根の場合は新しい木
      return true;
    } else {
      return isNewTree(parent, nodes); // 根でなければさらに親を探索
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  NodeList nodes(n + 1, make_pair(0, false));
  rep(i, m) {
    int x, y, z;
    cin >> x >> y >> z;
    auto rx = rootIndex(x, nodes);
    auto ry = rootIndex(y, nodes);
    if (rx != ry) {        // 根ノードが異なる場合
      nodes[rx].first = y; // xの木の根を y の子に挿して tree を unite
    }
  }
  int ans = 0;
  reps(i, n) {
    if (isNewTree(i, nodes))
      ans++; // 新しい木を発見するたびにカウント
  }
  cout << ans << endl;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000;
#define all(v) v.begin(), v.end()

vector<ll> par(100000);
vector<ll> ran(100000);
void init(ll n) {
  rep(i, n) {
    par.at(i) = i;
    ran.at(i) = 0;
  }
}
ll find(ll x) {
  if (par.at(x) == x) {
    return x;
  } else {
    return par.at(x) = find(par.at(x));
  }
}
void unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (ran.at(x) < ran.at(y)) {
    par.at(x) = y;
  } else {
    par.at(y) = x;
    if (ran.at(x) == ran.at(y))
      ran.at(x)++;
  }
}

int main() {
  ll N, M, ans = 0;
  cin >> N >> M;
  init(N);
  rep(i, M) {
    ll x, y, z;
    cin >> x >> y >> z;
    unite(x, y);
  }
  rep(i, N) {
    if (i == par.at(i))
      ans++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000;
#define all(v) v.begin(), v.end()

vector<ll> par(100000);
vector<ll> ran(100000);
void init(ll n) {
  rep(i, n) {
    par.at(i) = i;
    ran.at(i) = 0;
  }
}
ll find(ll x) {
  if (par.at(x) == x) {
    return x;
  } else {
    return par.at(x) = find(par.at(x));
  }
}
void unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (ran.at(x) < ran.at(y)) {
    par.at(x) = y;
  } else {
    par.at(y) = x;
    if (ran.at(x) == ran.at(y))
      ran.at(x)++;
  }
}

int main() {
  ll N, M, ans = 0;
  cin >> N >> M;
  init(N);
  rep(i, M) {
    ll x, y, z;
    cin >> x >> y >> z;
    unite(x - 1, y - 1);
  }
  rep(i, N) {
    if (i == par.at(i))
      ans++;
  }
  cout << ans << endl;
}
Problem p03045 (C++)

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

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int main() {
  int N, M;
  cin >> N >> M;

  UnionFind uf = UnionFind(N);
  int clusterNumber = N;

  for (int i = 0; i < M; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (uf.unite(x, y)) {
      clusterNumber--;
    }
  }
  cout << clusterNumber << endl;
  return 0;
}

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

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int main() {
  int N, M;
  cin >> N >> M;

  UnionFind uf = UnionFind(N);
  int clusterNumber = N;

  for (int i = 0; i < M; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    z--;
    if (uf.unite(x, y)) {
      clusterNumber--;
    }
  }
  cout << clusterNumber << endl;
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

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;
    uf.unite(x, y);
  }
  map<int, bool> appeared;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!appeared[uf.find(i)]) {
      appeared[uf.find(i)] = true;
      ans++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <map>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

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;
    uf.unite(x - 1, y - 1);
  }
  map<int, bool> appeared;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!appeared[uf.find(i)]) {
      appeared[uf.find(i)] = true;
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p03045 (C++)

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

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool same(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m), y(m), z(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i] >> z[i];
    x[i]--;
    y[i]--;
  }

  UnionFind uf(n);
  for (int i = 0; i < m; i++)
    uf.merge(x[i], y[i]);

  set<int> st;
  for (int i = 0; i < m; i++)
    st.insert(uf.root(i));
  cout << st.size() << endl;

  return 0;
}

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

struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool same(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m), y(m), z(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i] >> z[i];
    x[i]--;
    y[i]--;
  }

  UnionFind uf(n);
  for (int i = 0; i < m; i++)
    uf.merge(x[i], y[i]);

  set<int> st;
  for (int i = 0; i < n; i++)
    st.insert(uf.root(i));
  cout << st.size() << endl;

  return 0;
}