-
Notifications
You must be signed in to change notification settings - Fork 84
/
tyama_PKU1182.cpp
48 lines (48 loc) · 1.03 KB
/
tyama_PKU1182.cpp
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
#include <vector>
#include <set>
#include <cstdio>
using namespace std;
class union_find{
int *parent,*rank,n;
public:
int root(int a){return parent[a]==a?a:(parent[a]=root(parent[a]));}
union_find(int _n){n=_n;parent=new int[n];rank=new int[n];for(int i=0;i<n;i++)parent[i]=i,rank[i]=0;}
~union_find(){delete []parent;delete []rank;}
int same(int a,int b){return root(a)==root(b);}
int unite(int a,int b){
int x=root(a),y=root(b);if(x==y)return 0;
if(rank[x]<rank[y]){
parent[x]=y;
}else{
parent[y]=x;
if(rank[x]==rank[y])rank[x]++;
}
return 1;
}
};
int main(){
int N,K,R=0;
scanf("%d%d",&N,&K);
union_find uf(3*N);
for(;K--;){
int t,x,y;
scanf("%d%d%d",&t,&x,&y),x--,y--;
if(x<0||N<=x || y<0||N<=y){R++;continue;}
if(t==1){
if(uf.same(x,y+N) || uf.same(x,y+N*2))R++;
else{
uf.unite(x,y);
uf.unite(x+N,y+N);
uf.unite(x+N*2,y+N*2);
}
}else{
if(uf.same(x,y) || uf.same(x,y+N*2))R++;
else{
uf.unite(x,y+N);
uf.unite(x+N,y+N*2);
uf.unite(x+N*2,y);
}
}
}
printf("%d\n",R);
}