Test Instructions : The difference in size between each of the two numbers is given at a time. In the process of giving the relationship, insert the query: The difference between number A and B, if not, the output unknown
solution : The relative size of the relationship between the processing: and check Set
1. Given the relative size relationship of two points, find the root node of the two points and use path compression to point the two point father to the root node. The root node is then merged and the relative size relationship between the root nodes is given
2. When asked, at the same time find the point to the root node distance, subtract to get the relative size.
//Meek#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<cstring>#include<algorithm>#include<queue>#include<map>#include<Set>#include<stack>#include<sstream>#include<vector>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#definePB Push_back#defineFi first#defineSe Second#defineMP Make_pairinline ll read () {ll x=0, f=1; CharCh=GetChar (); while(ch<'0'|| Ch>'9') { if(ch=='-') f=-1; CH=GetChar (); } while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); } returnx*F;}//****************************************Const intn=500000+ -;Constll inf = 1ll<< A;Const intMod=1000000007;Charch[ -];intd[n],parent[n],flag,n;ll ans;voidinit () { for(intI=0; i<=n;i++) parent[i]=i; Mem (d);}intFindsintx) { if(X==parent[x])returnx; intfy=finds (parent[x]); D[X]= D[parent[x]] +D[x]; returnparent[x]=finds (Parent[x]);}intMain () {inta,b,m,w; while(SCANF ("%d%d", &n,&m)! =EOF) { if(n==0&&m==0) Break; Init (); for(intI=1; i<=m;i++) {scanf ("%s", CH); if(ch[0]=='!') {scanf ("%d%d%d",&a,&b,&W); intfx=finds (a); intfy=finds (b); if(fx!=fy) parent[fy]=fx,d[fy]=d[a]-w-D[b]; } Else{scanf ("%d%d",&a,&b); intfx=finds (a); intfy=finds (b); if(fx==FY) {cout<<d[a]-d[b]<<Endl; } Else{printf ("unknown\n"); } } } } return 0;}Code
Uvalive 6187 never Wait for Weights take right and check set