Description
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).
Input
The first line is two integers n and K, separated by a single space.
The following k lines each line is three positive integer d,x,y, and two numbers are separated by a space, where D denotes the kind of claim.
If d=1, it means that x and Y are homogeneous.
If d=2, it means x eats y.
Output
There is only one integer that represents the number of false lies.
Sample Input
100 71 101 1 2 1 22 2 3 2 3 3 1 1 3 2 3 1 1 5 5
Sample Output
3
problem-solving ideas: This problem wants us to get the number of lies, if this is the words and previous words conflict is a lie . I began to have no idea, looked at the sample, readily draw a picture, feel like a graph theory of things, but in fact, think carefully, in fact, this is a state of division!!! For example, A and B are similar relationships, then divide A and b into a set, and then say that A and B is the predator-prey relationship must be a lie. But the problem is that the role of an animal in the food chain is not certain, that a species may be a predator or a predator, and possibly a relationship between its peers. All of the three states played are represented. GCA, for species x,x represents Class A, X+n represents Class B, x+2n represents Class C, where a eats b,b eats c,c eats a.
For the two animals, if they are of the same kind, there must be no predation and predation, and if there is a predator-prey relationship, there must be no predator-prey and similar relationships.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMAX 50010*35 using namespacestd;6 intPre[max];7 intFind (intx)8 {9 intA;TenA=x; One while(pre[a]!=a) A { -A=Pre[a]; - } the returnA; - } - voidUnion (intROOT1,intRoot2) - { + intx, y; -x=Find (ROOT1); +y=Find (ROOT2); A if(x!=y) at { -pre[x]=y; - } - } - intSame (intXintY///determine whether two species have a relationship - { in returnFind (x) = =Find (y); - } to intMain () + { - intn,m,i,counts; the inta,b,q; *scanf"%d%d",&n,&m); $counts=0;Panax Notoginseng for(i=1; i<=n*3; i++) - { thepre[i]=i; + } A for(i=1; i<=m;i++) the { +scanf"%d%d%d",&q,&a,&b); - if(a<1|| a>n| | b<1|| B>N) $ { $counts++; - Continue; - } the if(q==1) - {Wuyi if(Same (a,b+n) | | Same (a,b+2*n))///there is a predator or prey relationship . the { -counts++; Wu } - Else///establishing similar relationships About { $ Union (A, b); -Union (a+n,b+n); -Union (A +2*n,b+2*n); - } A } + Else if(q==2) the { - if(Same (A, b) | | Same (a,b+2*n))///there is a similar or predatory relationship $ { thecounts++; the } the Else///establish all the predator-prey relationships the { -Union (a,b+n); inUnion (a+n,b+2*n); theUnion (A +2*n,b); the } About } the } theprintf"%d\n", counts); the return 0; +}
- When they're the same, they're not prey or prey.
- when they're prey, they're not the same or prey .
Food chain POJ 1182 (and check set)