POJ 1182 (relationship and search) food chain

Source: Internet
Author: User

Topic Portal: http://poj.org/problem?id=1182

This is a relationship type and look up the problem, for each animal, there are only three cases: the same kind, eat and be eaten;

So you can use the 0,1,2 three numbers to represent three cases, in the use and check the set of the time to add a relational array, the initial value of all assigned to 0

Then it is the change of the relationship that is added to each step of the set,

If the ancestor node is the same, the relationship between the two has already appeared, is known, and determines whether the relationship is the same as the given d-1

The ancestor nodes are different, which means that the relationship between the two is unknown before the relationship is given.

Oh, yes, on the POJ. A single set of inputs, multiple groups will WA

Code

1#include <cstdio>2 using namespacestd;3 intfather[50005];4 intrelat[50005];5 voidGive (intN)6 {7      for(intI=1; i<=n;i++)8     {9father[i]=i;Tenrelat[i]=0;//initial relationship is 0 One     } A } - intFindintx) - { the     if(X==father[x])returnFather[x]; -     intt=find (Father[x]); -Relat[x]= (Relat[x]+relat[father[x])%3and//To update the relationship during the search for the ancestor node -father[x]=T; +     returnFather[x]; - } + intMain () A { at     intN,k,sum,d,x,y,sx,sy; -scanf"%d%d",&n,&k); - give (n); -sum=0; -      while(k--) -     { inscanf" %d%d%d",&d,&x,&y); -         if(x>n| | Y>N) to         { +sum++; -             Continue; the         } *         if(d==2&&x==y)//You can't eat yourself. $         {Panax Notoginsengsum++; -             Continue; the         } +sx=find (x); Asy=find (y); the         if(sx==sy)//Description relationship can be determined because the previous occurrence of +         { -             if((relat[x]-relat[y]+3)%3!=d-1)//advocated to avoid negative numbers $sum++; $         } -         else//relationship is uncertain, then can not judge, can only give the relationship -         { thefather[sx]=Sy; -Relat[sx]= (relat[y]-relat[x]+3+d-1)%3; The order of Sx,sy,x,y is not reversed, this is the correspondingWuyi         } the     } -printf"%d\n", sum); Wu     return 0; -}

POJ 1182 (relationship and search) food chain

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.