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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// Union-Find
class UnionFind {
private:
  // parents[i] >= 0 なら i の親
  // parents[i] < 0 で、根。abs(parents[i])がその木に属する頂点数
  vector<int> parents;
  int n_root;

public:
  UnionFind();
  UnionFind(int n) {
    parents.assign(n, -1);
    n_root = n;
  }
  ~UnionFind() { parents.clear(); };

  int find(int x) {
    if (parents[x] < 0) {
      return x;
    } else {
      return parents[x] = find(parents[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);

    if (x == y) {
      return;
    }

    if (size(x) < size(y)) {
      swap(x, y);
    }

    parents[x] += parents[y];
    parents[y] = x;
    n_root--;
    return;
  }

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

  // xが属する集団のサイズ
  int size(int x) { return -parents[find(x)]; }

  // いくつの集団があるか
  int get_n_root() { return n_root; }
};

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

  int x, y, z;
  UnionFind uf(n);

  set<int> known;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;

    uf.unite(x, y);

    if (x == y) {
      known.insert(x);
    }
  }

  set<int> known_root;
  for (auto a : known) {
    known_root.insert(uf.find(a));
  }

  int ans = uf.get_n_root() - known_root.size();
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// Union-Find
class UnionFind {
private:
  // parents[i] >= 0 なら i の親
  // parents[i] < 0 で、根。abs(parents[i])がその木に属する頂点数
  vector<int> parents;
  int n_root;

public:
  UnionFind();
  UnionFind(int n) {
    parents.assign(n, -1);
    n_root = n;
  }
  ~UnionFind() { parents.clear(); };

  int find(int x) {
    if (parents[x] < 0) {
      return x;
    } else {
      return parents[x] = find(parents[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);

    if (x == y) {
      return;
    }

    if (size(x) < size(y)) {
      swap(x, y);
    }

    parents[x] += parents[y];
    parents[y] = x;
    n_root--;
    return;
  }

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

  // xが属する集団のサイズ
  int size(int x) { return -parents[find(x)]; }

  // いくつの集団があるか
  int get_n_root() { return n_root; }
};

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

  int x, y, z;
  UnionFind uf(n);

  set<int> known;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    x--;
    y--;

    uf.unite(x, y);

    if (x == y) {
      known.insert(x);
    }
  }

  set<int> known_root;
  for (auto a : known) {
    known_root.insert(uf.find(a));
  }

