Problem p02931 (C++)

Buggy submission (status: Runtime Error):
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define MAX 100100
#define ll long long
inline int read() {
  int x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    t = true, ch = getchar();
  while (ch <= '9' && ch >= '0')
    x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}
ll ans;
int n, h, w, tot, f[MAX];
bool cir[MAX];
struct edge {
  int u, v, w;
} E[MAX];
bool operator<(edge a, edge b) { return a.w > b.w; }
int getf(int x) { return x == f[x] ? x : f[x] = getf(f[x]); }
int main() {
  scanf("%d%d%d", &n, &h, &w);
  for (int i = 1; i <= h + w; ++i)
    f[i] = i;
  fp(i, 1, n) {
    R int x, y, c;
    scanf("%d%d%d", &x, &y, &c);

    E[++tot] = (edge){x, y + h, c};
  }
  //    for(int i=1;i<=n;++i)
  //        for(int j=1;j<=m;++j)
  //            E[++tot]=(edge){i,j+n,read()};
  sort(&E[1], &E[tot + 1]);
  for (int i = 1; i <= tot; ++i) {
    int u = getf(E[i].u), v = getf(E[i].v);
    if (u == v) {
      if (!cir[u])
        cir[u] = true, ans += E[i].w;
    } else if (!cir[u] || !cir[v])
      f[u] = v, ans += E[i].w, cir[v] |= cir[u];
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
// minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define MAX 500005
#define ll long long
inline int read() {
  int x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    t = true, ch = getchar();
  while (ch <= '9' && ch >= '0')
    x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}
ll ans;
int n, h, w, tot, f[MAX];
bool cir[MAX];
struct edge {
  int u, v, w;
} E[MAX];
bool operator<(edge a, edge b) { return a.w > b.w; }
int getf(int x) { return x == f[x] ? x : f[x] = getf(f[x]); }
int main() {
  scanf("%d%d%d", &n, &h, &w);
  for (int i = 1; i <= h + w; ++i)
    f[i] = i;
  fp(i, 1, n) {
    R int x, y, c;
    scanf("%d%d%d", &x, &y, &c);

    E[++tot] = (edge){x, y + h, c};
  }
  //    for(int i=1;i<=n;++i)
  //        for(int j=1;j<=m;++j)
  //            E[++tot]=(edge){i,j+n,read()};
  sort(&E[1], &E[tot + 1]);
  for (int i = 1; i <= tot; ++i) {
    int u = getf(E[i].u), v = getf(E[i].v);
    if (u == v) {
      if (!cir[u])
        cir[u] = true, ans += E[i].w;
    } else if (!cir[u] || !cir[v])
      f[u] = v, ans += E[i].w, cir[v] |= cir[u];
  }
  printf("%lld\n", ans);
  return 0;
}