#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
vector <int> node[203];
int red[203], green[203];
void bfs(int src){
memset(red, 0, sizeof(red));
memset(green, 0, sizeof(green));
queue <int> container;
container.push(src);
int flag = 0;
int visited[203] = {0};
while(!container.empty()){
int u = container.front();
visited[u] = 1;
for(int i = 0; i < node[u].size(); i++){
int v = node[u][i];
if(red[u] == 0){
if(green[u] == 0)red[u] = 1;
}
if(red[u] == 1){
if(red[v] == 0 && green[v] == 0)green[v] = 1;
}
if(green[u] == 1){
if(red[v] == 0 && green[v] == 0)red[v] = 1;
}
if(red[u] == 1 && red[v] == 1)flag = 1;
if(green[u] == 1 && green[v] == 1)flag = 1;
if(!visited[v]){
visited[v] = 1;
container.push(v);
}
}
container.pop();
}
if(flag == 1)cout << "NOT BICOLORABLE." << endl;
else cout << "BICOLORABLE." << endl;
}
int main(){
int N, E, x, y;
while(cin >> N ){
if(N == 0)break;
cin >> E;
for(int i = 0; i < E; i++)cin >> x >> y,node[x].push_back(y), node[y].push_back(x);
bfs(x);
for(int i = 0; i <= N; i++)node[i].clear();
}
return 0;
}
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
vector <int> node[203];
int red[203], green[203];
void bfs(int src){
memset(red, 0, sizeof(red));
memset(green, 0, sizeof(green));
queue <int> container;
container.push(src);
int flag = 0;
int visited[203] = {0};
while(!container.empty()){
int u = container.front();
visited[u] = 1;
for(int i = 0; i < node[u].size(); i++){
int v = node[u][i];
if(red[u] == 0){
if(green[u] == 0)red[u] = 1;
}
if(red[u] == 1){
if(red[v] == 0 && green[v] == 0)green[v] = 1;
}
if(green[u] == 1){
if(red[v] == 0 && green[v] == 0)red[v] = 1;
}
if(red[u] == 1 && red[v] == 1)flag = 1;
if(green[u] == 1 && green[v] == 1)flag = 1;
if(!visited[v]){
visited[v] = 1;
container.push(v);
}
}
container.pop();
}
if(flag == 1)cout << "NOT BICOLORABLE." << endl;
else cout << "BICOLORABLE." << endl;
}
int main(){
int N, E, x, y;
while(cin >> N ){
if(N == 0)break;
cin >> E;
for(int i = 0; i < E; i++)cin >> x >> y,node[x].push_back(y), node[y].push_back(x);
bfs(x);
for(int i = 0; i <= N; i++)node[i].clear();
}
return 0;
}
No comments:
Post a Comment