Problem p00859 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind(int n) : data(n, -1) {}
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y]; //???????????´??°
      data[y] = x;        //????????´??°
    }
    return x != y;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int find(int x) {
    if (data[x] < 0)
      return x;
    return data[x] = find(data[x]);
  }
};

struct Edge {
  int cost, from, to;
  Edge(int cost, int from, int to) : cost(cost), from(from), to(to) {}
};

bool operator<(const Edge &a, const Edge &b) {
  return a.cost != b.cost   ? a.cost < b.cost
         : a.from != b.from ? a.from << b.from
                            : a.to < b.to;
}

int main() {
  int n, m, INF = 1000000000;
  while (cin >> n >> m, n) {
    int a, b, w;
    vector<Edge> E;
    for (int i = 0; i < m; ++i) {
      cin >> a >> b >> w;
      --a;
      --b;
      // E.emplace_back(w,a,b);
      E.push_back(Edge(w, a, b));
    }
    sort(E.begin(), E.end());
    int ans = INF;

    for (int i = 0; i < m; ++i) {
      int e = n - 1;
      UnionFind uf(n);
      bool f = true;
      for (int j = i; j < m; ++j) {
        int cost = E[j].cost, from = E[j].from, to = E[j].to;
        if (!uf.same(from, to)) {
          --e;
          uf.unite(from, to);
        }
        if (!e) {
          ans = min(ans, cost - E[i].cost);
          f = false;
          break;
        }
      }
      if (f)
        break;
    }
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind(int n) : data(n, -1) {}
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y]; //???????????´??°
      data[y] = x;        //????????´??°
    }
    return x != y;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int find(int x) {
    if (data[x] < 0)
      return x;
    return data[x] = find(data[x]);
  }
};

struct Edge {
  int cost, from, to;
  Edge(int cost, int from, int to) : cost(cost), from(from), to(to) {}
};

bool operator<(const Edge &a, const Edge &b) {
  return a.cost != b.cost   ? a.cost < b.cost
         : a.from != b.from ? a.from < b.from
                            : a.to < b.to;
}

int main() {
  int n, m, INF = 1000000000;
  while (cin >> n >> m, n) {
    int a, b, w;
    vector<Edge> E;
    for (int i = 0; i < m; ++i) {
      cin >> a >> b >> w;
      --a;
      --b;
      // E.emplace_back(w,a,b);
      E.push_back(Edge(w, a, b));
    }
    sort(E.begin(), E.end());
    int ans = INF;

    for (int i = 0; i < m; ++i) {
      int e = n - 1;
      UnionFind uf(n);
      bool f = true;
      for (int j = i; j < m; ++j) {
        int cost = E[j].cost, from = E[j].from, to = E[j].to;
        if (!uf.same(from, to)) {
          --e;
          uf.unite(from, to);
        }
        if (!e) {
          ans = min(ans, cost - E[i].cost);
          f = false;
          break;
        }
      }
      if (f)
        break;
    }
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}