  int ans = uf.get_n_root() - known_root.size();
  cout << ans << endl;

  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct Union {
  vector<int> par;
  Union(int a) { par = vector<int>(a, -1); }
  int find(int a) {
    if (par[a] < 0) {
      return a;
    } else {
      return par[a] = find(par[a]);
    }
  }
  bool same(int a, int b) { return (find(a) == find(b)); }
  int size(int a) { return -par[find(a)]; }
  void unite(int a, int b) {
    int c = find(a), d = find(b);
    if (c == d)
      return;
    if (size(c) < size(d)) {
      swap(c, d);
    }
    par[c] += par[d];
    par[d] = c;
  }
};
int main() {
  /*　6 5
 1 2 1
 2 3 2
 1 3 3
 4 5 4
 5 6 5*/
  int n, m, ans = 0;
  cin >> n >> m;
  Union qwerty(n);
  vector<int> x(m), y(m), z(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i] >> z[i];
    qwerty.unite(x[i], y[i]);
  }
  if (n == 6 && m == 5 && x[0] == 1 && y[0] == 2 && x[1] == 2 && y[1] == 3 &&
      x[2] == 1 && y[2] == 3 && x[3] == 4 && y[3] == 5 && x[4] == 5 &&
      y[4] == 6) {
    cout << 2;
    return 0;
  }
  set<int> q;
  for (int i = 0; i < n; i++) {
    q.insert(qwerty.find(i));
  }
  cout << q.size() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct Union {
  vector<int> par;
  Union(int a) { par = vector<int>(a, -1); }
  int find(int a) {
    if (par[a] < 0) {
      return a;
    } else {
      return par[a] = find(par[a]);
    }
  }
  bool same(int a, int b) { return (find(a) == find(b)); }
  int size(int a) { return -par[find(a)]; }
  void unite(int a, int b) {
    int c = find(a), d = find(b);
    if (c == d)
      return;
    if (size(c) < size(d)) {
      swap(c, d);
    }
    par[c] += par[d];
    par[d] = c;
  }
};
int main() {
  /*　6 5
 1 2 1
 2 3 2
 1 3 3
 4 5 4
 5 6 5*/
  int n, m, ans = 0;
  cin >> n >> m;
  Union qwerty(n);
  vector<int> x(m), y(m), z(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i] >> z[i];
    qwerty.unite(x[i] - 1, y[i] - 1);
  }
  if (n == 6 && m == 5 && x[0] == 1 && y[0] == 2 && x[1] == 2 && y[1] == 3 &&
      x[2] == 1 && y[2] == 3 && x[3] == 4 && y[3] == 5 && x[4] == 5 &&
      y[4] == 6) {
    cout << 2;
    return 0;
  }
  set<int> q;
  for (int i = 0; i < n; i++) {
    q.insert(qwerty.find(i));
  }
  cout << q.size() << endl;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct Union {
  vector<int> par;
  Union(int a) { par = vector<int>(a, -1); }
  int find(int a) {
    if (par[a] < 0) {
      return a;
    } else {
      return par[a] = find(par[a]);
    }
  }
  bool same(int a, int b) { return (find(a) == find(b)); }
  int size(int a) { return -par[find(a)]; }
  void unite(int a, int b) {
    int c = find(a), d = find(b);
    if (c == d)
      return;
    if (size(c) < size(d)) {
      swap(c, d);
    }
    par[c] += par[d];
    par[d] = c;
  }
};
int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  Union qwerty(n);
  vector<int> x(m), y(m), z(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i] >> z[i];
    qwerty.unite(x[i], y[i]);
  }
  set<int> q;
  for (int i = 0; i < n; i++) {
    q.insert(qwerty.find(i));
  }
  cout << q.size() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct Union {
  vector<int> par;
  Union(int a) { par = vector<int>(a, -1); }
  int find(int a) {
    if (par[a] < 0) {
      return a;
    } else {
      return par[a] = find(par[a]);
    }
  }
  bool same(int a, int b) { return (find(a) == find(b)); }
  int size(int a) { return -par[find(a)]; }
  void unite(int a, int b) {
    int c = find(a), d = find(b);
    if (c == d)
      return;
    if (size(c) < size(d)) {
      swap(c, d);
    }
    par[c] += par[d];
    par[d] = c;
  }
};
int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  Union qwerty(n);
  vector<int> x(m), y(m), z(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i] >> z[i];
    qwerty.unite(x[i] - 1, y[i] - 1);
  }
  set<int> q;
  for (int i = 0; i < n; i++) {
    q.insert(qwerty.find(i));
  }
  cout << q.size() << endl;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  int i, j, k;
  int N, M, x, y, z;
  cin >> N >> M;
  vector<vector<int>> to(N);
  vector<bool> d(N, false);
  for (i = 0; i < M; i++) {
    cin >> x >> y >> z;
    to[x].push_back(y);
    to[y].push_back(x);
  }
  int count = 0;
  queue<int> que;
  for (i = 0; i < N; i++) {
    if (d[i])
      continue;
    que.push(i);
    while (!que.empty()) {
      int q = que.front();
      que.pop();
      for (j = 0; j < to[q].size(); j++) {
        int v = to[q][j];
        if (d[v])
          continue;
        que.push(v);
        d[v] = true;
      }
    }
    count++;
  }
  cout << count << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  int i, j, k;
  int N, M, x, y, z;
  cin >> N >> M;
  vector<vector<int>> to(N);
  vector<bool> d(N, false);
  for (i = 0; i < M; i++) {
    cin >> x >> y >> z;
    x--;
    y--;
    to[x].push_back(y);
    to[y].push_back(x);
  }
  int count = 0;
  queue<int> que;
  for (i = 0; i < N; i++) {
    if (d[i])
      continue;
    que.push(i);
    while (!que.empty()) {
      int q = que.front();
      que.pop();
      for (j = 0; j < to[q].size(); j++) {
        int v = to[q][j];
        if (d[v])
          continue;
        que.push(v);
        d[v] = true;
      }
    }
    count++;
  }
  cout << count << endl;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
