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:10: error: redeclaration of 'auto i'
   20 |     rank[i]=0;
      |          ^
a.cc:18:11: note: 'int i' previously declared here
   18 |   for(int i=0;i<n;i++){
      |           ^
a.cc:20:13: error: class template argument deduction failed:
   20 |     rank[i]=0;
      |             ^
a.cc:20:13: error: no matching function for call to 'rank(int)'
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: candidate: 'template<class> rank()-> std::rank< <template-parameter-1-1> >'
 1437 |     struct rank
      |            ^~~~
/usr/include/c++/14/type_traits:1437:12: note:   candidate expects 0 arguments, 1 provided
/usr/include/c++/14/type_traits:1437:12: note: candidate: 'template<class> rank(std::rank< <template-parameter-1-1> >)-> std::rank< <template-parameter-1-1> >'
/usr/include/c++/14/type_traits:1437:12: note:   template argument deduction/substitution failed:
a.cc:20:13: note:   mismatched types 'std::rank< <template-parameter-1-1> >' and 'int'
   20 |     rank[i]=0;
      |             ^
a.cc: In function 'void unite(int, int)':
a.cc:32:10: error: expected unqualified-id before '[' token
   32 |   if(rank[x]<rank[y]){
      |          ^
a.cc:37:12: error: expected unqualified-id before '[' token
   37 |     if(rank[x]==rank[y])rank[x]++;
      |            ^
a.cc:37:32: error: expected initializer before '++' token
   37 |     if(rank[x]==rank[y])rank[x]++;
      |                                ^~
a.cc:37:32: error: expected ';' before '++' token