-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfoodCycle.cpp
More file actions
56 lines (54 loc) · 936 Bytes
/
foodCycle.cpp
File metadata and controls
56 lines (54 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define N 50001
int pre[N*3];
int findPre(int k){
if(k!=pre[k]){
pre[k]=findPre(pre[k]);
}
return pre[k];
}
void join(int x,int y){
int fx = findPre(x);
int fy = findPre(y);
pre[fx] = fy;
}
int main(){
int n,k;
cin>>n>>k;
for(int i=1;i<=3*n;i++){
pre[i] = i;
}
int total=0;
for(int i=0;i<k;i++){
int tag,x,y;
cin>>tag>>x>>y;
if(x>n||x<1||y>n||y<1||(tag==2&&x==y)){
total++;
continue;
}
if(tag==1){
if(findPre(x)==findPre(y+n)||findPre(x+n)==findPre(y)){
total++;
continue;
}
join(x,y);
join(x+n,y+n);
join(x+2*n,y+2*n);
}else{
if(findPre(x)==findPre(y)||findPre(x+n)==findPre(y)){
total++;
continue;
}
join(x,y+n);
join(x+n,y+2*n);
join(x+2*n,y);
}
}
cout<<total<<endl;
return 0;
}