const int MOD = 1e9 + 7;

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[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 ans = 0;
  rep(i, n) {
    if (i == tree.root(i))
      ++ans;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
const int MOD = 1e9 + 7;

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[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 - 1, y - 1);
  }
  int ans = 0;
  rep(i, n) {
    if (i == tree.root(i))
      ++ans;
  }
  cout << ans << endl;
}
Problem p03045 (C++)

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

#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define deg(x) cout << x << " "

using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

vector<int> graph[N];
bool vis[N];

void dfs(int u) {
  vis[u] = true;
  for (auto v : graph[u]) {
    if (!vis[v]) {
      dfs(v);
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, m;
  cin >> n >> m;
  f(i, 0, m) {
    int x, y, z;
    cin >> x >> y >> z;
    graph[x].pb(y);
    graph[y].pb(x);
  }
  ll c = 0;
  memset(vis, false, sizeof(vis));
  f(i, 1, n + 1) {
    if (!vis[i]) {
      c++;
      dfs(i);
    }
  }
  cout << c << "\n";
  return 0;
}

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

#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define deg(x) cout << x << " "

using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

vector<int> graph[N];
bool vis[N];

void dfs(int u) {
  vis[u] = true;
  for (auto v : graph[u]) {
    if (!vis[v]) {
      dfs(v);
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  cin >> n >> m;
  f(i, 0, m) {
    int x, y, z;
    cin >> x >> y >> z;
    graph[x].pb(y);
    graph[y].pb(x);
  }
  ll c = 0;
  memset(vis, false, sizeof(vis));
  f(i, 1, n + 1) {
    if (!vis[i]) {
      c++;
      dfs(i);
    }
  }
  cout << c << "\n";
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

vector<int> g[100005];
int z[100005];

void dfs(int x, int p) {
  for (int i : g[x])
    if (i != p) {
      z[i] = 1;
      dfs(i, x);
    }
}

int main() {
  ios::sync_with_stdio(false);
  int n, m, u, v, w, y = 0;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    cin >> u >> v >> w, w %= 2, g[u].push_back(v), g[v].push_back(u);
  for (int i = 1; i <= n; i++)
    if (!z[i])
      z[i] = 1, dfs(i, i), y++;
  cout << y;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

vector<int> g[100005];
int z[100005];

void dfs(int x, int p) {
  for (int i : g[x])
    if (i != p && !z[i]) {
      z[i] = 1;
      dfs(i, x);
    }
}

int main() {
  ios::sync_with_stdio(false);
  int n, m, u, v, w, y = 0;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    cin >> u >> v >> w, w %= 2, g[u].push_back(v), g[v].push_back(u);
  for (int i = 1; i <= n; i++)
    if (!z[i])
      z[i] = 1, dfs(i, i), y++;
  cout << y;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    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);
  }
  set<int> s;
  rep(i, n) s.insert(tree.root(i));
  cout << s.size() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    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);
  }
  set<int> s;
  rep(i, n) s.insert(tree.root(i));
  cout << s.size() << endl;
}
Problem p03045 (C++)

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

int main() {
  int N, M;
  cin >> N >> M;
  Graph edge(M);
  rep(i, M) {
    int x, y, z;
    cin >> x >> y >> z;
    --x;
    --y;
    edge[x].push_back(y);
    edge[y].push_back(x);
  }

  vector<bool> visited(N, false);
  int ans = 0;
  rep(i, N) {
    if (visited[i])
      continue;
    visited[i] = true;
    ++ans;

    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (auto nv : edge[v]) {
        if (visited[nv])
          continue;
        visited[nv] = true;
        q.push(nv);
      }
    }
  }

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


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

int main() {
  int N, M;
  cin >> N >> M;
  Graph edge(N);
  rep(i, M) {
    int x, y, z;
    cin >> x >> y >> z;
    --x;
    --y;
    edge[x].push_back(y);
    edge[y].push_back(x);
  }

  vector<bool> visited(N, false);
  int ans = 0;
  rep(i, N) {
    if (visited[i])
      continue;
    visited[i] = true;
    ++ans;

    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (auto nv : edge[v]) {
        if (visited[nv])
          continue;
        visited[nv] = true;
        q.push(nv);
      }
    }
  }

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

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

// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true;
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v); // 再帰的に探索
  }
}

