Language: C++

#include <bits/stdc++.h>

using namespace std;
int par[1000000];
int rank[100000];

//nの要素で初期化
void init(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 par[x] = find(par[x]);
  }
}

//xとyの属する集合を併合
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]++;
  }
}

// xとyが同じ属性に属するか否か
bool same(int x, int y){
  return find(x) == find(y);
}
int main(){
  long long n;
  cin >> n;
  vector<pair<long long,int> > to(n);
  vector<pair<long long,int> > from(n);

  for(int i = 0; i < n; i++){
    cin >> to[i].first >> from[i].first;
    to[i].second = i;
    from[i].second = i;
  }
  sort(to.begin(),to.end());
  sort(from.begin(),from.end());

  long long ans = 0;
  deque<long long> mismatch;
  int toCnt = 0, fromCnt = 0;
  init(n);
  while(toCnt != n || fromCnt != n){
    if(mismatch.empty() == false){
      ans += min(to[toCnt].first , mismatch.front());
      mismatch.pop_front();
      toCnt ++;
      continue;
    }
    if(toCnt != n-1 || unite(to[toCnt].second, from[n-fromCnt-1].second)){
      mismatch.push_back(from[n-1-fromCnt].first);
      fromCnt++;
      ans += min(to[toCnt].first, from[n-fromCnt-1].first);
      toCnt++;
      fromCnt++;
    }else{
      ans += min(to[toCnt].first , from[n-fromCnt-1].first);
      toCnt ++;
      fromCnt ++;
    }
    //cout << ans << " toCnt " << toCnt << " fromCnt " << fromCnt << endl;

  }
  cout << ans << endl;
  return 0;
}


Compiler output:
a.cc: In function 'void init(int)':
a.cc:11:5: error: reference to 'rank' is ambiguous
   11 |     rank[i] = 0;
      |     ^~~~
In file included from /usr/include/c++/14/bits/stl_pair.h:60,
                 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:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:5:5: note:                 'int rank [100000]'
    5 | int rank[100000];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:30:6: error: reference to 'rank' is ambiguous
   30 |   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:5:5: note:                 'int rank [100000]'
    5 | int rank[100000];
      |     ^~~~
a.cc:30:16: error: reference to 'rank' is ambiguous
   30 |   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:5:5: note:                 'int rank [100000]'
    5 | int rank[100000];
      |     ^~~~
a.cc:34:8: error: reference to 'rank' is ambiguous
   34 |     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:5:5: note:                 'int rank [100000]'
    5 | int rank[100000];
      |     ^~~~
a.cc:34:19: error: reference to 'rank' is ambiguous
   34 |     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:5:5: note:                 'int rank [100000]'
    5 | int rank[100000];
      |     ^~~~
a.cc:34:28: error: reference to 'rank' is ambiguous
   34 |     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:5:5: note:                 'int rank [100000]'
    5 | int rank[100000];
      |     ^~~~
a.cc: In function 'int main()':
a.cc:67:29: error: could not convert 'unite(to.std::vector<std::pair<long long int, int> >::operator[](((std::vector<std::pair<long long int, int> >::size_type)toCnt)).std::pair<long long int, int>::second, from.std::vector<std::pair<long long int, int> >::operator[](((std::vector<std::pair<long long int, int> >::size_type)((n - ((long long int)fromCnt)) - 1))).std::pair<long long int, int>::second)' from 'void' to 'bool'
   67 |     if(toCnt != n-1 || unite(to[toCnt].second, from[n-fromCnt-1].second)){
      |                        ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~