Language: C++

//おまじない
#include <iostream>
#include<iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#define INF 1e9+7
#define rep(i,n) for(int i=0;i<n;i++)
#define NO cout<<"NO"<<endl;
#define YES cout << "YES"<<endl;
#define No cout << "No"<<endl;
#define Yes cout << "Yes"<<endl;
#define all(a) a.begin(),a.end()
#define P pair<int,int>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
//(int)'a'は97 (int)'A'は65 (int)'1'は49
//おまじない

const int maxn=100000;

//UnionFind
class UnionFind {
public:
    //親の番号を格納する。親だった場合は-(その集合のサイズ)
    vector<int> Parent;
    
    UnionFind(int N) {
        Parent = vector<int>(N, -1);
    }
    
    //Aがどのグループに属しているか調べる
    int root(int A) {
        if (Parent[A] < 0) return A;
        return Parent[A] = root(Parent[A]);
    }
    
    //自分のいるグループの頂点数を調べる
    int size(int A) {
        return -Parent[root(A)];
    }
    
    //AとBをくっ付ける
    bool connect(int A, int B) {
        
        A = root(A);
        B = root(B);
        if (A == B) {
            
            return false;
        }
        if (size(A) < size(B)) swap(A, B);
        
        Parent[A] += Parent[B];
        Parent[B] = A;
        
        return true;
    }
};

vector<ll,pair<ll,int>> edgh;
pair<ll,int> x[maxn];
pair<ll,int> y[maxn];
ll ans = 0;
int n;

int main(){
    
    cin >> n;
    rep(i,n){
        ll px,py;
        cin >> px >> py;
        x[i]=make_pair(px,i);
        y[i]=make_pair(py,i);
    }
    
    sort(x,x+n);
    sort(y,y+n);
    
    rep(i,n-1){
        ll cost = x[i+1].first-x[i].first;
        edgh.push_back(make_pair(cost,make_pair(x[i].second,x[i+1].second)));
    }
    
    rep(i,n-1){
        ll cost = y[i+1].first-y[i].first;
        edgh.push_back(make_pair(cost,make_pair(y[i].second,y[i+1].second)));
    }
    
    UnionFind Uni(n);
    
    sort(all(edgh));
    
    int cnt = 0;
    int idx = 0;
    while(cnt<n-1){
        if(Uni.root(edgh[idx].second.first)!=Uni.root(edgh[idx].second.second)){
            Uni.connect(edgh[idx].second.first,edgh[idx].second.second);
            ans += edgh.first;
        }
        idx++;
    }
    
    cout << ans<<endl;
    
    return 0;
}


Compiler output:
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'struct std::_Vector_base<long long int, std::pair<long long int, int> >':
/usr/include/c++/14/bits/stl_vector.h:428:11:   required from 'class std::vector<long long int, std::pair<long long int, int> >'
  428 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~
a.cc:64:25:   required from here
   64 | vector<ll,pair<ll,int>> edgh;
      |                         ^~~~
/usr/include/c++/14/bits/stl_vector.h:87:28: error: no type named 'value_type' in 'struct std::pair<long long int, int>'
   87 |         rebind<_Tp>::other _Tp_alloc_type;
      |                            ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:89:9: error: no type named 'value_type' in 'struct std::pair<long long int, int>'
   89 |         pointer;
      |         ^~~~~~~
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'class std::vector<long long int, std::pair<long long int, int> >':
a.cc:64:25:   required from here
   64 | vector<ll,pair<ll,int>> edgh;
      |                         ^~~~
/usr/include/c++/14/bits/stl_vector.h:518:20: error: '_M_allocate' has not been declared in 'std::vector<long long int, std::pair<long long int, int> >::_Base'
  518 |       using _Base::_M_allocate;
      |                    ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:519:20: error: '_M_deallocate' has not been declared in 'std::vector<long long int, std::pair<long long int, int> >::_Base'
  519 |       using _Base::_M_deallocate;
      |                    ^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:521:20: error: '_M_get_Tp_allocator' has not been declared in 'std::vector<long long int, std::pair<long long int, int> >::_Base'
  521 |       using _Base::_M_get_Tp_allocator;
      |                    ^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/requires_hosted.h:31,
                 from /usr/include/c++/14/iostream:38,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = long long int; _Alloc = std::pair<long long int, int>]':
