判断二分图的染色法

用三种颜色染色,无色-0,黑色-1,白色-2。

满足DFS框架,很好用^.^

 1 #include<stdio.h>
 2 #include<string.h>
 3 int color[maxn];
 4 bool bipartite(int u){
 5     for(int i=0;i<G[u].size();i++)
 6     {
 7         int v=G[u][i];
 8         if(color[v]==0){
 9             color[v]=3-color[u];
10             if(!bipartite(v))
11                 return false;
12         }
13         else{
14             if(color[v]==color[u]){
15                 return false;
16             }
17         }
18     }
19     return true;
20 }

 

posted @ 2016-05-18 19:54  Alan2  阅读(180)  评论(0编辑  收藏  举报