"Bzoj" 1202: [HNOI2005] cunning businessman

Source: Internet
Author: User

"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

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.