Questions surface:
Title Description
There are three types of animals in the animal kingdom A, B, C, three types of animal food chain constitute an interesting ring. A food B, B
Eat C, C eat A.
Animals prior N, 1 - id N. Each animal is A, B, C in kind, but we do not know
Which in the end is which.
Some people describe this relationship N animal food chain formed by two different ways:
The first argument is "1 XY", represents the X and Y are similar.
The second argument is "2 XY", X represents eat Y.
This person for N animals, with the above two statements, one sentence by sentence to say K, K which some true sentence
, Some false. When one of the following three words, this sentence is a lie, the truth is otherwise.
• The current case with some true words of earlier conflicts, is a lie
• if the current X or Y greater than N, that is a lie
• Current eat as saying X X, is a lie
Your task is given according to the total number of N and K words, the output of lies.
Input Format
The input data from eat.in
The first line of two integers, N, K, N expressed animal, K words.
Each word line the second line (in accordance with the requirements of the subject, see examples)
Output Format
Output in the eat.out
Line, an integer representing the total number of lies.
This question is just to clarify the logic is very simple
Is 1, X and Y are similar, if X is Y before predators or prey X is Y does not hold
If established, X and Y are the same, X is Y prey prey, predators X is Y predators
To 2:00, eat X Y (X is the natural enemy of Y), if X and Y are the same or X before natural enemies are not set up so Y
If established, X is Y predators, prey X is Y, X is Y predators prey
Further, there is readily obtained in the case lie: X or Y exceeds the limit, so the code is very easy
#include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdio> #include<cmath> #include<queue> using namespace std; int n,k,ans=0;int f[1500005];//f[i]i本身,f[i+n]i的猎物,f[i+2*n]i的天敌 int GF(int x) { if(f[x]==x)return x; f[x]=GF(f[x]); return f[x]; } void Union(int x,int y) { int f1=GF(x),f2=GF(y); if(f1!=f2)f[f1]=f2; }//合并 int main() { scanf("%d %d",&n,&k); for(int i=1;i<=n*3;i++)f[i]=i; for(int i=1;i<=k;i++) { int bj,xx,yy; Scanf ( "% D% D% D", & BJ, & XX, & YY); IF (XX> n-|| YY> n-) {ANS ++; Continue;} // determines whether or not exceed the limit IF (BJ ==. 1) { IF (GF (xx + n) == GF (yy) || GF (xx + 2 * n) == GF (yy)) {ans ++; continue;} // X is Y or X is Y predators prey Union (xx, yy); // X and Y are the same Union (xx + n, yy + n); // X Y prey is a prey Union (xx + 2 * n, yy + 2 * n); // X is Y predators predators } the else IF (BJ == 2) { IF (GF (XX) == GF (YY) || GF (2 * n-XX +) == GF (YY)) {ANS ++; Continue ;} // X and Y are the same or natural enemy X Y of Union (XX, YY * + n-2); // X is Y predators Union (xx + n, yy) ; // X is Y prey of Union (xx + 2 * n, yy + n); // X is Y predators prey } } the printf ( "% D \ n-", ANS); return 0; }