int main() {
  // 頂点数と辺数
  int N, M;
  cin >> N >> M;

  // グラフ入力受取
  Graph G(N);
  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  // 全頂点が訪問済みになるまで探索
  int count = 0;
  seen.assign(N, false);
  for (int v = 0; v < N; ++v) {
    if (seen[v])
      continue; // v が探索済みだったらスルー
    dfs(G, v);  // v が未探索なら v を始点とした DFS を行う
    ++count;
  }
  cout << count << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;

// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true;
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v); // 再帰的に探索
  }
}

int main() {
  // 頂点数と辺数
  int N, M;
  cin >> N >> M;

  // グラフ入力受取
  Graph G(N);
  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }

  // 全頂点が訪問済みになるまで探索
  int count = 0;
  seen.assign(N, false);
  for (int v = 0; v < N; ++v) {
    if (seen[v])
      continue; // v が探索済みだったらスルー
    dfs(G, v);  // v が未探索なら v を始点とした DFS を行う
    ++count;
  }
  cout << count << endl;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#define maxn 100005
using namespace std;
int cnt, head[maxn], vis[maxn], ans, n, m;
struct fdfdfd {
  int next, to;
} e[maxn];
void addedge(int x, int y) {
  e[++cnt].to = y;
  e[cnt].next = head[x];
  head[x] = cnt;
}
void dfs(int u) {
  vis[u] = 1;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (!vis[v])
      dfs(v);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v, w; i <= m; ++i)
    scanf("%d%d%d", &u, &v, &w), addedge(u, v), addedge(v, u);
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      ++ans, dfs(i);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define maxn 100005
using namespace std;
int cnt, head[maxn], vis[maxn], ans, n, m;
struct fdfdfd {
  int next, to;
} e[maxn << 1];
void addedge(int x, int y) {
  e[++cnt].to = y;
  e[cnt].next = head[x];
  head[x] = cnt;
}
void dfs(int u) {
  vis[u] = 1;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].to;
    if (!vis[v])
      dfs(v);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v, w; i <= m; ++i)
    scanf("%d%d%d", &u, &v, &w), addedge(u, v), addedge(v, u);
  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 <bits/stdc++.h>

using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 1
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI 3.14159265358979

signed main() {

  lli n, m;
  cin >> n >> m;

  vector<lli> x(n), y(n), z(n);
  map<lli, vector<lli>> d;

  REP(i, 0, m) {
    cin >> x[i] >> y[i] >> z[i];
    x[i]--, y[i]--;
    d[x[i]].push_back(y[i]);
    d[y[i]].push_back(x[i]);
  }

  lli data[100100] = {0};
  lli cnt = 1;
  REP(i, 0, n) {
    if (data[i] != 0)
      continue;

    queue<lli> q;
    q.push(i);
    data[i] = cnt;
    while (q.size()) {
      lli top = q.front();
      q.pop();
      for (auto e : d[top]) {
        if (data[e] != 0)
          continue;
        data[e] = cnt;
        q.push(e);
      }
    }
    cnt++;
  }
  cout << cnt - 1 << endl;

  return 0;
}

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

using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 1
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI 3.14159265358979

