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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int n, m;

class UnionFind {
  vector<int> par;
  vector<int> rank;

public:
  UnionFind() {}
  UnionFind(int size) { reset(size); }

  void reset(int size) {
    par.resize(size);
    rank.resize(size);
    for (int i = 0; i < size; i++)
      par[i] = i, rank[i] = 0;
  }

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

  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

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

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

void solve() {
  cin >> m;
  vector<P> p(m);
  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    int x, y, z;
    scanf("%d,%d,%d", &x, &y, &z);
    p[i].f = x;
    p[i].t = y;
    p[i].c = z;
  }
  sort(p.begin(), p.end());
  int cost = 0;
  for (int i = 0; i < m; i++) {
    if (!uf.same(p[i].f, p[i].t)) {
      uf.unite(p[i].f, p[i].t);
      cost += p[i].c;
    }
  }

  cout << cost / 100 - (n - 1) << endl;
}

int main() {
  while (cin >> n, n) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int n, m;

class UnionFind {
  vector<int> par;
  vector<int> rank;

public:
  UnionFind() {}
  UnionFind(int size) { reset(size); }

  void reset(int size) {
    par.resize(size);
    rank.resize(size);
    for (int i = 0; i < size; i++)
      par[i] = i, rank[i] = 0;
  }

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

  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

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

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

bool use[100];

void solve() {
  cin >> m;
  vector<P> p(m);
  UnionFind uf(n);
  for (int i = 0; i < n; i++) {
    int x, y, z;
    scanf("%d,%d,%d", &x, &y, &z);
    p[i].f = x;
    p[i].t = y;
    p[i].c = z;
  }
  sort(p.begin(), p.end());
  int cost = 0;
  for (int i = 0; i < m; i++) {
    if (!uf.same(p[i].f, p[i].t)) {
      uf.unite(p[i].f, p[i].t);
      cost += p[i].c;
    }
  }

  cout << cost / 100 - (n - 1) << endl;
}

int main() {
  while (cin >> n, n) {
    solve();
  }
  return 0;
}