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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

class EDGE {
public:
  int no;
  int nord1;
  int nord2;
  int value;
  EDGE(int a, int b, int c, int d) : no(a), nord1(b), nord2(c), value(d) {}
};

bool operator<(const EDGE &edge1, const EDGE &edge2) {
  return edge1.value > edge2.value;
}

bool operator>(const EDGE &edge1, const EDGE &edge2) {
  return edge1.value < edge2.value;
}

int main() {
  int V, E; // V: 頂点の数 E: 辺の数
  cin >> V >> E;
  UnionFind uf(V);
  //    bool is_in[V]; // 頂点が木としてつながってるかどうか
  //    for (int i = 0; i < V; i++) is_in[i] = false;
  multiset<EDGE, greater<EDGE>> edge_set;
  for (int i = 0; i < E; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    EDGE edge(i, a, b, c);
    edge_set.insert(edge);
  }
  int ans = 0;
  multiset<EDGE>::iterator it = edge_set.begin();
  for (; it != edge_set.end(); it++) {
    if (uf.unionSet((*it).nord1, (*it).nord2)) {
      //            printf("%d-%d edge is added\\n", (*it).nord1, (*it).nord2);
      ans += (*it).value;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

class edge {
public:
  int no;
  int nord1;
  int nord2;
  int value;
  edge(int a, int b, int c, int d) : no(a), nord1(b), nord2(c), value(d) {}
};

bool operator<(const edge &edge1, const edge &edge2) {
  return edge1.value < edge2.value;
}
bool operator>(const edge &edge1, const edge &edge2) {
  return edge1.value > edge2.value;
}

int main(void) {
  int V, E; // V:頂点の数 E:辺の数
  int ans = 0;
  cin >> V >> E;
  bool is_in[V];
  bool is_used[E];
  priority_queue<edge, vector<edge>, greater<edge>> EDGE;
  for (int i = 0; i < V; i++)
    is_in[i] = false;
  for (int i = 0; i < E; i++)
    is_used[i] = false;
  for (int i = 0; i < E; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    EDGE.push(edge(i, a, b, c));
  }
  while (1) {
    // 新しい辺を追加
    while (1) {
      if (!is_used[EDGE.top().no]) {
        ans += EDGE.top().value;
        is_in[EDGE.top().nord1] = true;
        is_in[EDGE.top().nord2] = true;
        EDGE.pop();
        break;
      } else {
        EDGE.pop();
      }
    }
    // 終了判定
    int flag = 1;
    for (int i = 0; i < V; i++) {
      if (!is_in[i]) {
        flag = 0;
        break;
      }
    }
    if (flag == 1)
      break;
  }
  cout << ans << endl;
  return 0;
}