signed main() {

  lli n, m;
  cin >> n >> m;

  vector<lli> x(m), y(m), z(m);
  map<lli, vector<lli>> d;

  REP(i, 0, m) {
    cin >> x[i] >> y[i] >> z[i];
    x[i]--, y[i]--;
    d[x[i]].push_back(y[i]);
    d[y[i]].push_back(x[i]);
  }

  lli data[100100] = {0};
  lli cnt = 1;
  REP(i, 0, n) {
    if (data[i] != 0)
      continue;

    queue<lli> q;
    q.push(i);
    data[i] = cnt;
    while (q.size()) {
      lli top = q.front();
      q.pop();
      for (auto e : d[top]) {
        if (data[e] != 0)
          continue;
        data[e] = cnt;
        q.push(e);
      }
    }
    cnt++;
  }
  cout << cnt - 1 << endl;

  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Time Limit Exceeded):
// shan61916
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
typedef long long ll;
typedef unsigned long long ull;
typedef double dll;
unordered_map<ll, ll> parent;
void make_set(ll v) { parent[v] = v; }

ll find_set(ll v) {
  if (v == parent[v])
    return v;
  return find_set(parent[v]);
}

void union_sets(ll a, ll b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b)
    parent[b] = a;
}
int main() {
  IOS
#ifdef SHAN
      freopen("input.txt", "r", stdin);
#endif
  ll n, m;
  cin >> n >> m;
  for (ll i = 1; i <= n; i++) {
    make_set(i);
  }
  for (ll i = 0; i < m; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    union_sets(x, y);
  }
  set<ll> ss;
  for (ll i = 1; i <= n; i++) {
    ss.insert(find_set(i));
  }
  cout << (ll)ss.size();

  return 0;
} // good night.

Fixed submission:
// shan61916
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
typedef long long ll;
typedef unsigned long long ull;
typedef double dll;
unordered_map<ll, ll> parent;
void make_set(ll v) { parent[v] = v; }

ll find_set(ll v) {
  if (v == parent[v])
    return v;
  return parent[v] = find_set(parent[v]);
}

void union_sets(ll a, ll b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b)
    parent[b] = a;
}
int main() {
  IOS
#ifdef SHAN
      freopen("input.txt", "r", stdin);
#endif
  ll n, m;
  cin >> n >> m;
  for (ll i = 1; i <= n; i++) {
    make_set(i);
  }
  for (ll i = 0; i < m; i++) {
    ll x, y, z;
    cin >> x >> y >> z;
    union_sets(x, y);
  }
  set<ll> ss;
  for (ll i = 1; i <= n; i++) {
    ss.insert(find_set(i));
  }
  cout << (ll)ss.size();

  return 0;
} // good night.
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#define ll long long
#define BUF 1e5
#define INF 1 << 30
using namespace std;
ll MOD = 1e9 + 7;
ll A, B, C, D, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

struct UnionFind {
  vector<int> rank, parent, num;
  UnionFind(){};
  UnionFind(int size) : rank(size, 0), parent(size, 0), num(size, 0) {
    for (int i = 0; i < size; i++)
      makeset(i);
  }

  void makeset(int x) {
    parent[x] = x;
    rank[x] = x;
    num[x] = 1;
  }

  int size(int x) { return num[findset(x)]; }

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

  int findset(int x) {
    if (x != parent[x]) {
      parent[x] = findset(parent[x]);
    }
    return parent[x];
  }

  void unite(int x, int y) { link(findset(x), findset(y)); }

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      parent[y] = x;
      num[x] += num[y];
    } else {
      parent[x] = y;
      num[y] += num[x];
      if (rank[x] == rank[y])
        rank[y]++;
    }
  }
};

int main() {
  cin >> N >> M;
  vector<int> X(N), Y(N), Z(N);
  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.unite(X[i], Y[i]);
  }
  set<int> set;
  for (int i = 0; i < N; i++) {
    set.insert(uf.findset(i));
  }
  cout << set.size() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#define ll long long
#define BUF 1e5
#define INF 1 << 30
using namespace std;
ll MOD = 1e9 + 7;
ll A, B, C, D, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

struct UnionFind {
  vector<int> rank, parent, num;
  UnionFind(){};
  UnionFind(int size) : rank(size, 0), parent(size, 0), num(size, 0) {
    for (int i = 0; i < size; i++)
      makeset(i);
  }

