Problem p03682 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1000000000000000000L;

struct edge {
  int from, to, cost;
};

struct UniTree {
  int *par, *rank;

  UniTree(int n) {
    par = new int[n];
    rank = new int[n];
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }

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

  void merge(int x, int y) {
    x = root(x);
    y = root(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 root(x) == root(y); }
};

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

edge g[100000];

int main() {
  int n;
  cin >> n;
  P x[100000];
  P y[100000];
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    x[i] = P(a, i);
    y[i] = P(b, i);
  }
  sort(x, x + n);
  sort(y, y + n);
  rep(i, n - 1) {
    int a = x[i].second;
    int b = x[i + 1].second;
    int c = x[i].first;
    int d = x[i + 1].first;
    g[i * 2] = {a, b, abs(d - c)};
    g[i * 2 + 1] = {b, a, abs(d - c)};
  }
  rep(i, n - 1) {
    int a = y[i].second;
    int b = y[i + 1].second;
    int c = y[i].first;
    int d = y[i + 1].first;
    g[i * 2 + n * 2 - 2] = {a, b, abs(d - c)};
    g[i * 2 + n * 2 - 1] = {b, a, abs(d - c)};
  }
  sort(g, g + (n * 4 - 4), comp);

  UniTree uni(n);
  ll ans = 0;
  rep(i, n * 4 - 4) {
    if (!uni.same(g[i].from, g[i].to)) {
      uni.merge(g[i].from, g[i].to);
      ans += g[i].cost;
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1000000000000000000L;

struct edge {
  int from, to, cost;
};

struct UniTree {
  int *par, *rank;

  UniTree(int n) {
    par = new int[n];
    rank = new int[n];
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }

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

  void merge(int x, int y) {
    x = root(x);
    y = root(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 root(x) == root(y); }
};

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

edge g[400000];

int main() {
  int n;
  cin >> n;
  P x[100000];
  P y[100000];
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    x[i] = P(a, i);
    y[i] = P(b, i);
  }
  sort(x, x + n);
  sort(y, y + n);
  rep(i, n - 1) {
    int a = x[i].second;
    int b = x[i + 1].second;
    int c = x[i].first;
    int d = x[i + 1].first;
    g[i * 2] = {a, b, abs(d - c)};
    g[i * 2 + 1] = {b, a, abs(d - c)};
  }
  rep(i, n - 1) {
    int a = y[i].second;
    int b = y[i + 1].second;
    int c = y[i].first;
    int d = y[i + 1].first;
    g[i * 2 + n * 2 - 2] = {a, b, abs(d - c)};
    g[i * 2 + n * 2 - 1] = {b, a, abs(d - c)};
  }
  sort(g, g + (n * 4 - 4), comp);

  UniTree uni(n);
  ll ans = 0;
  rep(i, n * 4 - 4) {
    if (!uni.same(g[i].from, g[i].to)) {
      uni.merge(g[i].from, g[i].to);
      ans += g[i].cost;
    }
  }

  cout << ans << endl;

  return 0;
}