a.cc:64:25:   required from here
   64 | vector<ll,pair<ll,int>> edgh;
      |                         ^~~~
/usr/include/c++/14/bits/stl_vector.h:136:24: error: no type named 'value_type' in 'struct std::pair<long long int, int>'
  136 |         _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
      |                        ^~~~~~~~~~~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:85:23: error: no matching function for call to 'std::vector<long long int, std::pair<long long int, int> >::push_back(std::pair<long long int, std::pair<int, int> >)'
   85 |         edgh.push_back(make_pair(cost,make_pair(x[i].second,x[i+1].second)));
      |         ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = long long int; _Alloc = std::pair<long long int, int>; value_type = long long int]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from 'std::pair<long long int, std::pair<int, int> >' to 'const std::vector<long long int, std::pair<long long int, int> >::value_type&' {aka 'const long long int&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = long long int; _Alloc = std::pair<long long int, int>; value_type = long long int]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from 'std::pair<long long int, std::pair<int, int> >' to 'std::vector<long long int, std::pair<long long int, int> >::value_type&&' {aka 'long long int&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
a.cc:90:23: error: no matching function for call to 'std::vector<long long int, std::pair<long long int, int> >::push_back(std::pair<long long int, std::pair<int, int> >)'
   90 |         edgh.push_back(make_pair(cost,make_pair(y[i].second,y[i+1].second)));
      |         ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = long long int; _Alloc = std::pair<long long int, int>; value_type = long long int]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from 'std::pair<long long int, std::pair<int, int> >' to 'const std::vector<long long int, std::pair<long long int, int> >::value_type&' {aka 'const long long int&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = long long int; _Alloc = std::pair<long long int, int>; value_type = long long int]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from 'std::pair<long long int, std::pair<int, int> >' to 'std::vector<long long int, std::pair<long long int, int> >::value_type&&' {aka 'long long int&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
a.cc:15:18: error: 'class std::vector<long long int, std::pair<long long int, int> >' has no member named 'begin'
   15 | #define all(a) a.begin(),a.end()
      |                  ^~~~~
a.cc:95:10: note: in expansion of macro 'all'
   95 |     sort(all(edgh));
      |          ^~~
a.cc:15:28: error: 'class std::vector<long long int, std::pair<long long int, int> >' has no member named 'end'
   15 | #define all(a) a.begin(),a.end()
      |                            ^~~
a.cc:95:10: note: in expansion of macro 'all'
   95 |     sort(all(edgh));
      |          ^~~
a.cc:100:25: error: no match for 'operator[]' (operand types are 'std::vector<long long int, std::pair<long long int, int> >' and 'int')
  100 |         if(Uni.root(edgh[idx].second.first)!=Uni.root(edgh[idx].second.second)){
      |                         ^
a.cc:100:59: error: no match for 'operator[]' (operand types are 'std::vector<long long int, std::pair<long long int, int> >' and 'int')
  100 |         if(Uni.root(edgh[idx].second.first)!=Uni.root(edgh[idx].second.second)){
      |                                                           ^
a.cc:101:29: error: no match for 'operator[]' (operand types are 'std::vector<long long int, std::pair<long long int, int> >' and 'int')
  101 |             Uni.connect(edgh[idx].second.first,edgh[idx].second.second);
      |                             ^
a.cc:101:52: error: no match for 'operator[]' (operand types are 'std::vector<long long int, std::pair<long long int, int> >' and 'int')
  101 |             Uni.connect(edgh[idx].second.first,edgh[idx].second.second);
      |                                                    ^
a.cc:102:25: error: 'class std::vector<long long int, std::pair<long long int, int> >' has no member named 'first'
  102 |             ans += edgh.first;
      |                         ^~~~~
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = long long int; _Alloc = std::pair<long long int, int>]':
/usr/include/c++/14/bits/stl_vector.h:314:7:   required from here
  314 |       _Vector_base() = default;
      |       ^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:141:26: error: no type named 'value_type' in 'struct std::pair<long long int, int>'
  141 |         : _Tp_alloc_type()
      |                          ^