Problem p03682 (C++)

Buggy submission (status: Runtime Error):
// q067.cpp
// Mon Aug 31 23:49:05 2020

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
typedef pair<ll, ll> P;

// Union-Find木
int par
    [100010]; // 親。1個上の親でしか無いことに注意。グループの根はfindで呼び出す。
int treerank[100010]; // 木の深さ

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

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

int main() {
  ll n;
  cin >> n;

  vector<P> node(n);
  vector<P> x;
  vector<P> y;
  rep(i, n) {
    cin >> node[i].first >> node[i].second;
    x.push_back(P(node[i].first, i));
    y.push_back(P(node[i].second, i));
  }

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

  vector<P> cost;
  vector<P> road;
  int c = 0;
  rep(i, n - 1) {
    road.push_back(P(x[i].second, x[i + 1].second));
    cost.push_back(
        P(min(abs(x[i].first - x[i + 1].first),
              abs(node[x[i].second].second - node[x[i + 1].second].second)),
          c));
    c++;
    road.push_back(P(y[i].second, y[i + 1].second));
    cost.push_back(
        P(min(abs(y[i].first - y[i + 1].first),
              abs(node[y[i].second].first - node[y[i + 1].second].first)),
          c));
    c++;
  }

  init(c);
  sort(cost.begin(), cost.end());

  ll ans = 0;
  rep(i, c) {
    ll id = cost[i].second;
    if (!same(road[id].first, road[id].second)) {
      ans += cost[i].first;
      unite(road[id].first, road[id].second);
    }
  }

  cout << ans << endl;
  //	printf("%.4f\n",ans);
}

Fixed submission:
// q067.cpp
// Mon Aug 31 23:49:05 2020

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
typedef pair<ll, ll> P;

// Union-Find木
int par
    [1000100]; // 親。1個上の親でしか無いことに注意。グループの根はfindで呼び出す。
int treerank[1000100]; // 木の深さ

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

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

int main() {
  ll n;
  cin >> n;

  vector<P> node(n);
  vector<P> x;
  vector<P> y;
  rep(i, n) {
    cin >> node[i].first >> node[i].second;
    x.push_back(P(node[i].first, i));
    y.push_back(P(node[i].second, i));
  }

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

  vector<P> cost;
  vector<P> road;
  int c = 0;
  rep(i, n - 1) {
    road.push_back(P(x[i].second, x[i + 1].second));
    cost.push_back(
        P(min(abs(x[i].first - x[i + 1].first),
              abs(node[x[i].second].second - node[x[i + 1].second].second)),
          c));
    c++;
    road.push_back(P(y[i].second, y[i + 1].second));
    cost.push_back(
        P(min(abs(y[i].first - y[i + 1].first),
              abs(node[y[i].second].first - node[y[i + 1].second].first)),
          c));
    c++;
  }

  init(c);
  sort(cost.begin(), cost.end());

  ll ans = 0;
  rep(i, c) {
    ll id = cost[i].second;
    if (!same(road[id].first, road[id].second)) {
      ans += cost[i].first;
      unite(road[id].first, road[id].second);
    }
  }

  cout << ans << endl;
  //	printf("%.4f\n",ans);
}