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 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5
Sample Output
3
The analysis is as follows:
Because N and K are large, it is important to maintain the relationship between animals efficiently and to quickly determine whether a contradiction arises. And check set is to maintain "belong to the same group" data structure, but in the subject, not only belong to the same class of information, but also the existence of predator-prey relationship.
For each animal I create 3 elements i-a, I-b, i-c, and use this 3*n element to build and find a set. This check set maintains the following information:
①i-x says "I belong to Kind X".
② each of the groups in the set indicates that all elements in the group represent situations that occur at the same time or do not occur.
For example, if I-a and J-b are in the same group, it means that if I belong to kind a then J must belong to the kind B, if J belongs to the kind B then I must belong to the kind a. Therefore, for each piece of information, you just need to follow the steps below.
1) The first, x and Y belong to the same kind ... merge x-a and y-a, X-b and Y-b, X-c and Y-c.
2) The second, the two different classes, X eat y .... Merge x-a and Y-b, X-b and Y-c, X-c and Y-a.
However, before merging, it is necessary to decide whether the merger will create contradictions.
For example, in the case of the first information, it is necessary to check whether the information such as x-a and Y-b or y-c are in the same group;
in the case of the second information, it is necessary to check whether information such as x-a and Y-b or y-c are in the same group.
The AC code is as follows: (there is a corresponding comment in the title)
#include <cstdio>#include <cstring>#include <iostream>using namespace STD;Const intmaxn=201314;intD[MAXN],X[MAXN],Y[MAXN],N,K,PAR[MAXN];voidInitintN) { for(intI=0; i<n; i++) par[i]=i;}intFind_par (intXX) {if(Xx==par[xx])returnxxElse returnPar[xx]=find_par (Par[xx]);}voidUniteintAintb) {intPar_a=find_par (a);intPar_b=find_par (b);if(Par_a!=par_b) Par[par_a]=par_b;}BOOLSame (intAintb) {returnFind_par (a) ==find_par (b);voidInput () {scanf("%d%d", &n,&k); for(intI=0; i<k; i++)scanf("%d%d%d", &d[i],&x[i],&y[i]);}voidSolve () {input (); Init3*N);intans=0; for(intI=0; i<k; i++) {intNum=d[i];inta=x[i]-1, b=y[i]-1;if(a<0|| a>n-1|| b<0|| b>n-1)//Do not meet the criteria{ans++;Continue; }if(num==1)//indicates that both A and B are of the same species, may be of type A, may be of type B, and of course may be of type C{//For the first kind of information, there is no predator-prey relationship. //should be excluded 2*3, but because each time is 3 cases simultaneously merge, that is to say //3 species in 1 kinds of situations can be represented, so just write the following two kinds of situations, the same. if(Same (a,b+n) | | Same (a,b+2*n)) ans++;Else{Unite (A, b);//If both are Class A, merge into a group ofUnite (A+N,B+N);//If both are Class B, merge into a group ofUnite (A +2*n,b+2*N);//If both are Class C, merge into a group of} }Else//show A and B are not the same species, a eat B (a eat b,b eat C,c eat a){//For the second kind of information can not appear both are homogeneous or the relationship between the two violations of the predator-prey relationship if(Same (A, b) | | Same (a,b+2*n)) ans++;Else{Unite (A,B+N);//a Eat bUnite (a+n,b+2*N);//b Eat CUnite (A +2*N,B);//c Eat a} } }cout<<ans<<endl;}intMain () {solve ();return 0;}
POJ-1182 the food chain (and the use of a check set)