Problem p03682 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
typedef long long ll;
static const ll MOD = 1e9 + 7;
static const ll INF = (ll)1 << 62;

//////////////////////////////////////////////////////
// Union Find /////////////////////////////////////////
//////////////////////////////////////////////////////
static const ll MAX_M = 100001;
static const ll MAX_N = 100001;
int par[MAX_N];
int rak[MAX_N];

void init(int n) {
  rep(i, 0, n) {
    par[i] = i;
    rak[i] = 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 (rak[x] < rak[y])
    par[x] = y;
  else {
    par[x] = y;
    if (rak[x] == rak[y])
      rak[x]++;
  }
}

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

struct edge {
  ll u, v, cost;
};
bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }
//////////////////////////////////////////////////////
// Krascal    /////////////////////////////////////////
//////////////////////////////////////////////////////
int V, E;
edge es[MAX_M];

int krascal() {
  sort(es, es + E, comp);
  init(V);
  int res = 0;

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

vector<pair<ll, pair<ll, ll>>> x_y;
vector<pair<ll, pair<ll, ll>>> y_x;
int main() {
  ll n;
  cin >> n;
  V = n;
  E = 2 * (n - 1);

  rep(i, 0, n) {
    ll X, Y;
    cin >> X >> Y;
    x_y.push_back(make_pair(X, make_pair(Y, i)));
    y_x.push_back(make_pair(Y, make_pair(X, i)));
  }

  sort(x_y.begin(), x_y.end());
  sort(y_x.begin(), y_x.end());

  rep(i, 0, n - 1) {
    edge *e = new edge();
    e->u = x_y[i].second.second;
    e->v = x_y[i + 1].second.second;
    e->cost = min(abs(x_y[i].first - x_y[i + 1].first),
                  abs(x_y[i].second.first - x_y[i + 1].second.first));
    es[i] = *e;
  }

  rep(i, 0, n - 1) {
    edge *e = new edge();
    e->u = y_x[i].second.second;
    e->v = y_x[i + 1].second.second;
    e->cost = min(abs(y_x[i].first - y_x[i + 1].first),
                  abs(y_x[i].second.first - y_x[i + 1].second.first));
    es[n - 1 + i] = *e;
  }
  cout << krascal() << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
typedef long long ll;
static const ll MOD = 1e9 + 7;
static const ll INF = (ll)1 << 62;

//////////////////////////////////////////////////////
// Union Find /////////////////////////////////////////
//////////////////////////////////////////////////////
static const ll MAX_M = 100001;
static const ll MAX_N = 100001;
int par[MAX_N];
int rak[MAX_N];

void init(int n) {
  rep(i, 0, n) {
    par[i] = i;
    rak[i] = 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 (rak[x] < rak[y])
    par[x] = y;
  else {
    par[x] = y;
    if (rak[x] == rak[y])
      rak[x]++;
  }
}

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

struct edge {
  ll u, v, cost;
};
bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }
//////////////////////////////////////////////////////
// Krascal    /////////////////////////////////////////
//////////////////////////////////////////////////////
int V, E;
edge es[MAX_M * 2];

int krascal() {
  sort(es, es + E, comp);
  init(V);
  int res = 0;

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

vector<pair<ll, pair<ll, ll>>> x_y;
vector<pair<ll, pair<ll, ll>>> y_x;
int main() {
  ll n;
  cin >> n;
  V = n;
  E = 2 * (n - 1);

  rep(i, 0, n) {
    ll X, Y;
    cin >> X >> Y;
    x_y.push_back(make_pair(X, make_pair(Y, i)));
    y_x.push_back(make_pair(Y, make_pair(X, i)));
  }

  sort(x_y.begin(), x_y.end());
  sort(y_x.begin(), y_x.end());

  rep(i, 0, n - 1) {
    edge *e = new edge();
    e->u = x_y[i].second.second;
    e->v = x_y[i + 1].second.second;
    e->cost = min(abs(x_y[i].first - x_y[i + 1].first),
                  abs(x_y[i].second.first - x_y[i + 1].second.first));
    es[i] = *e;
  }

  rep(i, 0, n - 1) {
    edge *e = new edge();
    e->u = y_x[i].second.second;
    e->v = y_x[i + 1].second.second;
    e->cost = min(abs(y_x[i].first - y_x[i + 1].first),
                  abs(y_x[i].second.first - y_x[i + 1].second.first));
    es[n - 1 + i] = *e;
  }
  cout << krascal() << endl;

  return 0;
}