"Algorithm" and check set
Exercises
Because there are negative numbers, the way to judge the legitimacy of information is only a number of known intervals to form a known large interval.
Using and checking the set to maintain the known interval, each time (x-1,y) to connect the edge, the two endpoints into and check the set, the latter if the need for legitimacy to determine the inevitable access to one of them.
Read in V equivalent to v[y]-v[x-1] each time, maintaining and checking the distance from each node to its root.
When a joint is involved in merging with a set, that is, Fa[fa[x-1]]=fa[y], the distance to update Fa[x-1] (formerly 0) is the distance to the new root fa[y], note that the prefix and the direction of the problem can be derived from the formula.
Finally, note that when you update distances in the find process, you must follow these procedures strictly:
int T=find (fa[x]);
D[X]+=D[FA[X]];
fa[x]=t;
This is a common routine for maintaining and checking distances.
Do not understand the Konjac Konjac Hu's can turn left: http://blog.csdn.net/cjk_cjk/article/details/46681371
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn= the;intFA[MAXN],D[MAXN];intn,m,t;intFindintx) { if(fa[x]==x)returnx; intt=find (fa[x]); D[X]+=D[fa[x]]; FA[X]=T; returnfa[x];}intMain () {scanf ("%d",&T); while(t--) {memset (d,0,sizeof(d)); scanf ("%d%d",&n,&m); for(intI=0; i<=n;i++) fa[i]=i; BOOLflag=0; for(intI=1; i<=m;i++) { intx,y,w; scanf ("%d%d%d",&x,&y,&W); X--; intP=find (x), q=find (y); if(p!=q) {fa[p]=Q; D[P]=d[y]-(D[X]+W);//note the prefix and direction } Else if(D[Y]-D[X]!=W) {flag=1; Break;} } if(flag) printf ("false\n");Elseprintf"true\n"); } return 0;}
View Code
"Bzoj" 1202: [HNOI2005] cunning businessman