Language: C++

#include<iostream>
#include<cstdio>
#include<bitset>
#include<algorithm>
using namespace std;

class union_find{
private:
	vector<int> parents;
	vector<int> rank;
	vector<int> weight;
public:
	union_find(int n){
		for(int i=0;i<n;i++){
			parents.push_back(i);
			rank.push_back(0);
			weight.push_back(0);
		}
	}
	int find(int x){
		if(parents[x]==x){
			return x;
		}else{
			int tmp=find(parents[x]);
			weight[x]=weight[x]+weight[parents[x]];
			return parents[x]=tmp;
		}
	}
	void unite(int x,int y,int w){
		int tmpx=find(x);
		int tmpy=find(y);
		if(tmpx==tmpy)return;
		if(rank[tmpx]<rank[tmpy]){
			weight[tmpx]=w-weight[x]+weight[y];
			parents[tmpx]=tmpy;
		}else{
			weight[tmpy]=-w-weight[y]+weight[x];
			parents[tmpy]=tmpx;
			if(rank[tmpx]==rank[tmpy])rank[tmpx]++;
		}
	}
	int get_weight(int x,int y){
		if(find(x)==find(y)){
			return weight[x]-weight[y];
		}else{
			return -2000000;
		}
	}
};
int main(){
	char c;
	int a,b,w;
	int n,m;
	while(cin>>n>>m,n!=0||m!=0){
		union_find uf(n+1);
		for(int i=0;i<m;i++){
			cin>>c;
			if(c=='!'){
				cin>>a>>b>>w;
				uf.unite(a,b,w);
			}else if(c=='?'){
				cin>>a>>b;
				int tmp=uf.get_weight(a,b);
				if(tmp!=-2000000){
					cout<<tmp<<endl;
				}else{
					cout<<"UNKNOWN"<<endl;
				}
			}
		}
	}
}

Compiler output:
a.cc:9:9: error: 'vector' does not name a type
    9 |         vector<int> parents;
      |         ^~~~~~
a.cc:10:9: error: 'vector' does not name a type
   10 |         vector<int> rank;
      |         ^~~~~~
a.cc:11:9: error: 'vector' does not name a type
   11 |         vector<int> weight;
      |         ^~~~~~
a.cc: In constructor 'union_find::union_find(int)':
a.cc:15:25: error: 'parents' was not declared in this scope
   15 |                         parents.push_back(i);
      |                         ^~~~~~~
a.cc:16:29: error: expected unqualified-id before '.' token
   16 |                         rank.push_back(0);
      |                             ^
a.cc:17:25: error: 'weight' was not declared in this scope; did you mean 'get_weight'?
   17 |                         weight.push_back(0);
      |                         ^~~~~~
      |                         get_weight
a.cc: In member function 'int union_find::find(int)':
a.cc:21:20: error: 'parents' was not declared in this scope
   21 |                 if(parents[x]==x){
      |                    ^~~~~~~
a.cc:25:25: error: 'weight' was not declared in this scope; did you mean 'get_weight'?
   25 |                         weight[x]=weight[x]+weight[parents[x]];
      |                         ^~~~~~
      |                         get_weight
a.cc: In member function 'void union_find::unite(int, int, int)':
a.cc:33:24: error: expected unqualified-id before '[' token
   33 |                 if(rank[tmpx]<rank[tmpy]){
      |                        ^
a.cc:34:25: error: 'weight' was not declared in this scope; did you mean 'get_weight'?
   34 |                         weight[tmpx]=w-weight[x]+weight[y];
      |                         ^~~~~~
      |                         get_weight
a.cc:35:25: error: 'parents' was not declared in this scope
   35 |                         parents[tmpx]=tmpy;
      |                         ^~~~~~~
a.cc:37:25: error: 'weight' was not declared in this scope; did you mean 'get_weight'?
   37 |                         weight[tmpy]=-w-weight[y]+weight[x];
      |                         ^~~~~~
      |                         get_weight
a.cc:38:25: error: 'parents' was not declared in this scope
   38 |                         parents[tmpy]=tmpx;
      |                         ^~~~~~~
a.cc:39:32: error: expected unqualified-id before '[' token
   39 |                         if(rank[tmpx]==rank[tmpy])rank[tmpx]++;
      |                                ^
a.cc:39:61: error: expected initializer before '++' token
   39 |                         if(rank[tmpx]==rank[tmpy])rank[tmpx]++;
      |                                                             ^~
a.cc:39:61: error: expected ';' before '++' token
a.cc: In member function 'int union_find::get_weight(int, int)':
a.cc:44:32: error: 'weight' was not declared in this scope; did you mean 'get_weight'?
   44 |                         return weight[x]-weight[y];
      |                                ^~~~~~
      |                                get_weight