Language: C++

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#define MAX_N 100010

using namespace std;

int par[100010];
int rank[100010];

void init(int n){
  int i;
  for(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]);
  }
}

void unite(int x ,int y){
  x=find(x);
  y=find(y);
  if(x==y) return ;

  if((int)rank[x]<(int)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);
}

struct edge{int u,v,cost;};

bool comp(const edge& e1,const edge& e2){
  return e1.cost<e2.cost;
}

edge es[MAX_N];
int V,E;

int kruskal(){
  sort(es,es+E,comp);
  init(V);
  int res=0;
  for(int i=0;i<E;i++){
    edge e=es[i];
    if(!same(e.u,e.v)){
      unite(e.u,e.v);
      res += e.cost;
    }
  }
  return res;
}

int main(void){
  string S;
  int W,H;
  int res;
  cin>>W>>H;
  int i,j;
  vector<int> p(W);
  vector<int> q(H);
  int k=0;

  for(i=0;i<W;i++) cin>>p[i];
  for(i=0;i<H;i++) cin>>q[i];

  V=(W+1)*(W+1);
  E=((W+1)*H)+(W*(H+1));

  k=0;
  for(j=0;j<=H;j++){
    for(i=0;i<=W;i++) {
      if(i<W){
        es[k].u=j*(W+1)+i;      
        es[k].v=j*(W+1)+i+1;
        es[k].cost=p[i];
        k++;
      }
      if(j<H){
        es[k].u=j*(W+1)+i;      
        es[k].v=j*(W+1)+i+W+1;
        es[k].cost=q[j];
        k++;
      }
    }
  }
  res=kruskal();
  cout<<res<<endl;
  return 0;
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:16:5: error: reference to 'rank' is ambiguous
   16 |     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 a.cc:2:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:5: note:                 'int rank [100010]'
   10 | int rank[100010];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:33:11: error: reference to 'rank' is ambiguous
   33 |   if((int)rank[x]<(int)rank[y]){
      |           ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:5: note:                 'int rank [100010]'
   10 | int rank[100010];
      |     ^~~~
a.cc:33:24: error: reference to 'rank' is ambiguous
   33 |   if((int)rank[x]<(int)rank[y]){
      |                        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:10:5: note:                 'int rank [100010]'
   10 | int rank[100010];
      |     ^~~~
a.cc:37:8: error: reference to 'rank' is ambiguous
   37 |     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:10:5: note:                 'int rank [100010]'
   10 | int rank[100010];
      |     ^~~~
a.cc:37:17: error: reference to 'rank' is ambiguous
   37 |     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:10:5: note:                 'int rank [100010]'
   10 | int rank[100010];
      |     ^~~~
a.cc:37:26: error: reference to 'rank' is ambiguous
   37 |     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:10:5: note:                 'int rank [100010]'
   10 | int rank[100010];
      |     ^~~~