Title Description
animal kingdom there are three types of animals A, B, C, these three animal food chain interesting annular configuration. 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
it in the end is which.
It was carried out with two versions of this trophic animals consisting of N Description:
The first argument is "1 XY", it 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 this sentence some true
, some false. When one of the following three words, this sentence is a lie, the truth is otherwise.
• The current case with some previous conflicts true, then, is to lie
• if the current X or Y than the big N is a lie
• Current eat as saying X X is a lie
Your mission is based on a given N and the total number of K output lies sentence.
Input Output Format
Input Format:
from eat.in data input
two integers of the first row, 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 to eat.out in
a row, an integer representing the total number of lies.
Sample Input Output
Input Sample # 1:
100. 7
1 101 1
2 1 2
2 2. 3
2. 3. 3
1 1. 3
2. 3 1
1. 5. 5
Output Sample # 1:
3
Description
. 1 ≤ N ≤ 10. 5 * ^. 4
. 1 ≤ K ≤ 10. 5 ^
Explanation: The classic check and maintain the relationship between the set, where the use of white paper solution, if X, Y class. We put (X, the Y), (X + N, the Y + N), (X + 2 N, the Y + 2 N) were combined, if X eat Y. We put (X-, the Y + N), (X-N +, the Y + 2 N), (X-2 + N, the Y) were combined, then also determines contradictory Similarly, different number representing different relationships, we inquire the corresponding relationship between the sequence Enough
#include<iostream>
#define N 50001
using namespace std;
int father[3*N]={0};//0 同类,1吃,2被吃
void init(){
for(int i=0;i<3*N;i++) father[i]=i;
}
int find(int x){
if(father[x]==x) return x;
return father[x]=find(father[x]);
}
void merge(int x,int y){
x=find(x);y=find(y);
father[x]=y;
}
bool same(int x,int y){
return find(x)==find(y);
}
int n=0,k=0;
int ret=0;
int ok(int cmd,int x,int y){
if(x>n||y>n) return 1;
if(cmd==1){
if(same(x,y+N)||same(x,y+2*N)) return 1;
merge(x,y);merge(x+N,y+N);merge(x+2*N,y+2*N);
}else{
if(same(x,y)||same(x,y+2*N)) return 1;
merge(x,y+N);merge(x+N,y+2*N);merge(x+2*N,y);
}
return 0;
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>k;
init();
for(int i=1,a,b,c;i<=k;i++){
cin>>a>>b>>c;
ret+=ok(a,b,c);
}
cout<<ret<<endl;
return 0;
}