POJ 1182 food chain "and check set"

Source: Internet
Author: User

Problem-solving ideas: First, there is no thought----and then read a few problem-solving reports

http://blog.csdn.net/ditian1027/article/details/20804911

http://poj.org/showmessage?message_id=152847

http://blog.163.com/jiazheng2222%40126/blog/static/16963238320101258935104/

This is an analysis of discuss inside---

http://poj.org/showmessage?message_id=152847 My understanding is that there must be some kind of connection between them for any of the two elements in the set, A/b,> > because the elements of the concentration are connected, Otherwise, it will not be merged into the current collection. So we > > the relationship between these 2 elements into an offset, in the context of the food chain, it may be assumed > > a->b offset 0 o'clock A and b similar > > a->b offset 1 o'clock a eat b> > a-> B Offset 2 o'clock A is eaten by B, that is, B eats a> > with these basics, we can perform a relationship transformation between any two elements in the and check. > > You may wish to continue assuming that the current collection root node of a aa,b the current set root node bb,a->b The offset value of d-1 (ask for known conditions given in the question) > > (1) if AA and BB are not the same, then we merge BB onto AA, and update DELTA[BB] Value (Delta[i] represents the current collection root node of I to the offset of i) > >     at this point aa->bb = aa->a + a->b + B->BB, maybe this step is called vector thinking mode > >     on further conversion to: AA->BB = (delta[a]+d-1+3-delta[b])%3 = Delta[bb], (modulo 3 is the guaranteed offset value always at [0,2]) > > (2) If AA and BB are the same, then we verify that the offsets between a->b are consistent with the d-1 given in the title > >     a->b = a->aa + aa->b = a->aa + bb->b,> >     further conversion to: A->b = (3-delta[a]+delta[b])%3,> > If the     same is true, otherwise false. > > Hope can help with LS:]

  

              Below is my own experience

PRE[X]: Indicates that the parent of X is Pre[x]p=find (x), where p represents the root node of x Relation[x]: Represents the relationship of node x to its root node, relation[x]=p->x (because the vector is used later, the direction of the vector is described first Out, Relation[x] The vector represents the vector from the root node P points to X, relation[x]=0 represents p with X-like relation[x]=1 expression P eats x relation[x]=2 means x eats p----- ------------------------------------------------------------------------------for a given sentence d,x,y first determine whether X, Y is in the same set relationship (  The set is the true or false of the if (x, y in the same set) that can be judged by the X, y relationship) {;} else {This sentence is considered true, X, y are merged;} -----------------------------------------------------------------------------------judge the truth of a sentence can have two ways (method one)     Directly list all the cases that are true, if the sentence does not meet all the listed cases, then this sentence is false 1) d==1 means x, Y is similar relation[x] relation[y] 0 0 1 1 2 2 So if relation[x]!=relation[y], then this sentence is a Lie 2) d==2 means x eat yrelation[x] relation[y] 0 2/     /At this time X and root node similar, want to let X eat y,relation[y]=2 2 1//At this time X eat root node, to let X eat y, then Y and root node similar, relation[y]=1 1 0 At this point x is eaten by the root node, to let X eat y, then y should eat root node, relation[y]=1 if not meet the three sets of corresponding values, then this sentence is False (method II) detailed see HTTP://POJ.ORG/SHOWMESSAGe?message_id=152847 according to the vector to do x->y=x->p+p->y//because at this time p=q, so you can change p to q =-relation[x]+relation [y] =relation[y]- relation[x]//further processing, in order to prevent a negative value for the expression, add 3,= (relation[y]-relation[x]+3)%3//For the value of the expression between 0 to 2, to the expression modulo 3 and because the title is D==1 x, Y is similar, corresponding to our provisions of the same kind as 0, should be d-1 so that we calculate the offset and said that the offset of the sentence is consistent, if not consistent, then said the Lie ((relation[y]-relation[x]+3)%3)!=d-1, This sentence is a lie---------------------------------------------------------------------------------------p,q different when merging X, Y merge Q to P, update relation[q] p->q=p->x+x->y+y->q =relation[x]+ (d-1) + (-relation[y])//To prevent the expression from negative value, plus 3 = (re lation[x]-relation[y]+ (d-1) +3)%3//to make the expression value between 0 and 2, modulo 3 is relation[q]= (relation[x]-relation[y]+ (d-1) +3)%3---------- ------------------------------------------------------------------------------the compression path, update the relation[] array in more detail: http ://blog.csdn.net/ditian1027/article/details/20804911 now know son node X, Father node FX, grandfather node FFX, request ffx->x then ffx->x=ffx->fx+ Fx->x =relation[fx]+relation[x] that is relation[x]= (relation[x]+relation[tmp])%3 the FX as the biological father of X, and because FThe IND () function is with path compression, after compression, the Father node of X becomes FX ', cannot be used to calculate and we need the value of FX, so it is recorded with TMP,  Re-compress----------------------------------------------------------------------------------------

Reflection: When using vectors to represent relation[] arrays, the beginning of the vector and the end point of the vector must be clear, or the subsequent sub-symbol will not be

Here are two different codes for judging the true and false

#include <stdio.h> #define MAXN 50010int pre[maxn],relation[maxn];int find (int a) {int tmp;tmp=pre[a];if (a!=pre[a ]) Pre[a]=find (Pre[a]); relation[a]= (relation[a]+relation[tmp])%3;return pre[a];} void unionroot (int x,int y,int d) {int p,q;p=find (x); Q=find (y);p re[q]=p;relation[q]= (relation[x]-relation[y]+3+d-1)% 3;} int main () {int i,n,k,sum,x,y,p,q,d;scanf ("%d%d", &n,&k); for (i=0;i<=maxn;i++) {pre[i]=i;relation[i]=0;} Sum=0;while (k--) {scanf ("%d%d%d", &d,&x,&y);p =find (x); Q=find (y); if ((x>n| | Y>n) | | (x==y&&d==2)) {sum++;continue;    } if (p==q) {if ((relation[y]-relation[x]+3)%3!=d-1) sum++;} Elseunionroot (x,y,d);} printf ("%d\n", sum);}

  

#include <stdio.h> #define MAXN 50010int pre[maxn],relation[maxn];int find (int a) {int tmp;tmp=pre[a];if (a!=pre[a ]) Pre[a]=find (Pre[a]); relation[a]= (relation[a]+relation[tmp])%3;return pre[a];} void unionroot (int x,int y,int d) {int p,q;p=find (x); Q=find (y);p re[p]=q;relation[p]= (relation[y]-relation[x]+3+d-1)% 3;} int main () {int i,n,k,sum,x,y,p,q,d;scanf ("%d%d", &n,&k); for (i=0;i<=maxn;i++) {pre[i]=i;relation[i]=0;} Sum=0;while (k--) {scanf ("%d%d%d", &d,&x,&y);p =find (x); Q=find (y); if ((x>n| | Y>n) | | (x==y&&d==2)) {sum++;continue;    } if (p==q) {if (D==1&&relation[x]!=relation[y]) ++sum;if (d==2) {if (relation[x]==0&&relation[y]!=2) + + Sum;if (relation[x]==1&&relation[y]!=0) ++sum;if (relation[x]==2&&relation[y]!=1) ++sum;} else  unionroot (x,y,d);} printf ("%d\n", sum);}

  

POJ 1182 food chain "and check set"

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.