10004-bicoloring
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=945
In 1976 the ' Four Color Map theorem ' is proven with the assistance of a computer. This theorem states so every map can be colored using-only four colors, in such a way ' no region is colored using th e same color as a neighbor region.
Here you are are asked to solve a simpler similar problem. You are have to decide whether a given arbitrary connected graph can is bicolored. It, if one can assign colors (from a palette of two) to the nodes in such a way that no two adjacent nodes have the s Ame color. To simplify the problem can assume:
No node would have an edge to itself.
The graph is nondirected. This is, if a node a is said to be connected to a node b, then you must assume this b is Connec Ted to a.
The graph would be strongly connected. That is, there'll be in least one path from any node to the other node.
Input
The input consists of several test cases. Each test case starts with a line containing the number N (1 < n<) of different nodes. The second line contains the number of edges L. After this, L lines'll follow, containing two numbers that specify a edge between the two nodes that they represen T. A node in the graph would be labeled using a number A ( ).
An input with n = 0 would mark the end of the input and am not processed.
Output
You are have to decide whether the input graph can is bicolored or not, and print it as shown below.
Sample Input
3
3
0 1
1 2
2 0 9 8
0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0
Sample Output
Not bicolorable.
Bicolorable.
A simple binary graph to determine the problem, with the adjacency matrix to calculate.
Complete code:
01./*0.025s*/02.
#include <cstdio> #include <cstring> 05.
06.int a[201][201], vist[202], t[202];
07.int N, M, p; 09.int DFS () 10. {One for (int i = 0; i < n; i++) 12. for (int j = 0; J < N; j + +) 13. if (A[i][j]) 14. {if (T[i] && t[j] && vist[i] = = Vist[j]) 16.
return 0; /else if (T[i] &&!t[j]) 18.
{Vist[j] =-vist[i];
T[J] = 1; 21.} 22. else if (!t[i] && t[j]) 23.
{Vist[i] =-vist[j];
T[i] = 1; 26.} 27. } 28.
return 1;
29.} 30. 31.int main (void) 32. {A. while (scanf ("%d%d", &n, &m), N) 34.
{memset (A, 0, sizeof (a));
memset (vist, 0, sizeof (vist));memset (t, 0, sizeof (t)); for (int i = 0; i < m i++) 39.
{. int C, D;
scanf ("%d%d", &c, &d);
a[c][d]++; 43.} 44.
Vist[0] = 1;
T[0] = 1; Puts (Dfs ()?
"Bicolorable.": "Not bicolorable."); 47.} 48.
return 0; An.}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/