Language: C++

/**
 *    author:  FromDihPout
 *    created: 2020-08-01
**/

#include <bits/stdc++.h>
using namespace std;


int find(int node, vector<int> &link) {
    while (link[node] != node) {
        node = link[node];
    }
    return node;
}

bool same(int node1, int node2, vector<int> &link) {
    return find(node1, link) == find(node2, link);
}

void unite(int node1, int node2, vector<int> &link, vector<int> &len) {
    node1 = find(node1, link);
    node2 = find(node2, link);
    if (len[node1] < len[node2]) {
        swap(node1, node2);
    }
    len[node1] += len[node2];
    link[node2] = node1;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    vector<pair<int,int>> x(n), y(n);
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        x[i] = {a, i};
        y[i] = {b, i};
    }
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    
    vector<pair<int, pair<int,int>>> edges(2 * (n - 1));
    for (int i = 0; i < n - 1; i++) {
        edges[i] = {x[i+1].f - x[i].f, {x[i].s, x[i+1].s}};
    }
    for (int i = 0; i < n - 1; i++) {
        edges[i + n - 1] = {y[i+1].f - y[i].f, {y[i].s, y[i+1].s}};
    }
    sort(edges.begin(), edges.end());
    
    long long ans = 0;
    vector<int> link(n), len(n);
    for (int i = 0; i < n; i++) {
        link[i] = i;
    }
    for (int i = 0; i < edges.size(); i++) {
        if (!same(edges[i].s.f, edges[i].s.s, link)) {
            unite(edges[i].s.f, edges[i].s.s, link, len);
            ans += edges[i].f;
        }
    }
    
    cout << ans << '\n';
    
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:49:28: error: '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'struct std::pair<int, int>'} has no member named 'f'
   49 |         edges[i] = {x[i+1].f - x[i].f, {x[i].s, x[i+1].s}};
      |                            ^
a.cc:49:37: error: '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'struct std::pair<int, int>'} has no member named 'f'
   49 |         edges[i] = {x[i+1].f - x[i].f, {x[i].s, x[i+1].s}};
      |                                     ^
a.cc:49:46: error: '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'struct std::pair<int, int>'} has no member named 's'
   49 |         edges[i] = {x[i+1].f - x[i].f, {x[i].s, x[i+1].s}};
      |                                              ^
a.cc:49:56: error: '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'struct std::pair<int, int>'} has no member named 's'
   49 |         edges[i] = {x[i+1].f - x[i].f, {x[i].s, x[i+1].s}};
      |                                                        ^
a.cc:49:58: error: no match for 'operator=' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, std::pair<int, int> > >, std::pair<int, std::pair<int, int> > >::value_type' {aka 'std::pair<int, std::pair<int, int> >'} and '<brace-enclosed initializer list>')
   49 |         edges[i] = {x[i+1].f - x[i].f, {x[i].s, x[i+1].s}};
      |                                                          ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:6:
/usr/include/c++/14/bits/stl_pair.h:946:9: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, const _U1&>, std::is_assignable<_T2&, const _U2&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(const std::pair<_U1, _U2>&) [with _U2 = _U1; _T1 = int; _T2 = std::pair<int, int>]'
  946 |         operator=(const pair<_U1, _U2>& __p)
      |         ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:946:9: note:   template argument deduction/substitution failed:
a.cc:49:58: note:   couldn't deduce template parameter '_U1'
   49 |         edges[i] = {x[i+1].f - x[i].f, {x[i].s, x[i+1].s}};
      |                                                          ^
/usr/include/c++/14/bits/stl_pair.h:957:9: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, _U1&&>, std::is_assignable<_T2&, _U2&&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U2 = _U1; _T1 = int; _T2 = std::pair<int, int>]'
  957 |         operator=(pair<_U1, _U2>&& __p)
      |         ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:957:9: note:   template argument deduction/substitution failed:
a.cc:49:58: note:   couldn't deduce template parameter '_U1'
   49 |         edges[i] = {x[i+1].f - x[i].f, {x[i].s, x[i+1].s}};
      |                                                          ^
