-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathButterfly2.cpp
More file actions
63 lines (56 loc) · 949 Bytes
/
Butterfly2.cpp
File metadata and controls
63 lines (56 loc) · 949 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
57
58
59
60
61
62
63
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define N 1005
int pre[N*2];
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;
while(cin>>n>>k){
bool flag = false;
for(int i=0;i<2*n;i++){
pre[i] = i;
}
for(int i=0;i<k;i++){
int tag,x,y;
scanf("%d%d%d",&x,&y,&tag);
if(flag){
continue;
}
if(tag==0){
if(findPre(x)==findPre(y+n)||findPre(x+n)==findPre(y)){
flag = true;
}else{
join(x,y);
join(x+n,y+n);
}
}else{
if(findPre(x)==findPre(y)||findPre(x+n)==findPre(y+n)){
flag = true;
}else{
join(x,y+n);
join(x+n,y);
}
}
}
if(flag){
cout<<"NO"<<endl;
}else{
cout<<"YES"<<endl;
}
}
return 0;
}