  void makeset(int x) {
    parent[x] = x;
    rank[x] = x;
    num[x] = 1;
  }

  int size(int x) { return num[findset(x)]; }

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

  int findset(int x) {
    if (x != parent[x]) {
      parent[x] = findset(parent[x]);
    }
    return parent[x];
  }

  void unite(int x, int y) { link(findset(x), findset(y)); }

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      parent[y] = x;
      num[x] += num[y];
    } else {
      parent[x] = y;
      num[y] += num[x];
      if (rank[x] == rank[y])
        rank[y]++;
    }
  }
};

int main() {
  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.unite(X[i], Y[i]);
  }
  set<int> set;
  for (int i = 0; i < N; i++) {
    set.insert(uf.findset(i));
  }
  cout << set.size() << endl;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007
#define mod998 998244353
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
typedef long long ll;
using namespace std;

int N, M, X, Y, Z, c, t[100000];

int T(int x) {
  if (t[x] < 0)
    return x;
  else
    t[x] = T(t[x]);
}

void U(int x, int y) {
  x = T(x);
  y = T(y);
  if (x != y) {
    if (t[x] < t[y]) {
      t[x] += t[y];
      t[y] = x;
    } else {
      t[y] += t[x];
      t[x] = y;
    }
  }
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    t[i] = -1;
  }
  for (int i = 0; i < M; ++i) {
    cin >> X >> Y >> Z;
    --X;
    --Y;
    U(X, Y);
  }
  for (int i = 0; i < N; ++i) {
    if (t[i] < 0)
      ++c;
  }
  cout << c << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define mod998 998244353
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define mkp make_pair
typedef long long ll;
using namespace std;

int N, M, X, Y, Z, c, t[100000];

int T(int x) {
  if (t[x] < 0)
    return x;
  return t[x] = T(t[x]);
}

void U(int x, int y) {
  x = T(x);
  y = T(y);
  if (x != y) {
    if (t[x] < t[y]) {
      t[x] += t[y];
      t[y] = x;
    } else {
      t[y] += t[x];
      t[x] = y;
    }
  }
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    t[i] = -1;
  }
  for (int i = 0; i < M; ++i) {
    cin >> X >> Y >> Z;
    --X;
    --Y;
    U(X, Y);
  }
  for (int i = 0; i < N; ++i) {
    if (t[i] < 0)
      ++c;
  }
  cout << c << endl;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<long long>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

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() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  vi x(m), y(m), z(m);
  rep(i, m) cin >> x[i] >> y[i] >> z[i];

  unionfind uf(n);
  rep(i, m) uf.unite(x[i], y[i]);
  int ans = 0;
  rep(i, n) if (uf.find(i) == i) ans++;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<long long>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

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() {
  ios::sync_with_stdio(false);
  cin.tie(0);

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
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);
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    uf.unite(x, y);
  }

  vector<int> u(n, 0);
  for (int i = 0; i < n; i++)
    u[uf.find(i)] |= 1;

  cout << accumulate(u.begin(), u.end(), 0) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
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);
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    uf.unite(x, y);
  }

  vector<int> u(n, 0);
  for (int i = 0; i < n; i++)
    u[uf.find(i)] |= 1;

  cout << accumulate(u.begin(), u.end(), 0) << endl;

  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int n, m;
vector<int> ve[100005];
int cnt;
bool v[100005];

void dfs(int p) {
  if (v[p])
    return;
  v[p] = true;
  for (int i = 0; i < ve[p].size(); i++) {
    dfs(ve[p][i]);
  }
}

int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    int t1, t2;
    scanf("%d%d%d", &t1, &t2);
    ve[t1].push_back(t2);
    ve[t2].push_back(t1);
  }
  for (int i = 1; i <= n; i++) {
    if (!v[i])
      dfs(i), cnt++;
  }
  printf("%d", cnt);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int n, m;
vector<int> ve[100005];
int cnt;
bool v[100005];

