Analysis:Because this question only gives two colors, we can use BFS as an opportunistic method to determine that the level of two knots in wood is the same. But it seems like this is the case.Four color map theorem,Therefore, four colors can be dropped. Three colors are considered. Who can tell me! (Think about it in the evening, okay)
The following is an example of ACCode:
# Include <cstdio> # include <cstring> using namespace STD; const int maxn = 210; int map [maxn] [maxn]; int nodelevel [maxn]; bool vis [maxn] [maxn]; bool vis [maxn]; int Q [maxn * maxn]; int n, m; void BFS (INT s) {int front = 1; int rear = 1; Q [Front] = s; vis [s] = true; while (front <= rear) {int v = Q [Front ++]; for (INT I = 0; I <n; I ++) {If (Map [v] [I]) {If (! Vis [I]) {vis [I] = true; vis [v] [I] = true; nodelevel [I] = nodelevel [v] + 1; Q [++ rear] = I;} If (nodelevel [v] = nodelevel [I] &! Vis [I] [v]) {printf ("not bicolorable. \ n "); Return ;}}} printf (" bicolorable. \ n "); return;} int main () {While (scanf (" % d ", & N) {memset (VIS, false, sizeof (VIS); memset (VIS, false, sizeof (VIS); memset (MAP, 0, sizeof (MAP); memset (nodelevel, 0, sizeof (nodelevel); scanf ("% d", & M); For (INT I = 0; I <m; I ++) {int p, q; scanf ("% d", & P, & Q); map [p] [Q] = map [Q] [p] = 1 ;} BFS (0);} return 0 ;}