Language: C++

#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

const int MAXN = 100010;

int n;
struct location {
  ll x, y;
  int pos;
};
struct edge {
  int from, to;
  ll cost;
};
location loc[MAXN];

bool xcompare(location a, location b) {
  return a.x < b.x;
}

bool ycompare(location a, location b) {
  return a.y < b.y;
}

bool edgecompare(edge a, edge b) {
  return a.cost < b.cost;
}

int par[MAXN];
int rank_[MAXN];
int size[MAXN];

void init() {
    for (int i=0;i<n;i++) {
      par[i] = i;
      rank_[i] = 0;
      size[i] = 1;
    }
}

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

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

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  }

  if (rank_[x] < rank_[y]) {
    par[x] = y;
    size[y] += size[x];
  } else {
    par[y] = x;
    if (rank_[x] == rank_[y]) {
      rank_[x]++;
    }
    size[x] += size[y];
  }
}


void solve() {
  vector<edge> edges;
  init();
  sort(loc, loc + n, xcompare);
  for (int i=0;i<n-1;i++) {
    edges.push_back((edge){loc[i].pos, loc[i+1].pos, min(abs(loc[i].x - loc[i+1].x), abs(loc[i].y - loc[i+1].y))});
  }
  sort(loc, loc + n, ycompare);
  for (int i=0;i<n-1;i++) {
    edges.push_back((edge){loc[i].pos, loc[i+1].pos, min(abs(loc[i].x - loc[i+1].x), abs(loc[i].y - loc[i+1].y))});
  }

  sort(edges.begin(), edges.end(), edgecompare);

  int remain = n;
  ll cost = 0;
  for (int i=0;i<edges.size();i++) {
    if (!same(edges[i].from, edges[i].to)) {
      cost += edges[i].cost;
      unite(edges[i].from, edges[i].to);
    }
  }
  cout << cost << endl;
}

int main() {
  cin >> n;
  ll x, y;
  for (int i=0;i<n;i++) {
    cin >> x >> y;
    loc[i].x = x;
    loc[i].y = y;
    loc[i].pos = i;
  }
  solve();
}


Compiler output:
a.cc: In function 'void init()':
a.cc:40:7: error: reference to 'size' is ambiguous
   40 |       size[i] = 1;
      |       ^~~~
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,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:34:5: note:                 'int size [100010]'
   34 | int size[MAXN];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:65:5: error: reference to 'size' is ambiguous
   65 |     size[y] += size[x];
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:34:5: note:                 'int size [100010]'
   34 | int size[MAXN];
      |     ^~~~
a.cc:65:16: error: reference to 'size' is ambiguous
   65 |     size[y] += size[x];
      |                ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:34:5: note:                 'int size [100010]'
   34 | int size[MAXN];
      |     ^~~~
a.cc:71:5: error: reference to 'size' is ambiguous
   71 |     size[x] += size[y];
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:34:5: note:                 'int size [100010]'
   34 | int size[MAXN];
      |     ^~~~
a.cc:71:16: error: reference to 'size' is ambiguous
   71 |     size[x] += size[y];
      |                ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:34:5: note:                 'int size [100010]'
   34 | int size[MAXN];
      |     ^~~~
a.cc: In function 'void solve()':
a.cc:79:3: error: 'sort' was not declared in this scope; did you mean 'short'?
   79 |   sort(loc, loc + n, xcompare);
      |   ^~~~
      |   short