Problem p03682 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
typedef pair<int, int> P;
vector<P> x, y;

struct UnionFind {
  vector<int> par;
  vector<int> height;

  UnionFind(int n) {
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      height.push_back(0);
    }
  }

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

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (height[x] < height[y])
      par[x] = y;
    else {
      par[y] = x;
      if (height[x] == height[y])
        height[x]++;
    }
  }

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

struct edge {
  int u;
  int v;
  int cost;
};

bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }

int kruskal(int V, int E, edge *es) {
  sort(es, es + E, comp);
  UnionFind uf(V);

  int res = 0;
  for (int i = 0; i < E; i++) {
    edge e = es[i];
    if (!uf.same(e.u, e.v)) {
      uf.unite(e.u, e.v);
      res += e.cost;
    }
  }

  return res;
}

edge es[100005];

int main() {
  int N;
  int tmpx, tmpy;

  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> tmpx >> tmpy;
    x.push_back(P(tmpx, i));
    y.push_back(P(tmpy, i));
  }

  sort(x.begin(), x.end());
  sort(y.begin(), y.end());

  for (int i = 0; i < N - 1; i++) {
    es[i].u = x[i].second;
    es[i].v = x[i + 1].second;
    es[i].cost = x[i + 1].first - x[i].first;
  }
  for (int i = 0; i < N - 1; i++) {
    es[i + N - 1].u = y[i].second;
    es[i + N - 1].v = y[i + 1].second;
    es[i + N - 1].cost = y[i + 1].first - y[i].first;
  }

  int cost;

  cost = kruskal(N, 2 * N - 2, es);

  cout << cost << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
typedef pair<int, int> P;
vector<P> x, y;

struct UnionFind {
  vector<int> par;
  vector<int> height;

  UnionFind(int n) {
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      height.push_back(0);
    }
  }

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

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (height[x] < height[y])
      par[x] = y;
    else {
      par[y] = x;
      if (height[x] == height[y])
        height[x]++;
    }
  }

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

struct edge {
  int u;
  int v;
  int cost;
};

bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }

int kruskal(int V, int E, edge *es) {
  sort(es, es + E, comp);
  UnionFind uf(V);

  int res = 0;
  for (int i = 0; i < E; i++) {
    edge e = es[i];
    if (!uf.same(e.u, e.v)) {
      uf.unite(e.u, e.v);
      res += e.cost;
    }
  }

  return res;
}

edge es[200010];

int main() {
  int N;
  int tmpx, tmpy;

  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> tmpx >> tmpy;
    x.push_back(P(tmpx, i));
    y.push_back(P(tmpy, i));
  }

  sort(x.begin(), x.end());
  sort(y.begin(), y.end());

  for (int i = 0; i < N - 1; i++) {
    es[i].u = x[i].second;
    es[i].v = x[i + 1].second;
    es[i].cost = x[i + 1].first - x[i].first;
  }
  for (int i = 0; i < N - 1; i++) {
    es[i + N - 1].u = y[i].second;
    es[i + N - 1].v = y[i + 1].second;
    es[i + N - 1].cost = y[i + 1].first - y[i].first;
  }

  int cost;

  cost = kruskal(N, 2 * N - 2, es);

  cout << cost << endl;

  return 0;
}