判断二分图的染色法

Posted Alan_Lin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了判断二分图的染色法相关的知识,希望对你有一定的参考价值。

用三种颜色染色,无色-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 }

 

以上是关于判断二分图的染色法的主要内容,如果未能解决你的问题,请参考以下文章