Language: C++

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <numeric> // accumulate(v.begin(), v.end(), 0)

using namespace std;
#define ll long long

#define MAX_N 100000

int par[MAX_N]; // 親の番号
int rank[MAX_N];

// n要素で初期化
void init(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]); // 経路圧縮
  }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y){
  return root(x) == root(y);
}

// xとyの属する集合を併合
void unite(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]++;
  }
}

struct data{
  int n;
  ll x;
  ll y;
};
struct edge{
  int p;
  int q;
  ll cost;
};

bool compx(data d1, data d2){
  return d1.x <= d2.x;
}
bool compy(data d1, data d2){
  return d1.y <= d2.y;
}
bool compe(edge e1, edge e2){
  return e1.cost <= e2.cost;
}

int main(){
  int N;
  cin >> N;
  vector<data> Dx, Dy;
  for(int i=0; i<N; i++){
    ll x, y;
    cin >> x >> y;
    data dtmp = {i, x, y};
    Dx.push_back(dtmp);
    Dy.push_back(dtmp);
  }
  sort(Dx.begin(), Dx.end(), compx);
  sort(Dy.begin(), Dy.end(), compy);

  vector<edge> E;
  for(int i=0; i<N-1; i++){
    edge etmpx = {Dx[i].n, Dx[i+1].n, Dx[i+1].x - Dx[i].x};
    edge etmpy = {Dx[i].n, Dx[i+1].n, Dy[i+1].y - Dy[i].y};
    E.push_back(etmpx);
    E.push_back(etmpy);
  }
  sort(E.begin(), E.end(), compe);

  init(MAX_N);
  ll ans = 0;
  for(int i=0; i<2*(N-1); i++){
    edge e = E[i];
    if(!same(e.p, e.q)){
      ans += e.cost;
      unite(e.p, e.q);
      cout << e.p << ' ' << e.q << endl;
    }
  }
  cout << ans << endl;
  return 0;
}


Compiler output:
a.cc: In function 'void init(int)':
a.cc:20:5: error: reference to 'rank' is ambiguous
   20 |     rank[i] = 0;
      |     ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:43:6: error: reference to 'rank' is ambiguous
   43 |   if(rank[x] < rank[y]){
      |      ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:43:16: error: reference to 'rank' is ambiguous
   43 |   if(rank[x] < rank[y]){
      |                ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:47:8: error: reference to 'rank' is ambiguous
   47 |     if(rank[x] == rank[y])
      |        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:47:19: error: reference to 'rank' is ambiguous
   47 |     if(rank[x] == rank[y])
      |                   ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:48:7: error: reference to 'rank' is ambiguous
   48 |       rank[x]++;
      |       ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc: At global scope:
a.cc:63:12: error: reference to 'data' is ambiguous
   63 | bool compx(data d1, data d2){
      |            ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:52:8: note:                 'struct data'
   52 | struct data{
      |        ^~~~
a.cc:63:21: error: reference to 'data' is ambiguous
   63 | bool compx(data d1, data d2){
      |                     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:52:8: note:                 'struct data'
   52 | struct data{
      |        ^~~~
a.cc:63:28: error: expression list treated as compound expression in initializer [-fpermissive]
   63 | bool compx(data d1, data d2){
      |                            ^
a.cc:66:12: error: reference to 'data' is ambiguous
   66 | bool compy(data d1, data d2){
      |            ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:52:8: note:                 'struct data'
   52 | struct data{
      |        ^~~~
a.cc:66:21: error: reference to 'data' is ambiguous
   66 | bool compy(data d1, data d2){
      |                     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:52:8: note:                 'struct data'
   52 | struct data{
      |        ^~~~
a.cc:66:28: error: expression list treated as compound expression in initializer [-fpermissive]
   66 | bool compy(data d1, data d2){
      |                            ^
a.cc: In function 'int main()':
a.cc:76:14: error: template argument 1 is invalid
   76 |   vector<data> Dx, Dy;
      |              ^
a.cc:76:14: error: template argument 2 is invalid
a.cc:80:5: error: reference to 'data' is ambiguous
   80 |     data dtmp = {i, x, y};
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:52:8: note:                 'struct data'
   52 | struct data{
      |        ^~~~
a.cc:81:8: error: request for member 'push_back' in 'Dx', which is of non-class type 'int'
   81 |     Dx.push_back(dtmp);
      |        ^~~~~~~~~
a.cc:81:18: error: 'dtmp' was not declared in this scope; did you mean 'tm'?
   81 |     Dx.push_back(dtmp);
      |                  ^~~~
      |                  tm
a.cc:82:8: error: request for member 'push_back' in 'Dy', which is of non-class type 'int'
   82 |     Dy.push_back(dtmp);
      |        ^~~~~~~~~
a.cc:84:11: error: request for member 'begin' in 'Dx', which is of non-class type 'int'
   84 |   sort(Dx.begin(), Dx.end(), compx);
      |           ^~~~~
a.cc:84:23: error: request for member 'end' in 'Dx', which is of non-class type 'int'
   84 |   sort(Dx.begin(), Dx.end(), compx);
      |                       ^~~
a.cc:85:11: error: request for member 'begin' in 'Dy', which is of non-class type 'int'
   85 |   sort(Dy.begin(), Dy.end(), compy);
      |           ^~~~~
a.cc:85:23: error: request for member 'end' in 'Dy', which is of non-class type 'int'
   85 |   sort(Dy.begin(), Dy.end(), compy);
      |                       ^~~
a.cc:89:21: error: invalid types 'int[int]' for array subscript
   89 |     edge etmpx = {Dx[i].n, Dx[i+1].n, Dx[i+1].x - Dx[i].x};
      |