点击打开链接
//
// main.cpp
// test
//
// Created by 吴有堃 on 2017/9/11.
// Copyright © 2017年 吴有堃. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1e3+5;
int n=0,m=0,Map[maxn][maxn];
int judge[maxn]; ///0,1值,判断染色是否相同
int vis[maxn];//标记点是否访问,决定是否放入队列
struct Node{
int pt;
};
queue<Node>Q;
void BFS()
{
while (!Q.empty()) Q.pop();
memset(judge,-1,sizeof(judge));
memset(vis,0,sizeof(vis));
Node temp1,temp2;
temp1.pt=0;
Q.push(temp1);judge[0]=0;vis[temp1.pt]=1;
while (!Q.empty()) {
temp1=Q.front();Q.pop();
//printf("%d\n",temp1.pt);
for(int i=0;i<n;i++){
if(Map[temp1.pt][i]==0){//相同列
if(judge[i]!=judge[temp1.pt]&&judge[i]!=-1&&judge[temp1.pt]!=-1){
printf("NO\n");
return;
}
judge[i]=judge[temp1.pt];
if(!vis[i]){
temp2.pt=i;
vis[i]=1;
Q.push(temp2);
}
}
else if(Map[temp1.pt][i]==1){///不同列
if(judge[i]==judge[temp1.pt]&&judge[i]!=-1){
printf("NO\n");
return;
}
judge[i]=1-judge[temp1.pt];
if(!vis[i]){
temp2.pt=i;
vis[i]=1;
Q.push(temp2);
}
}
}
}
printf("YES\n");
return;
}
int main()
{
int i=0,a=0,b=0,val=0;
while(scanf("%d %d",&n,&m)!=EOF){
memset(Map, -1, sizeof(Map));
for(i=0;i<m;i++){
scanf("%d %d %d",&a,&b,&val);
Map[a][b]=Map[b][a]=val;
}
BFS();
}
return 0;
}