Language: C++

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
#define nu 1000005
#define maxnum 100005
#define num 30
int n;
struct Md
{
    int v,u,flag;

}M[maxnum],C[maxnum];
struct Node
{
    int x,y,z;

}dis[nu];
bool cmp(Md a,Md b){
   return a.v<b.v;
}
bool cmp0(Md a,Md b){
   return a.u<b.u;
}
bool cmp1(Node a,Node b){
   return a.z<b.z;
}
int book[maxnum];
void init()
{
    for(int i=1;i<=n;i++)
        book[i]=i;

}
int getx(int x)
{
    if(book[x]!=x)
        book[x]=getx(book[x]);
    return book[x];
}
void mergexy(int x,int y)
{
     book[y]=x;
}
void change(){
    for(int i=1;i<=n;i++)
        {
            C[i].v=M[i].v;
            C[i].u=M[i].u;
            C[i].flag=M[i].flag;
        }
}
int main()
{
    scanf("%d",&n);

    for(int i=1;i<=n;i++){
         scanf("%d%d",&M[i].v,&M[i].u);
         M[i].flag=i;
    }
    int t=unique(M+1,M+n)-M;
    /*for(int i=1;i<=t;i++)
        cout<<M[i].v<<" "<<M[i].u<<endl;*/
      n=t;
      change();
      int fl=0;
      sort(C+1,C+n+1,cmp);
      for(int i=2;i<n;i++)
      {
          dis[++fl].x=C[i].flag,dis[fl].y=C[i-1].flag,dis[fl].z=(min(fabs(C[i].v-C[i-1].v),fabs(C[i].u-C[i-1].u)));
      }
      for
      change();
      sort(C+1,C+n+1,cmp0);
      for(int i=2;i<n;i++)
      {
          dis[++fl].x=C[i].flag,dis[fl].y=C[i-1].flag,dis[fl].z=(min(fabs(C[i].v-C[i-1].v),fabs(C[i].u-C[i-1].u)));
      }
    sort(dis+1,dis+flag+1,cmp1);
   /*  cout<<flag<<endl;
    for(int i=1;i<=flag;i++)
        cout<<dis[i].z<<endl;*/
        init();
         int number=0;
        int sum=0;
       for(int i=1;i<=flag;i++)
        {
            int p=getx(dis[i].x),q=getx(dis[i].y);
            if(p!=q){
                mergexy(p,q);
                number++;
                sum+=dis[i].z;
        }
        if(number==n)  break;
    }
   cout<<sum<<endl;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:77:7: error: expected '(' before 'change'
   77 |       change();
      |       ^~~~~~
      |       (
a.cc:78:11: error: could not convert 'std::sort<Md*, bool (*)(Md, Md)>((((Md*)(& C)) + 12), (((Md*)(& C)) + ((((sizetype)n) + 1) * 12)), cmp0)' from 'void' to 'bool'
   78 |       sort(C+1,C+n+1,cmp0);
      |       ~~~~^~~~~~~~~~~~~~~~
      |           |
      |           void
a.cc:79:7: error: expected primary-expression before 'for'
   79 |       for(int i=2;i<n;i++)
      |       ^~~
a.cc:78:28: error: expected ')' before 'for'
   78 |       sort(C+1,C+n+1,cmp0);
      |                            ^
      |                            )
   79 |       for(int i=2;i<n;i++)
      |       ~~~                   
a.cc:77:7: note: to match this '('
   77 |       change();
      |       ^~~~~~
a.cc:83:20: error: 'flag' was not declared in this scope; did you mean 'fl'?
   83 |     sort(dis+1,dis+flag+1,cmp1);
      |                    ^~~~
      |                    fl
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/string:51,
                 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/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_equal_to_iter::operator()(_Iterator1, _Iterator2) const [with _Iterator1 = Md*; _Iterator2 = Md*]':
/usr/include/c++/14/bits/stl_algo.h:869:20:   required from '_ForwardIterator std::__unique(_ForwardIterator, _ForwardIterator, _BinaryPredicate) [with _ForwardIterator = Md*; _BinaryPredicate = __gnu_cxx::__ops::_Iter_equal_to_iter]'
  869 |         if (!__binary_pred(__dest, __first))
      |              ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:900:27:   required from '_FIter std::unique(_FIter, _FIter) [with _FIter = Md*]'
  900 |       return std::__unique(__first, __last,
      |              ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
  901 |                            __gnu_cxx::__ops::__iter_equal_to_iter());
      |                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:65:17:   required from here
   65 |     int t=unique(M+1,M+n)-M;
      |           ~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:117:23: error: no match for 'operator==' (operand types are 'Md' and 'Md')
  117 |       { return *__it1 == *__it2; }
      |                ~~~~~~~^~~~~~~~~
In file included from /usr/include/c++/14/string:48:
/usr/include/c++/14/bits/stl_iterator.h:1208:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator==(const __normal_iterator<_IteratorL, _Container>&, const __normal_iterator<_IteratorR, _Container>&)'
 1208 |     operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1208:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:117:23: note:   'Md' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
  117 |       { return *__it1 == *__it2; }
      |                ~~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1216:5: note: candidate: 'template<class _Iterator, class _Container> bool __gnu_cxx::operator==(const __normal_iterator<_Iterator, _Container>&, const __normal_iterator<_Iterator, _Container>&)'
 1216 |     operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1216:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:117:23: note:   'Md' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
  117 |       { return *__it1 == *__it2; }
      |                ~~~~~~~^~~~~~~~~