Language: C++

#include<iostream>
using namespace std;
int parent[1<<17],rank[1<<17],weight[1<<17];
int root(int x)
{
	if(parent[x]==x)return x;
	else
	{
		int r=root(parent[x]);
		weight[x]+=weight[parent[x]];
		return parent[x]=r;
	}
}
int get_weight(int x)
{
	root(x);
	return weight[x];
}
bool issame(int x,int y)
{
	return root(x)==root(y);
}
void diff(int x,int y)
{
	if(!issame(x,y))
	{
		cout<<"UNKNOWN"<<endl;
	}
	else cout<<get_weight(y)-get_weight(x)<<endl;
}
void merge(int x,int y,int w)
{
	w+=get_weight(x)-get_weight(y);
	x=root(x);y=root(y);
	if(rank[x]<rank[y])swap(x,y),w*=-1;
	if(rank[x]==rank[y])rank[x]++;
	parent[y]=x;
	weight[y]=w;
}
main()
{
	int n,m;
	while(cin>>n>>m,n)
	{
		for(int i=0;i++<n;)parent[i]=i,rank[i]=0,weight[i]=0;
		for(int i=0;i<m;i++)
		{
			char c;cin>>c;
			if(c=='!')
			{
				int a,b,w;cin>>a>>b>>w;
				merge(a,b,w);
			}
			else
			{
				int a,b;cin>>a>>b;
				diff(a,b);
			}
		}
	}
}



Compiler output:
a.cc: In function 'void merge(int, int, int)':
a.cc:35:12: error: reference to 'rank' is ambiguous
   35 |         if(rank[x]<rank[y])swap(x,y),w*=-1;
      |            ^~~~
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:3:19: note:                 'int rank [131072]'
    3 | int parent[1<<17],rank[1<<17],weight[1<<17];
      |                   ^~~~
a.cc:35:20: error: reference to 'rank' is ambiguous
   35 |         if(rank[x]<rank[y])swap(x,y),w*=-1;
      |                    ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:3:19: note:                 'int rank [131072]'
    3 | int parent[1<<17],rank[1<<17],weight[1<<17];
      |                   ^~~~
a.cc:36:12: error: reference to 'rank' is ambiguous
   36 |         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:3:19: note:                 'int rank [131072]'
    3 | int parent[1<<17],rank[1<<17],weight[1<<17];
      |                   ^~~~
a.cc:36:21: error: reference to 'rank' is ambiguous
   36 |         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:3:19: note:                 'int rank [131072]'
    3 | int parent[1<<17],rank[1<<17],weight[1<<17];
      |                   ^~~~
a.cc:36:29: error: reference to 'rank' is ambiguous
   36 |         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:3:19: note:                 'int rank [131072]'
    3 | int parent[1<<17],rank[1<<17],weight[1<<17];
      |                   ^~~~
a.cc: At global scope:
a.cc:40:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   40 | main()
      | ^~~~
a.cc: In function 'int main()':
a.cc:45:48: error: reference to 'rank' is ambiguous
   45 |                 for(int i=0;i++<n;)parent[i]=i,rank[i]=0,weight[i]=0;
      |                                                ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:3:19: note:                 'int rank [131072]'
    3 | int parent[1<<17],rank[1<<17],weight[1<<17];
      |                   ^~~~