二分圖判斷(dfs)
阿新 • • 發佈:2018-11-21
二分圖著色法判斷
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; const int maxn = 210; int map[maxn][maxn] , color[maxn]; int V , e , sta , last; bool dfs(int v ,int c) { color[v] = c; for(int i = 0 ; i < V ; i ++) { if(map[v][i] == 1 && color[i] == c) return false; if(map[v][i] == 1 && color[i] == 0 && !dfs(i,-c)) return false; } return true; } int main(void) { while(~scanf("%d %d",&V,&e)) { memset(map,0,sizeof(map)); memset(color,0,sizeof(color)); for(int i = 1 ; i <= e ; i ++) { scanf("%d %d",&sta, &last); map[sta][last] = 1; map[last][sta] = 1; } if(!dfs(0,1)) printf("NOT BICOLORABLE.\n"); else printf("BICOLORABLE.\n"); } return 0; }