Problem p02364 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int V;
vector<int> v, parent, depth;

struct edge {
  int f, t, c;
  bool operator<(const edge &e) const { return c < e.c; };
};

int find(int a) {
  if (parent[a] == a)
    return a;

  // ここで、検索時に辺の縮約も行う
  else
    return parent[a] = find(parent[a]);
}

void merge(int a, int b) {
  int pa = find(a), pb = find(b);
  if (pa == pb)
    return;

  if (depth[pa] > depth[pb])
    swap(pa, pb);
  if (depth[pa] == depth[pb])
    ++depth[pa];
  parent[pb] = pa;
}

bool same(int a, int b) { return find(a) == find(b); }

void init_union_find(int V) {
  v = vector<int>(V);
  parent = vector<int>(V);
  depth = vector<int>(V, 1);
  REP(i, V) parent[i] = i;
}

int kruskal(int V, int E, vector<edge> es) {
  sort(es.begin(), es.end());
  init_union_find(V);
  int min_cost = 0;
  REP(i, E) {
    if (!same(es[i].f, es[i].t)) {
      min_cost += es[i].c;
      merge(es[i].f, es[i].t);
    }
  }
  return min_cost;
}

int main() {
  int E;
  cin >> V >> E;
  vector<edge> es(E);
  REP(i, E) cin >> es[i].f >> es[i].t >> es[i].c;
  cout << kruskal(V, E, es) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int V;
vector<int> v, parent, depth;

struct edge {
  int f, t, c;
  bool operator<(const edge &e) const { return c < e.c; };
};

int find(int a) {
  if (parent[a] == a)
    return a;

  // ここで、検索時に辺の縮約も行う
  else
    return parent[a] = find(parent[a]);
}

void merge(int a, int b) {
  int pa = find(a), pb = find(b);
  if (pa == pb)
    return;

  if (depth[pa] > depth[pb])
    swap(pa, pb);
  if (depth[pa] == depth[pb])
    ++depth[pa];
  parent[pb] = pa;
}

bool same(int a, int b) { return find(a) == find(b); }

void init_union_find(int V) {
  v = vector<int>(V);
  parent = vector<int>(V);
  depth = vector<int>(V, 1);
  REP(i, V) parent[i] = i;
}

int kruskal(int V, vector<edge> es) {
  sort(es.begin(), es.end());
  init_union_find(V);
  int min_cost = 0;
  REP(i, V) {
    if (!same(es[i].f, es[i].t)) {
      min_cost += es[i].c;
      merge(es[i].f, es[i].t);
    }
  }
  return min_cost;
}

int main() {
  int E;
  cin >> V >> E;
  vector<edge> es(E);
  REP(i, E) cin >> es[i].f >> es[i].t >> es[i].c;
  cout << kruskal(V, es) << endl;
  return 0;
}