/usr/include/c++/14/bits/stl_pair.h:921:7: note: candidate: 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::__conditional_t<((bool)std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value), const std::pair<_T1, _T2>&, const std::__nonesuch&>) [with _T1 = int; _T2 = std::pair<int, int>; std::__conditional_t<((bool)std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value), const std::pair<_T1, _T2>&, const std::__nonesuch&> = const std::pair<int, std::pair<int, int> >&]'
  921 |       operator=(__conditional_t<__and_<is_copy_assignable<_T1>,
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:923:65: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::__conditional_t<true, const std::pair<int, std::pair<int, int> >&, const std::__nonesuch&>' {aka 'const std::pair<int, std::pair<int, int> >&'}
  921 |       operator=(__conditional_t<__and_<is_copy_assignable<_T1>,
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  922 |                                        is_copy_assignable<_T2>>::value,
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  923 |                                 const pair&, const __nonesuch&> __p)
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_pair.h:931:7: note: candidate: 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::__conditional_t<((bool)std::__and_<std::is_move_assignable<_Tp>, std::is_move_assignable<_T2> >::value), std::pair<_T1, _T2>&&, std::__nonesuch&&>) [with _T1 = int; _T2 = std::pair<int, int>; std::__conditional_t<((bool)std::__and_<std::is_move_assignable<_Tp>, std::is_move_assignable<_T2> >::value), std::pair<_T1, _T2>&&, std::__nonesuch&&> = std::pair<int, std::pair<int, int> >&&]'
  931 |       operator=(__conditional_t<__and_<is_move_assignable<_T1>,
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:933:55: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::__conditional_t<true, std::pair<int, std::pair<int, int> >&&, std::__nonesuch&&>' {aka 'std::pair<int, std::pair<int, int> >&&'}
  931 |       operator=(__conditional_t<__and_<is_move_assignable<_T1>,
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  932 |                                        is_move_assignable<_T2>>::value,
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  933 |                                 pair&&, __nonesuch&&> __p)
      |                                 ~~~~~~~~~~~~~~~~~~~~~~^~~
a.cc:52:36: error: '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'struct std::pair<int, int>'} has no member named 'f'
   52 |         edges[i + n - 1] = {y[i+1].f - y[i].f, {y[i].s, y[i+1].s}};
      |                                    ^
a.cc:52:45: error: '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'struct std::pair<int, int>'} has no member named 'f'
   52 |         edges[i + n - 1] = {y[i+1].f - y[i].f, {y[i].s, y[i+1].s}};
      |                                             ^
a.cc:52:54: error: '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'struct std::pair<int, int>'} has no member named 's'
   52 |         edges[i + n - 1] = {y[i+1].f - y[i].f, {y[i].s, y[i+1].s}};
      |                                                      ^
a.cc:52:64: error: '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'struct std::pair<int, int>'} has no member named 's'
   52 |         edges[i + n - 1] = {y[i+1].f - y[i].f, {y[i].s, y[i+1].s}};
      |                                                                ^
a.cc:52:66: error: no match for 'operator=' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, std::pair<int, int> > >, std::pair<int, std::pair<int, int> > >::value_type' {aka 'std::pair<int, std::pair<int, int> >'} and '<brace-enclosed initializer list>')
   52 |         edges[i + n - 1] = {y[i+1].f - y[i].f, {y[i].s, y[i+1].s}};
      |                                                                  ^
/usr/include/c++/14/bits/stl_pair.h:946:9: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, const _U1&>, std::is_assignable<_T2&, const _U2&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(const std::pair<_U1, _U2>&) [with _U2 = _U1; _T1 = int; _T2 = std::pair<int, int>]'
  946 |         operator=(const pair<_U1, _U2>& __p)
      |         ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:946:9: note:   template argument deduction/substitution failed:
a.cc:52:66: note:   couldn't deduce template parameter '_U1'
   52 |         edges[i + n - 1] = {y[i+1].f - y[i].f, {y[i].s, y[i+1].s}};
      |                                                                  ^
/usr/include/c++/14/bits/stl_pair.h:957:9: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, _U1&&>, std::is_assignable<_T2&, _U2&&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U2 = _U1; _T1 = int; _T2 = std::pair<int, int>]'
  957 |         operator=(pair<_U1, _U2>&& __p)
      |         ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:957:9: note:   template argument deduction/substitution failed:
a.cc:52:66: note:   couldn't deduce template parameter '_U1'
   52 |         edges[i + n - 1] = {y[i+1].f - y[i].f, {y[i].s, y[i+1].s}};
      |                                                                  ^
/usr/include/c++/14/bits/stl_pair.h:921:7: note: candidate: 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::__conditional_t<((bool)std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value), const std::pair<_T1, _T2>&, const std::__nonesuch&>) [with _T1 = int; _T2 = std::pair<int, int>; std::__conditional_t<((bool)std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value), const std::pair<_T1, _T2>&, const std::__nonesuch&> = const std::pair<int, std::pair<int, int> >&]'
  921 |       operator=(__conditional_t<__and_<is_copy_assignable<_T1>,
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:923:65: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::__conditional_t<true, const std::pair<int, std::pair<int, int> >&, const std::__nonesuch&>' {aka 'const std::pair<int, std::pair<int, int> >&'}
  921 |       operator=(__conditional_t<__and_<is_copy_assignable<_T1>,
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  922 |                                        is_copy_ass