#include <stdio.h> #include <stdbool.h> #include <string.h> bool path[201][201]; bool DFS(int pointColor[], int i, int n, int color){ int change_color = color ^ 3; // 1 ^ 3 = 2 ; 2 ^ 3 = 1 (In binary and it's a loop) bool answer = true; if(pointColor[i]){ if(pointColor[i] != color) return false; if(pointColor[i] == color) return true; } pointColor[i] = color; for(int j = i + 1; j < n; j++){ if(path[i][j]) answer &= DFS(pointColor, j, n, change_color); // &= means AND logic gate for left and right so if 1 &= 0 -> 0 0 and so on. } return answer; } /* void printPath(int n){ printf("\n"); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ printf("%d ", path[i][j]); } printf("\n"); } printf("\n"); } */ int main(){ int n, l, n1, n2; int pointColor[201]; while(scanf("%d", &n) != EOF && n != 0){ // The input consists of several test cases. // Each test case starts with a line containing the number n // An input with n = 0 will mark the end of the input and is not to be processed. scanf("%d", &l); // after this, l lines will follow memset(pointColor, 0, sizeof(pointColor)); // init the point memset(path, false, sizeof(path)); // init the path // each containing two numbers that specify an edge // between the two nodes that they represent. for(int i = 0; i < l; i++){ scanf("%d%d", &n1, &n2); path[n1][n2] = true; // the graph is nondirected. That is, // if a node a is said to be connected to a node b, // then you must assume that b is connected to a. path[n2][n1] = true; } //printEdge(n); // check edges if(DFS(pointColor, 0, n, 1)) printf("BICOLORALBE.\n"); else printf("NOT BICOLORABLE.\n"); } return 0; } |
3 3 0 1 1 2 2 0 |