Bicoloring
代码:
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;
int path[205][205];
int vis[205], m, n;
bool col[205];
bool bfs(){
queue <int> que;
que.push(0);
vis[0] = 1;
col[0] = true;
while (!que.empty()){
int t = que.front();
que.pop();
for (int i = 0; i < n; i++){
if (path[t][i]){
if (!vis[i]){
que.push(i);
vis[i] = 1;
col[i] = !col[t];
}
else{
if (col[i] == col[t])
return false;
}
path[t][i] = 0;
}
}
}
return true;
}
int main(){
while (scanf("%d", &n) && n != 0){
memset(path, 0, sizeof(path));
memset(vis, 0, sizeof(vis));
memset(col, 0, sizeof(col));
int t1, t2;
scanf("%d", &m);
for (int i = 0; i < m; i++){
scanf("%d%d", &t1, &t2);
path[t1][t2] = 1;
}
if (bfs())
printf("BICOLORABLE.\n");
else
printf("NOT BICOLORABLE.\n");
}
return 0;
}