Problem p03682 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

// macro
#define rep(i, n) for (i = 0; i < n; i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
#define N 10000
vector<int> par(N);
vector<int> ranks(N);

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

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

double dist(double x1, double x2, double y1, double y2, double z1, double z2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) +
              (z1 - z2) * (z1 - z2));
}

int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> y(n);
  int i;
  rep(i, n) cin >> x[i] >> y[i];
  vector<pair<int, int>> p1(n);
  rep(i, n) p1[i] = make_pair(x[i], i);
  vector<pair<int, int>> p2(n);
  rep(i, n) p2[i] = make_pair(y[i], i);
  sort(all(p1));
  sort(all(p2));
  vector<int> a(n * 2 - 2);
  vector<int> b(n * 2 - 2);
  vector<int> c(n * 2 - 2);
  rep(i, n - 1) {
    a[i * 2] = p1[i].second;
    b[i * 2] = p1[i + 1].second;
    c[i * 2] = p1[i + 1].first - p1[i].first;
    a[i * 2 + 1] = p2[i].second;
    b[i * 2 + 1] = p2[i + 1].second;
    c[i * 2 + 1] = p2[i + 1].first - p2[i].first;
  }
  vector<pair<int, int>> g(n * 2 - 2);
  rep(i, n * 2 - 2) { g[i] = make_pair(c[i], i); }
  sort(all(g));
  init(n);
  int ans = 0;
  rep(i, n * 2 - 2) {
    int nowi = g[i].second;
    if (!same(a[nowi], b[nowi])) {
      unite(a[nowi], b[nowi]);
      ans += g[i].first;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

// macro
#define rep(i, n) for (i = 0; i < n; i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
#define N 1000000
vector<int> par(N);
vector<int> ranks(N);

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

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

double dist(double x1, double x2, double y1, double y2, double z1, double z2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) +
              (z1 - z2) * (z1 - z2));
}

int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> y(n);
  int i;
  rep(i, n) cin >> x[i] >> y[i];
  vector<pair<int, int>> p1(n);
  rep(i, n) p1[i] = make_pair(x[i], i);
  vector<pair<int, int>> p2(n);
  rep(i, n) p2[i] = make_pair(y[i], i);
  sort(all(p1));
  sort(all(p2));
  vector<int> a(n * 2 - 2);
  vector<int> b(n * 2 - 2);
  vector<int> c(n * 2 - 2);
  rep(i, n - 1) {
    a[i * 2] = p1[i].second;
    b[i * 2] = p1[i + 1].second;
    c[i * 2] = p1[i + 1].first - p1[i].first;
    a[i * 2 + 1] = p2[i].second;
    b[i * 2 + 1] = p2[i + 1].second;
    c[i * 2 + 1] = p2[i + 1].first - p2[i].first;
  }
  vector<pair<int, int>> g(n * 2 - 2);
  rep(i, n * 2 - 2) { g[i] = make_pair(c[i], i); }
  sort(all(g));
  init(n);
  int ans = 0;
  rep(i, n * 2 - 2) {
    int nowi = g[i].second;
    if (!same(a[nowi], b[nowi])) {
      unite(a[nowi], b[nowi]);
      ans += g[i].first;
    }
  }
  cout << ans << endl;
}