poj1182 food chain (species and collection)

Source: Internet
Author: User

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

R[x] = 0 means x and father are homogeneous
R[X] = 1 x Eat father
R[X] = 2 means x is eaten by the father
Because there are only three species of animals, and three species of animals constitute a ring
So the relationship between animals can be deduced.

The relationship between C and A is (R[b] + r[c])%3; The relationship between B and father A can be 0,1,2 for the relationship between 0,1,2 C and Father B. Each kind of value, when you deduce on paper, you can get this relationship.

This relationship will be used to modify the r[c] when the path is compressed.

How to tell if D a B is a lie? When FA = = fb. It shows that both animals A and B are in a set. Then we can deduce the relationship between them.

We know two black lines, we ask for the Red Line 2, that is, the relationship between A and B. If you know the Red Line 1, then deduce the relationship between A and B, then the same as the above figure

Red Line 1 = (3-r[b])%3; This enumerates the values of r[b], and then pushes it to know.

So if (R[a] + 3-r[b])%3==d-1 that's the truth.

If FA!=FB. Then we're going to merge two sets.

。 The same reason as above, the first red Line 1, and then seek the Red Line 2, and then find the Red Line 3. Then merge two collections

1#include <stdio.h>2 intfather[55555],r[55555];3 4 voidInitintN)5 {6      for(intI=1; i<=n; ++i)7     {8Father[i] =i;9R[i] =0;Ten     } One } A  - intFindintx) - { the     if(X==father[x])returnFather[x]; -     intt =find (Father[x]); -R[X] = (R[father[x]] + r[x])%3; -FATHER[X] =T; +     returnFather[x]; -     /* + derivation of the relationship between path compression, r[father[x]] [father[x] and root node, r[x] is the relationship between X and Father[x] A     */ at } - intMain () - { -     intN,k,i,d,x,y,ans,fx,fy; -scanf"%d%d",&n,&k); -     { inAns =0; - init (n); to          for(i=0; i<k; ++i) +         { -scanf"%d%d%d",&d,&x,&y); the             if(X>n | | y>n | | (d==2&& x==y)) ans++; *             Else $             {Panax NotoginsengFX =find (x); -FY =find (y); the                 if(fx==fy) +                 { A                     if((r[x]+3-r[y])%3!=d-1) ans++; the                 } +                 Else -                 { $FATHER[FX] =fy; $                     //R[fy] = (r[x]-r[y]+d-1+3)%3; -R[FX] = (d1+R[Y]-R[X])%3; -                 } the             } -         }Wuyiprintf"%d\n", ans); the     } -     return 0; Wu}

poj1182 food chain (species and collection)

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.