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;
  }
 int findSet(int x){
    if(x==p[x]){
      return x;
    }else{
      return findSet(p[x]);
    }
  }
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
  void link(int x,int y){   
    p[x]=y;
    if(rank[x]>=rank[y]){  
      rank[y]=rank[x]+1;
    }
  }
   void unite(int x,int y,int w){
    weight[x]=w;
    link(x,y);
  }

  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 'void link(int, int)':
a.cc:28:8: error: reference to 'rank' is ambiguous
   28 |     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:28:17: error: reference to 'rank' is ambiguous
   28 |     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:29:7: error: reference to 'rank' is ambiguous
   29 |       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:29:15: error: reference to 'rank' is ambiguous
   29 |       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:54:18: error: 'i' was not declared in this scope
   54 |      for(int I=0;i<n;i++){
      |                  ^
a.cc:62:11: error: 'ds' was not declared in this scope
   62 |           ds.unite(a,b,w);
      |           ^~
a.cc:66:15: error: 'ds' was not declared in this scope; did you mean 'd'?
   66 |           if(!ds.same(c,d)){
      |               ^~
      |               d