Topic description
There are three groups of animals A, B, and C in the animal kingdom, and the food chains of these three groups form an interesting loop. A eats B, B
Eat C, C eat A.
There are N animals, numbered 1-N. Every animal is one of A,B,C, but we don't know
Which one is it.
Some people describe the food chain relationship formed by these N animals in two ways:
The first statement is "1 XY", which means that X and Y are of the same kind.
The second is "2 XY", which means X eats Y.
This person uses the above two statements to say K sentences sentence after sentence to N animals. Some of these K sentences are true.
Yes, some are fake. When a sentence satisfies one of the following three conditions, the sentence is false, otherwise it is true.
• The current words conflict with some previous true words and are false words
• If X or Y is greater than N in the current word, it is a lie
• The current word means X eats X, which is a lie
Your task is to output the total number of false statements given N and K sentences.
Input and output format
Input format:
Enter data from eat.in
The first line has two integers, N, K, indicating that there are N animals, K sentences.
The second line starts with one sentence per line (according to the requirements of the title, see the example)
Output format:
output to eat.out
One line, an integer, representing the total number of falsehoods.
Input and output example
illustrate
1 ≤ N ≤ 5 ∗ 10^4
1 ≤ K ≤ 10^5
Ideas: The idea of this question is basically the same as that of detaining criminals. We will use three arrays (here I use an array for convenience, assuming i is itself, then i+n is his prey, and i+n*2 is his enemy) When x and y are of the same kind, we only need to judge whether y is the prey of x, and whether y is the enemy of x, if not, then this is not a lie, if they are directly merged, their enemy Merged, their prey merged. The same can be obtained......
Code:
#include<iostream> #include <cstdio> #define X 300000+7 using namespace std; int tot,fa[X]; // fa[i] refers to itself, fa[i+n] is what he can beat, fa[i+n*2] is to eat his int read() { int sum=0; char ch=getchar(); while(ch>'9'||ch<'0')ch=getchar(); while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=getchar(); return sum; } int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); } int hb(int x,int y) { int ff=find(x),fff=find(y); fa [ff] = fff; } intmain () { int n,k; n=read(); k=read(); for(int i=1;i<=n*3;++i) fa[i]=i; for(int i=1,x,u,v;i<=k;++i) { x=read();u=read();v=read(); if(u>n||v>n){ ++tot; continue; }; int ff=find(v); if(x==1) { if(find(u+n)==ff||find(u+n*2)==ff){ ++tot; continue; } hb(u,v);hb(u+n,v+n);hb(u+n*2,v+n*2); } else { if(u==v){ ++tot; continue; } if(find(u)==ff||find(u+2*n)==ff){ ++tot; continue; } hb(u,v+2*n);hb(u+n,v);hb(u+2*n,v+n); } } cout << tot; }