由于题目对输入的图给了比较严格的规定:总是连通的,无向的,无自环的。所以使用简单的dfs可以水过~ #include<stdio.h> #include<string.h> #include<vector> using namespace std; const int maxn = 210; vector<int> g[maxn]; int color[maxn]; int n, m; bool dfs(int cur) { for(int i = 0; i < g[cur].size(); i++) { int nxt = g[cur][i]; if(color[nxt] == -1) { color[nxt] = 1 - color[cur]; if(dfs(nxt) == false) return false; } else if(color[nxt] == color[cur]) return false; } return true; } #define LOCAL int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif while(scanf("%d", &n), n != 0) { memset(color, -1, sizeof(color)); for(int i = 0; i < n; i++) g[i].clear(); scanf("%d", &m); int a, b; for(int i = 0; i < m; i++) { scanf("%d%d", &a, &b); g[a].push_back(b); g[b].push_back(a); } color[0] = 0; if(dfs(0)) printf("BICOLORABLE./n"); else printf("NOT BICOLORABLE./n"); } return 0; }