Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
struct edge {int u,v,cost;};
int par[100001],rank[100001];
edge es[200002];
int N;
bool comp(const edge& e1,const edge& e2){
  return e1.cost<e2.cost;
}

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

int find(int x){
  if(par[x]==x)return x;
  else return x=find(par[x]);
}
void unite(int x,int y){
  x=find(x);
  y=find(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 find(x) == find(y);
}
int main(){
  cin >> N;
  vector<pair<int ,int> > p(N),p2(N);
  for(int i=0;i<N;i++){
    int x,y;
    cin >> x >> y;
    p[i]=make_pair(x,i);
    p2[i]=make_pair(y,i);
  }
  sort(p.begin(),p.end());
  sort(p2.begin(),p2.end());

  for(int i=0;i<N-1;i++){
    es[i].u=p[i].second;
    es[i].v=p[i+1].second;
    es[i].cost=p[i+1].first-p[i].first;
   
  }

   for(int i=0;i<N-1;i++){
    es[N-1+i].u=p2[i].second;
    es[N-1+i].v=p2[i+1].second;
    es[N-1+i].cost=p2[i+1].first-p2[i].first;
  
  }
   sort(es,es+2*(N-1),comp);
   //for(int i=0;i<2*(N-1);i++)  printf("%d %d %d\n",es[i].u,es[i].v,es[i].cost);
   init_union_find(N);
   ll res=0;
   for(int i=0;i<2*(N-1);i++){
     edge e=es[i];
     if(!same(e.u,e.v)){
       unite(e.u,e.v);
       res+=e.cost;
     }
   }
   cout << res << endl;
  return 0; 
}


Compiler output:
a.cc: In function 'void init_union_find(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:10:17: note:                 'int rank [100001]'
   10 | int par[100001],rank[100001];
      |                 ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:32:6: error: reference to 'rank' is ambiguous
   32 |   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:10:17: note:                 'int rank [100001]'
   10 | int par[100001],rank[100001];
      |                 ^~~~
a.cc:32:14: error: reference to 'rank' is ambiguous
   32 |   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:10:17: note:                 'int rank [100001]'
   10 | int par[100001],rank[100001];
      |                 ^~~~
a.cc:37:8: error: reference to 'rank' is ambiguous
   37 |     if(rank[x]==rank[y])rank[x]++;
      |        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:17: note:                 'int rank [100001]'
   10 | int par[100001],rank[100001];
      |                 ^~~~
a.cc:37:17: error: reference to 'rank' is ambiguous
   37 |     if(rank[x]==rank[y])rank[x]++;
      |                 ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:17: note:                 'int rank [100001]'
   10 | int par[100001],rank[100001];
      |                 ^~~~
a.cc:37:25: error: reference to 'rank' is ambiguous
   37 |     if(rank[x]==rank[y])rank[x]++;
      |                         ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:17: note:                 'int rank [100001]'
   10 | int par[100001],rank[100001];
      |                 ^~~~