In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences.
Input
The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100, 000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100, 000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ̸= b). That is, w = wb−wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ̸= b). The last dataset is followed by a line consisting of two zeros separated by a space.
Output
For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb−wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print ‘UNKNOWN’ followed by a newline.
题意:n个点,m次询问,当输入'!'时两者之间建一条权重为w的边(反向-w),输入‘?’时查询两者之间路径长。
并查集裸题

这个题目是带权值的并查集。对于每个节点,除了保存它的父亲节点以外还要保存它与父亲节点的差,这样就能实现同一个集合中任意两个元素的大小比较了。

同时发现,由于对于同一个集合中的每一个元素都是和它的父亲节点做比较,所以整个来说这一个集合是一棵树,所有的集合可以看成是一个森林。

下面是代码(写的有点不清楚,反正改着改着就过了emmm):
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
#define INF 0x3fffffff
#define maxn 100005
#define mod 998244353
int n,m,k,t;
int num[maxn],fa[maxn];
char c;
int fi(int x){
    if(fa[x]==x)
        return x;
    int xx=fi(fa[x]);
    num[x]+=num[fa[x]];
    fa[x]=xx;
    return fa[x];
}
int x,y,w;
int main(){
    while(scanf("%d%d",&n,&m),n){
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++)
            fa[i]=i;
        for(int i=1;i<=m;i++){
            cin>>c;
            if(c=='!'){
                scanf("%d%d%d",&x,&y,&w);
                int fx=fi(x),fy=fi(y);
                fa[fy]=fx;
                num[fy]=num[x]+w-num[y];
            }
            else{
                scanf("%d%d",&x,&y);
                int fx=fi(x),fy=fi(y);
                if(fx!=fy){
                    puts("UNKNOWN");
                    continue;
                }
                printf("%d\n",num[y]-num[x]);
            }
        }
    }
}