Language: C++

#include<iostream>
#include<vector>
#include<string>
#include<stdio.h>

using namespace std;
int p[100000];
int rank[100000];
int weight[100000];

  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
    weight[x]=0;
  }
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
  void unite(int x,int y,int w){
    weight[x]=w;
    link(x,y);
  }
  void link(int x,int y){   
    p[x]=y;
    if(rank[x]>=rank[y]){  
      rank[y]=rank[x]+1;
    }
  }
  int findSet(int x){
    if(x==p[x]){
      return x;
    }else{
      return findSet(p[x]);
    }
  }
  int depth(int x){
    int ans=0;
    ans+=weight[x];
    while(x!=p[x]){
      ans+=weight[p[x]];
      x=p[x];
    }
    return ans;
  }

int main(){
  int n,m;
  while(scanf("%d%d",&n,&m)){
    if(!n&&!m){
      break;
    }else{
      char query[5];
     for(int I=0;i<n;i++){
makeSet(i);
}
      for(int i=0;i<m;i++){
        scanf("%s",query);
        if(query[0]=='!'){
          int a,b,w;
          scanf("%d%d%d",&a,&b,&w);
          ds.unite(a,b,w);
        }else{
          int c,d;
          scanf("%d%d",&c,&d);
          if(!ds.same(c,d)){
            printf("UNKNOWN\n");
          }else{
            printf("%d\n",ds.depth(c)-ds.depth(d));
          }
        }
      }
    }
  }
  return 0;
}
      

Compiler output:
a.cc: In function 'void makeSet(int)':
a.cc:13:5: error: reference to 'rank' is ambiguous
   13 |     rank[x]=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:8:5: note:                 'int rank [100000]'
    8 | int rank[100000];
      |     ^~~~
a.cc: In function 'bool same(int, int)':
a.cc:17:12: error: 'findSet' was not declared in this scope
   17 |     return findSet(x)==findSet(y);
      |            ^~~~~~~
a.cc: In function 'void unite(int, int, int)':
a.cc:21:5: error: 'link' was not declared in this scope
   21 |     link(x,y);
      |     ^~~~
a.cc: In function 'void link(int, int)':
a.cc:25:8: error: reference to 'rank' is ambiguous
   25 |     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:8:5: note:                 'int rank [100000]'
    8 | int rank[100000];
      |     ^~~~
a.cc:25:17: error: reference to 'rank' is ambiguous
   25 |     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:8:5: note:                 'int rank [100000]'
    8 | int rank[100000];
      |     ^~~~
a.cc:26:7: error: reference to 'rank' is ambiguous
   26 |       rank[y]=rank[x]+1;
      |       ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:5: note:                 'int rank [100000]'
    8 | int rank[100000];
      |     ^~~~
a.cc:26:15: error: reference to 'rank' is ambiguous
   26 |       rank[y]=rank[x]+1;
      |               ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:5: note:                 'int rank [100000]'
    8 | int rank[100000];
      |     ^~~~
a.cc: In function 'int main()':
a.cc:53:18: error: 'i' was not declared in this scope
   53 |      for(int I=0;i<n;i++){
      |                  ^
a.cc:61:11: error: 'ds' was not declared in this scope
   61 |           ds.unite(a,b,w);
      |           ^~
a.cc:65:15: error: 'ds' was not declared in this scope; did you mean 'd'?
   65 |           if(!ds.same(c,d)){
      |               ^~
      |               d