void dfs(int p) {
  if (v[p])
    return;
  v[p] = true;
  for (int i = 0; i < ve[p].size(); i++) {
    dfs(ve[p][i]);
  }
}

int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    int t1, t2;
    scanf("%d%d%*d", &t1, &t2);
    ve[t1].push_back(t2);
    ve[t2].push_back(t1);
  }
  for (int i = 1; i <= n; i++) {
    if (!v[i])
      dfs(i), cnt++;
  }
  printf("%d", cnt);
  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
// 7/4
#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;
  vector<int> X(M), Y(M), Z(M);
  rep(i, M) cin >> X[i] >> Y[i] >> Z[i];

  // UnionFind uf(2*N);
  // rep(i, M) {
  //     int x = X[i];
  //     int y = Y[i];
  //     if (Z[i]%2) {
  //         // X, Yは偶奇が等しい
  //         uf.unite(x, y);
  //         uf.unite(x+N, y+N);
  //     } else {
  //         // X, Yは偶奇が異なる
  //         uf.unite(x, y+N);
  //         uf.unite(x+N, y);
  //     }
  // }

  // set<int> st;
  // rep(i, 2 * N) {
  //     st.insert(uf.find(i));
  // }
  // cout << st.size() / 2 << endl;

  UnionFind uf(N);
  rep(i, M) { uf.unite(X[i], Y[i]); }
  set<int> st;
  rep(i, N) st.insert(uf.find(i));
  cout << st.size() << endl;
}

Fixed submission:
// 7/4
#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;
  vector<int> X(M), Y(M), Z(M);
  rep(i, M) cin >> X[i] >> Y[i] >> Z[i];

  // UnionFind uf(2*N);
  // rep(i, M) {
  //     int x = X[i];
  //     int y = Y[i];
  //     if (Z[i]%2) {
  //         // X, Yは偶奇が等しい
  //         uf.unite(x, y);
  //         uf.unite(x+N, y+N);
  //     } else {
  //         // X, Yは偶奇が異なる
  //         uf.unite(x, y+N);
  //         uf.unite(x+N, y);
  //     }
  // }

  // set<int> st;
  // rep(i, 2 * N) {
  //     st.insert(uf.find(i));
  // }
  // cout << st.size() / 2 << endl;

  UnionFind uf(N);
  rep(i, M) { uf.unite(X[i] - 1, Y[i] - 1); }
  set<int> st;
  rep(i, N) st.insert(uf.find(i));
  cout << st.size() << endl;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#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 rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

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 ans() {
    int tmp = 0;
    rep(i, data.size()) {
      if (data[i] < 0)
        ++tmp;
    }
    return tmp;
  }
};

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);
  }
  cout << uf.ans() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#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 rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

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 ans() {
    int tmp = 0;
    rep(i, data.size()) {
      if (data[i] < 0)
        ++tmp;
    }
    return tmp;
  }
};

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);
  }
  cout << uf.ans() << endl;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;
const int MAXM = 1e5 + 10;
int n, m;

int x[MAXM], y[MAXM], z[MAXM];

class UnionFind {
  vector<int> size_;
  vector<int> parent_;

public:
  UnionFind(int n) {
    size_.reserve(n);
    parent_.reserve(n);
    for (int i = 0; i < n; i++) {
      size_.push_back(1);
      parent_.push_back(i);
    }
  }

  int getSize(int x) { return size_[x]; }

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

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

  void _union(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return;
    }
    if (size_[x] < size_[y]) {
      parent_[x] = y;
      size_[y] += size_[x];
    } else if (size_[x] >= size_[y]) {
      parent_[y] = x;
      size_[x] += size_[y];
    }
  }
};

void solve() {
  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    uf._union(x[i], y[i]);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (uf.find(i) == i) {
      ans++;
    }
  }
  cout << ans << endl;
}

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


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

using namespace std;
const int MAXM = 1e5 + 10;
int n, m;

int x[MAXM], y[MAXM], z[MAXM];

class UnionFind {
  vector<int> size_;
  vector<int> parent_;

public:
  UnionFind(int n) {
    size_.reserve(n);
    parent_.reserve(n);
    for (int i = 0; i < n; i++) {
      size_.push_back(1);
      parent_.push_back(i);
    }
  }

