一眼LCT什么鬼。。。
带权并查集瞎搞搞AC
#include#include #include #include #include #include using namespace std;typedef long long LL;int fa[110000];LL d[110000];int findfa(int x){ if(fa[x]==x)return fa[x]; int FA=findfa(fa[x]); d[x]+=d[fa[x]];fa[x]=FA; return fa[x];}char ss[10];int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0)break; for(int i=1;i<=n;i++)fa[i]=i,d[i]=0LL; int x,y;LL c; for(int i=1;i<=m;i++) { scanf("%s",ss+1); if(ss[1]=='!') { scanf("%d%d%lld",&x,&y,&c); int fx=findfa(x),fy=findfa(y); if(fx!=fy) { d[fx]=d[y]+c-d[x]; fa[fx]=fy; } } else { scanf("%d%d",&x,&y); int fx=findfa(x),fy=findfa(y); if(fx!=fy)printf("UNKNOWN\n"); else printf("%lld\n",d[x]-d[y]); } } } return 0;}