Problem p03915 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
using std::map;
using std::max;
using std::min;
using std::sort;
using std::string;
using std::swap;
typedef long long ll;

template <typename T> void read(T &x) {
  int flag = 1;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -flag;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
#if __cplusplus >= 201103l
template <typename T, typename... V> void read(T &x, V &...v) {
  read(x), read(v...);
}
#endif

const int _ = 2e5 + 10;
int n, q, ff[_], d[_];
struct Edge {
  int u, v, w;
} e[_];
int cnt;
inline bool operator<(const Edge &a, const Edge &b) { return a.w < b.w; }
inline void addEdge(int u, int v, int w) { e[++cnt] = {u, v, w}; }
void ChkMin(int &a, int b) {
  if (b < a)
    a = b;
}
int find(int x) { return ff[x] == x ? x : ff[x] = find(ff[x]); }

int main() {
  read(n, q), memset(d, 127, sizeof(d[0]) * n);
  for (int i = 1, x, y, z; i <= q; ++i)
    read(x, y, z), addEdge(x, y, z), ChkMin(d[x], z + 1), ChkMin(d[y], z + 2);
  for (int i = 0; i < n; ++i)
    ChkMin(d[i], d[(i + n - 1) % n] + 2);
  for (int i = 0; i < n; ++i)
    ChkMin(d[i], d[(i + n - 1) % n] + 2);
  for (int i = 0; i < n; ++i)
    addEdge(i, (i + 1) % n, d[i]), ff[i] = i;
  sort(e + 1, e + cnt + 1);
  int blo = n;
  ll MST = 0;
  for (int i = 1; i <= cnt && blo > 1; ++i) {
    int u = find(e[i].u), v = find(e[i].v);
    if (u == v)
      continue;
    ff[u] = v, MST += e[i].w, --blo;
  }
  printf("%lld\n", MST);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
using std::map;
using std::max;
using std::min;
using std::sort;
using std::string;
using std::swap;
typedef long long ll;

template <typename T> void read(T &x) {
  int flag = 1;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -flag;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
#if __cplusplus >= 201103l
template <typename T, typename... V> void read(T &x, V &...v) {
  read(x), read(v...);
}
#endif

const int _ = 4e5 + 10;
int n, q, ff[_], d[_];
struct Edge {
  int u, v, w;
} e[_];
int cnt;
inline bool operator<(const Edge &a, const Edge &b) { return a.w < b.w; }
inline void addEdge(int u, int v, int w) { e[++cnt] = {u, v, w}; }
void ChkMin(int &a, int b) {
  if (b < a)
    a = b;
}
int find(int x) { return ff[x] == x ? x : ff[x] = find(ff[x]); }

int main() {
  read(n, q), memset(d, 127, sizeof(d[0]) * n);
  for (int i = 1, x, y, z; i <= q; ++i)
    read(x, y, z), addEdge(x, y, z), ChkMin(d[x], z + 1), ChkMin(d[y], z + 2);
  for (int i = 0; i < n; ++i)
    ChkMin(d[i], d[(i + n - 1) % n] + 2);
  for (int i = 0; i < n; ++i)
    ChkMin(d[i], d[(i + n - 1) % n] + 2);
  for (int i = 0; i < n; ++i)
    addEdge(i, (i + 1) % n, d[i]), ff[i] = i;
  sort(e + 1, e + cnt + 1);
  int blo = n;
  ll MST = 0;
  for (int i = 1; i <= cnt && blo > 1; ++i) {
    int u = find(e[i].u), v = find(e[i].v);
    if (u == v)
      continue;
    ff[u] = v, MST += e[i].w, --blo;
  }
  printf("%lld\n", MST);
  return 0;
}