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 and output formats
Input formats:
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 formats:
Output in the eat.out
Line, an integer representing the total number of lies.
Sample input and output
Explanation
1 ≤ N ≤ 5 ∗ 10^4
1 ≤ K ≤ 10^5
Ideas:
Kind of disjoint-set
The open disjoint-set three times, respectively, the same deposit, predators and prey, then it is determined true and false.
Code:
1 #include <iostream>
2 #define R register
3 using namespace std;
4 const int maxn=5*10000+5;
5 int Fa[maxn*4];
6 inline int find(R int x)
7 {
8 if(x==Fa[x]) return x; 9 return Fa[x]=find(Fa[x]); 10 } 11 inline void merge(R int x,R int y) {Fa[find(x)]=find(y);} 12 int main() 13 { 14 R int n,k,ans=0; 15 cin>>n>>k; 16 for(R int i=1;i<=3*n;i++) Fa[i]=i; 17 for(R int i=1;i<=k;i++) 18 { 19 R int d,x,y;cin>>d>>x>>y; 20 if(x>n||y>n) {ans++;continue;} 21 if(d==1) 22 { 23 if(find(x+n)==find(y)||find(x+n+n)==find(y)) {ans++;continue;} 24 merge(x,y),merge(x+n,y+n),merge(x+n+n,y+n+n); 25 } 26 else 27 { 28 if(x==y){ans++;continue;} 29 if(find(x)==find(y)||find(x+n+n)==find(y)) {ans++;continue;} 30 else merge(x+n,y),merge(x,y+n+n),merge(x+n+n,y+n); 31 } 32 } 33 cout<<ans<<endl; 34 return 0; 35 }
Weighted disjoint-set:
The distance to the animal Finding Roots% 3,0,1,2 represent a class of animals
1 #include <iostream> 2 using namespace std; 3 const int maxn=5*10000+5; 4 int A[maxn],B[maxn],C[maxn];int Fa[maxn];int R[maxn]; 5 inline int get(int x) 6 { 7 if(x==Fa[x]) return x; 8 int temp=Fa[x]; 9 Fa[x]=get(Fa[x]); 10 R[x]=((R[x]+R[temp]+3)%3); 11 return Fa[x]; 12 } 13 inline void merge(int x,int y,int p) 14 { 15 int fx=get(x),fy=get(y); 16 Fa[fx]=fy; 17 R[fx]=(R[y]+3-R[x]+p)%3; 18 } 19 int n,k; 20 int main() 21 { 22 cin>>n>>k;int ans=0; 23 for(int i=1;i<=n;i++) Fa[i]=i; 24 bool f1=0; 25 for(int i=1;i<=k;i++) 26 { 27 int p,x,y; 28 cin>>p>>x>>y; 29 if(x>n||y>n){ans++;continue;} 30 if(p==2&&x==y) {ans++;continue;} 31 int fx=get(x),fy=get(y); 32 if(fx==fy){if((R[x]-R[y]+3)%3!=p-1) ans++;} 33 else {merge(x,y,p-1);} 34 } 35 cout<<ans<<endl; 36 return 0; 37 }