There are three types of animal a,b,c in the animal kingdom, and the food chain of these three animals is an interesting ring. A eat B, B eat c,c eat a.
Existing n animals, numbered with 1-n. Every animal is one of the a,b,c, but we don't know what it is.
Some people describe the food chain relationship between these n animals in two ways:
The first argument is "1 x y", which means that x and Y are homogeneous.
The second argument is "2 x y", which means x eats y.
This person to n animals, with the above two statements, a sentence after sentence to say K sentence, this k sentence some is true, some false. When one sentence satisfies one of the following three, the sentence is a lie, otherwise it is the truth.
1) The current words conflict with some of the preceding words, which is false;
2) The current word in x or y is greater than N, is a lie;
3) The current words say x eats x, is a lie.
Your task is to output the total number of falsehoods according to the given N (1 <= n <= 50,000) and the K-sentence (0 <= K <= 100,000).
If Father[a]=b, rela[a]=0 represents a and B similar; Rela[a]=1 says B can eat a;rela[a]=2 that a can eat B.
Then it is to enumerate all the circumstances, you can deduce the formula, it is best not to write each weight separately, otherwise it will be chaotic.
The first belt right and check set, feel not easy to push the formula, out of the results, natural easy to use.
#include <cstdio>#include<cstring>#include<vector>#include<cmath>#include<stack>#include<cstdlib>#include<queue>#include<map>#include<iostream>#include<algorithm>using namespaceStd;typedefLong LongLL;intN,k,ans;intd,x,y;intfather[50100],rela[50100];intFINDFA (intX//to find the ancestor, by the way, to clarify the relationship .{ if(x!=Father[x]) { intZU=FINDFA (Father[x]);//Find AncestorsRela[x]= (Rela[x]+rela[father[x])%3;//after enumeration, you can derivefather[x]=zu; } returnfather[x];}intCombineintOpintXinty) { intZUX=FINDFA (x);//to find the ancestor, by the way, the relationship between X and ancestor intZuy=findfa (y);//to find the ancestors, and to clarify the relationship between Y and ancestors if(Zux==zuy)//in a system { if((rela[y]-rela[x]+3)%3!=OP)return false;//after enumeration, you can derive Else return true; } Else//is two systems, it will be able to pass the relationship between the rotation of the docking{Father[zuy]=zux;//let the Fathers on theRela[zuy]= (rela[x]-rela[y]+op+3)%3;//after enumeration, you can derive return true; }}intMain () {scanf ("%d%d",&n,&k); for(intI=1; i<=n;i++) Father[i]=i; while(k--) {scanf ("%d%d%d",&d,&x,&y); if(x>n| | y>n| | (d==2&&x==y)) ans++; Else if(!combine (d1, x, y)) ans++; } printf ("%d\n", ans); return 0;}
POJ 1182 food chain with right and check