Language: C++

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

#define rep(l,n) for(int l = 0; l < (n); l++)
#define rep2(i,k,n) for(int i = (k); i < (n); i++)

inline void readInt2(int *a, int *b) {
  int c, p = 0; char s[20] = "";
  for(int i = 0; (c = getchar()) != '\n'; ++i) {
    if (c == ' ') { *a = atoi(s); strcpy(s, ""); p = i+1; continue; }
    else s[i] = c;
  }
  *b = atoi(s+p);
}

/*inline void readInt3(int *a, int *b, int *c) {
  int d, p = 0; char s[20] = "";
  for(int i = 0; (d = getchar()) != '\n'; i++) {
    if(d == ' ') {
      if(p == 0) { *a = atoi(s); strcpy(s, ""); p = i+1; }
      else {*b = atoi(s+p); strcpy(s, ""); p = i+1; }
    } else s[i] = d;
  }
  *c = atoi(s+p);
  }*/
inline void readInt(int *a) {
  int c; char s[20] = "";
  for (int i = 0; (c = getchar()) != '\n'; ++i) s[i] = c;
  *a = atoi(s);
}

int parent[10000000], rank[1000000];
void uf_init(int n);
void unite(int x, int y);
bool same(int x, int y);
int find(int x);

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

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

vector<edge> v;
edge es;
int N, M; // 頂点数と辺の数

int kruskal() {
  int res = 0;
  uf_init(N);
  sort(v.begin(), v.end(), comp);
  rep(i,M) {
    //cout << v[i].u << " " << v[i].v << " " << v[i].cost << endl;
      edge e = v[i];
      if (!same(e.u, e.v)) {
        //cout << e.u << " " << e.v << endl;
	      unite(e.u, e.v);
	      res += e.cost;
      }
    }
  return res;
}

// Union Find
void uf_init(int n) {
  rep(i,n+1) { parent[i] = i; rank[i] = 0; }
}
int find(int x) {
  if (parent[x] == x) return x;
  else return parent[x] = find(parent[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if(x == y) return;
  if (rank[x] < rank[y]) parent[x] = y;
  else {
    parent[y] = x;
    if(rank[x] == rank[y]) rank[x]++;
  }
}
bool same(int x, int y) {
  return find(x) == find(y);
}


int main(void) {
  int H, W, p, q;

  readInt2(&W, &H);
  N = (W+1) * (H+1);
  //cout << W << " " << H << endl;

  for (int i = 0; i < W; i++) {
    readInt(&p);
    rep(j,H+1) {
        edge e;
        e.u = i+j*(W+1); e.v = i+j*(W+1)+1; e.cost = p;
        v.push_back(e);
    }
  }
  for (int i = 0; i < H; i++) {
    readInt(&q);
    rep(j,W+1) {
        edge e;
        e.u = i*(W+1)+j; e.v = i*(W+1)+(W+1)+j; e.cost = q;
        v.push_back(e);
    }
  }
  M = W*(H+1) + H*(W+1);
  /*for (int i = 0; i < M; i++) {
    cout << v[i].u << " " << v[i].v << " " << v[i].cost << endl;
  }*/

  printf("%d\n",kruskal());
  return 0;
}


Compiler output:
a.cc: In function 'void uf_init(int)':
a.cc:73:31: error: reference to 'rank' is ambiguous
   73 |   rep(i,n+1) { parent[i] = i; rank[i] = 0; }
      |                               ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 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:36:23: note:                 'int rank [1000000]'
   36 | int parent[10000000], rank[1000000];
      |                       ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:83:7: error: reference to 'rank' is ambiguous
   83 |   if (rank[x] < rank[y]) parent[x] = y;
      |       ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:36:23: note:                 'int rank [1000000]'
   36 | int parent[10000000], rank[1000000];
      |                       ^~~~
a.cc:83:17: error: reference to 'rank' is ambiguous
   83 |   if (rank[x] < rank[y]) parent[x] = y;
      |                 ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:36:23: note:                 'int rank [1000000]'
   36 | int parent[10000000], rank[1000000];
      |                       ^~~~
a.cc:86:8: error: reference to 'rank' is ambiguous
   86 |     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:36:23: note:                 'int rank [1000000]'
   36 | int parent[10000000], rank[1000000];
      |                       ^~~~
a.cc:86:19: error: reference to 'rank' is ambiguous
   86 |     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:36:23: note:                 'int rank [1000000]'
   36 | int parent[10000000], rank[1000000];
      |                       ^~~~
a.cc:86:28: error: reference to 'rank' is ambiguous
   86 |     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:36:23: note:                 'int rank [1000000]'
   36 | int parent[10000000], rank[1000000];
      |                       ^~~~