-
Notifications
You must be signed in to change notification settings - Fork 2
/
Butterfly.cpp
74 lines (66 loc) · 1.13 KB
/
Butterfly.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include <queue>
#include <vector>
#include<list>
#include <algorithm>
#include<iostream>
#include<math.h>
using namespace std;
#define N 100005
#define Inf 0x3f3f3f3f
struct node{
int p;
int r;
}g[N];
int findPre(int k){
if(k==g[k].p){
return k;
}
int y =g[k].p;
int fx = findPre(g[k].p);
g[k].p = fx;
if(g[k].r == g[y].r){
g[k].r = 0;
}else{
g[k].r = 1;
}
return g[k].p;
}
int main() {
int n,m;
while(cin>>n>>m){
for(int i=0;i<n;i++){
g[i].p = i;
g[i].r = 0;
}
bool flag = false;
for(int i=0;i<m;i++){
int x,y,r;
scanf("%d%d%d",&x,&y,&r);
int xp = findPre(x);
int yp = findPre(y);
if(xp==yp){
if((g[x].r==g[y].r&&r==1)||(g[x].r!=g[y].r&&r==0)){
flag = true;
}
}else{
g[xp].p = yp;
if(g[x].r==g[y].r){
g[xp].r = r;
}else{
g[xp].r = (r+1)%2;
}
}
}
if(flag){
cout<<"NO"<<endl;
}else{
cout<<"YES"<<endl;
}
}
return 0;
}