Question:
In 1976 the ''four Color Map Theorem "was proven with the degree of a computer. this theorem states that every map can be colored using only four colors, in such a way that no region is colored using the same color as a neighbor region.
Here you are asked to solve a simpler similar problem. you have to decide whether a given arbitrary connected graph can be bicolored. that is, if one can assign colors (from a palette of two) to the nodes in such a way that no two adjacent nodes have the same color. to simplify the problem you can assume:
No node will have an edge to itself.
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.
The graph will be strongly CTED. That is, there will be at least one path from any node to any other node.
Question Translation:
In 1976, the "four-color theorem" was proved with the help of computers. This theorem states that any map can be filled with only four colors, and the colors of no adjacent areas are the same.
Now let you solve a simpler problem. You must decide whether the given connected graph can be filled with two colors. That is to say, if one of the nodes is assigned a color, the two nodes directly connected must not be in the same color. To make the problem simpler, you can assume that:
1. No node is connected to itself.
2. It is an undirected graph. That is, if a is connected to B, B is connected to.
3. The figure is strongly connected. That is to say, at least one path can go to all nodes.
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.
Analysis and Summary:
Method 1: Search BFS
From the question, we can see that for each node, all the points connected to it must be different from the color of this point. Naturally, you can use wide search: select one of them, assign a color to this point, replace it with a number 0, and then perform wide search, then all neighboring points can be assigned another color, which can be replaced by 1. In this case, if a vertex has been assigned a value, you can determine whether the existing value is the same as the value to be given to it. If it is the same, continue. If they are different, it cannot be determined directly.
BFS code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Define maxn210
Using namespace std;
Int n, m, a, B, G [MAXN] [MAXN], lastPos;
Int vis [210], edge [250] [2];
Bool flag;
Int que [100000];
Void bfs (int pos ){
Int front = 0, rear = 1;
Que [0] = pos;
While (front <rear ){
Int m = que [front ++];
For (int I = 0; I <n; ++ I ){
If (G [m] [I]) {
If (! Vis [I]) {
Que [rear ++] = I;
Vis [I] = vis [m] + 1;
}
Else if (vis [I] = vis [m]) {
Flag = true;
Return;
}
}
}
}
}
Int main (){
# Ifdef LOCAL
Freopen ("input.txt", "r", stdin );
# Endif
While (~ Scanf ("% d", & n ){
Memset (G, 0, sizeof (G ));
Scanf ("% d", & m );
For (int I = 0; I <m; ++ I ){
Scanf ("% d", & a, & B );
G [a] [B] = 1;
G [B] [a] = 1;
}
Memset (vis, 0, sizeof (vis ));
Vis [0] = 1;
Flag = false;
Bfs (0 );
If (flag) printf ("not bicolorable. \ n ");
Else printf ("BICOLORABLE. \ n ");
}
Return 0;
}
Method 2: Deep Search for DFS
Similarly, this question can also be done through deep search. The basic idea of deep search is to continue searching in one direction without any further dyeing. The current color is the opposite of the previous one. If you find a dyed color (that is, a loop), you can also determine whether the existing color is consistent with the color given to it. If they are inconsistent, the system determines that they are not allowed.
DFS code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Define maxn210
Using namespace std;
Int n, m, a, B, G [MAXN] [MAXN], lastPos;
Int vis [210];
Bool flag;
Void dfs (int pos ){
If (flag) return;
For (int I = 0; I <n; ++ I ){
If (G [pos] [I]) {
If (vis [I] =-1 ){
Vis [I] =! Vis [pos];
Dfs (I );
Vis [I] =-1;
}
Else if (vis [I]! =! Vis [pos]) {
Flag = true;
Return;
}
}
} Www.2cto.com
}
Int main (){
# Ifdef LOCAL
Freopen ("input.txt", "r", stdin );
# Endif
While (~ Scanf ("% d", & n ){
Memset (G, 0, sizeof (G ));
Scanf ("% d", & m );
For (int I = 0; I <m; ++ I ){
Scanf ("% d", & a, & B );
G [a] [B] = 1;
G [B] [a] = 1;
}
Flag = false;
Memset (vis,-1, sizeof (vis ));
Vis [0] = 0;
Dfs (0 );
If (flag) printf ("not bicolorable. \ n ");
Else printf ("BICOLORABLE. \ n ");
}
Return 0;
}
Author: shuangde800