Problem p03682 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define mst(a, b) memset((a), (b), sizeof(a))
#define rush()                                                                 \
  int T;                                                                       \
  scanf("%d", &T);                                                             \
  while (T--)

typedef long long ll;
const int maxn = 100005;
const int mod = 20090717;
const int INF = 0x3f3f3f;
const double eps = 1e-9;

int n, cnt;
int pre[maxn];
int head[maxn];
int Rank[maxn];

struct point {
  int x, y, id;
} a[maxn];

struct node {
  int u, v, next, val;
} e[maxn];

void add(int u, int v, int val) {
  e[cnt].u = u;
  e[cnt].v = v;
  e[cnt].val = val;
  e[cnt].next = head[u];
  head[u] = cnt++;
}

bool cmpx(const point &a, const point &b) { return a.x < b.x; }

bool cmpy(const point &a, const point &b) { return a.y < b.y; }

bool cmp_val(const node &a, const node &b) { return a.val < b.val; }

int find(int x) {
  int t, r = x;
  while (x != pre[x]) {
    x = pre[x];
  }
  while (r != x) {
    t = pre[r];
    pre[r] = x;
    r = t;
  }
  return x;
}

void join(int a, int b) {
  int A = find(a);
  int B = find(b);
  if (A == B)
    return;
  if (Rank[A] < Rank[B]) {
    pre[A] = B;
  } else {
    pre[B] = A;
    if (Rank[A] == Rank[B])
      Rank[A]++;
  }
}

int main() {
  int x, y;
  while (~scanf("%d", &n)) {
    cnt = 0;
    mst(head, -1);
    mst(Rank, 0);
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &x, &y);
      pre[i] = i;
      a[i].x = x;
      a[i].y = y;
      a[i].id = i;
    }
    sort(a + 1, a + 1 + n, cmpx);
    for (int i = 1; i < n; i++) {
      add(a[i].id, a[i + 1].id, a[i + 1].x - a[i].x);
    }
    sort(a + 1, a + 1 + n, cmpy);
    for (int i = 1; i < n; i++) {
      add(a[i].id, a[i + 1].id, a[i + 1].y - a[i].y);
    }
    sort(e, e + cnt, cmp_val);
    ll ans = 0;
    int num = 0;
    for (int i = 0; i < cnt; i++) {
      if (find(e[i].u) != find(e[i].v)) {
        join(e[i].u, e[i].v);
        num++;
        ans += e[i].val;
      }
      if (num == n - 1)
        break;
    }
    printf("%I64d\n", ans);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define mst(a, b) memset((a), (b), sizeof(a))
#define rush()                                                                 \
  int T;                                                                       \
  scanf("%d", &T);                                                             \
  while (T--)

typedef long long ll;
const int maxn = 100005;
const int mod = 20090717;
const int INF = 0x3f3f3f;
const double eps = 1e-9;

int n, cnt;
int pre[maxn];
int head[maxn];
int Rank[maxn];

struct point {
  int x, y, id;
} a[maxn];

struct node {
  int u, v, next, val;
} e[maxn * 2];

void add(int u, int v, int val) {
  e[cnt].u = u;
  e[cnt].v = v;
  e[cnt].val = val;
  e[cnt].next = head[u];
  head[u] = cnt++;
}

bool cmpx(const point &a, const point &b) { return a.x < b.x; }

bool cmpy(const point &a, const point &b) { return a.y < b.y; }

bool cmp_val(const node &a, const node &b) { return a.val < b.val; }

int find(int x) {
  int t, r = x;
  while (x != pre[x]) {
    x = pre[x];
  }
  while (r != x) {
    t = pre[r];
    pre[r] = x;
    r = t;
  }
  return x;
}

void join(int a, int b) {
  int A = find(a);
  int B = find(b);
  if (A == B)
    return;
  if (Rank[A] < Rank[B]) {
    pre[A] = B;
  } else {
    pre[B] = A;
    if (Rank[A] == Rank[B])
      Rank[A]++;
  }
}

int main() {
  int x, y;
  while (~scanf("%d", &n)) {
    cnt = 0;
    mst(head, -1);
    mst(Rank, 0);
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &x, &y);
      pre[i] = i;
      a[i].x = x;
      a[i].y = y;
      a[i].id = i;
    }
    sort(a + 1, a + 1 + n, cmpx);
    for (int i = 1; i < n; i++) {
      add(a[i].id, a[i + 1].id, a[i + 1].x - a[i].x);
    }
    sort(a + 1, a + 1 + n, cmpy);
    for (int i = 1; i < n; i++) {
      add(a[i].id, a[i + 1].id, a[i + 1].y - a[i].y);
    }
    sort(e, e + cnt, cmp_val);
    ll ans = 0;
    int num = 0;
    for (int i = 0; i < cnt; i++) {
      if (find(e[i].u) != find(e[i].v)) {
        join(e[i].u, e[i].v);
        num++;
        ans += e[i].val;
      }
      if (num == n - 1)
        break;
    }
    printf("%I64d\n", ans);
  }
  return 0;
}