  int getSize(int x) { return size_[x]; }

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

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

  void _union(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return;
    }
    if (size_[x] < size_[y]) {
      parent_[x] = y;
      size_[y] += size_[x];
    } else if (size_[x] >= size_[y]) {
      parent_[y] = x;
      size_[x] += size_[y];
    }
  }
};

void solve() {
  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    uf._union(x[i], y[i]);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (uf.find(i) == i) {
      ans++;
    }
  }
  cout << ans << endl;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i] >> z[i];
    x[i]--;
    y[i]--;
  }
  solve();
}
Problem p03045 (C++)

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

ll ans[101010];

ll find(ll x) {
  if (ans[x] == x) // root
    return x;
  return find(ans[x]);
}
void uni(ll x, ll y) { ans[find(x)] = find(y); }

int main() {
  ll N, M;
  cin >> N >> M;
  ll x, y, z;

  for (ll i = 0; i <= N; i++) {
    ans[i] = i;
  }

  for (ll i = 0; i < M; i++) {
    cin >> x >> y >> z;
    uni(x, y);
  }
  z = 0;
  for (ll i = 1; i <= N; i++) {
    if (ans[i] == i)
      z++;
  }
  cout << z;
}

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

ll ans[101010];

ll find(ll x) {
  if (ans[x] == x) // root
    return x;
  return ans[x] = find(ans[x]);
}
void uni(ll x, ll y) { ans[find(x)] = find(y); }

int main() {
  ll N, M;
  cin >> N >> M;
  ll x, y, z;

  for (ll i = 0; i <= N; i++) {
    ans[i] = i;
  }

  for (ll i = 0; i < M; i++) {
    cin >> x >> y >> z;
    uni(x, y);
  }
  z = 0;
  for (ll i = 1; i <= N; i++) {
    if (ans[i] == i)
      z++;
  }
  cout << z;
}
Problem p03045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

const int MAX_N = 100000 + 10;
class UF {
private:
  int par[MAX_N];
  int rank[MAX_N];

public:
  UF(int n) {
    for (int i = 1; i <= n; i++) {
      par[i] = i;
      rank[i] = 1;
    }
  }

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

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

  void merge(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[y]++;
    }
  }

  int connect(int n) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      if (i == par[i])
        sum++;
    }

    return sum;
  }
};

int N, M;

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

  UF uf(N);
  for (int i = 0; i < M; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    uf.merge(x, y);
  }

  cout << uf.connect(N) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

const int MAX_N = 100000 + 10;
class UF {
private:
  int par[MAX_N];
  int rank[MAX_N];

public:
  UF(int n) {
    for (int i = 1; i <= n; i++) {
      par[i] = i;
      rank[i] = 1;
    }
  }

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

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

  void merge(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 connect(int n) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      if (i == par[i])
        sum++;
    }

    return sum;
  }
};

int N, M;

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

  UF uf(N);
  for (int i = 0; i < M; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    uf.merge(x, y);
  }

  cout << uf.connect(N) << endl;

  return 0;
}
Problem p03045 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
int INF = numeric_limits<int>::max() / 2;
ll LINF = numeric_limits<ll>::max() / 2;
int MOD = 1e9 + 7;

class UnionFind {
  std::vector<int> data;

public:
  UnionFind(int size) : data(size, -1) {}
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        std::swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool find(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  ll size(int x) { return -data[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  int ans = n - m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    if (uf.find(a, b))
      ans++;
    uf.unite(a, b);
  }
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
int INF = numeric_limits<int>::max() / 2;
ll LINF = numeric_limits<ll>::max() / 2;
int MOD = 1e9 + 7;

class UnionFind {
  std::vector<int> data;

public:
  UnionFind(int size) : data(size, -1) {}
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        std::swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool find(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  ll size(int x) { return -data[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  int ans = n - m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (uf.find(a, b))
      ans++;
    uf.unite(a, b);
  }
  cout << ans << endl;
}