/*
* 二分图判断,染色法,任意找一个未染色的点A,染色,然后给所有相邻的点染上相反的颜色
* 不是二分图的充要条件 : 如果相邻的点已经染了色,而且颜色跟点A相同
* 实现方式 BFS , 邻接矩阵
* 序号1 ~ n 注意初始化
*/
const int SIZE = 200;
int Graph[SIZE][SIZE];
int color[SIZE];
bool BFS( int i,int n ){
queue<int> que;
que.push(i);
color[i] = 1;
while( !que.empty() ){
int st = que.front();
que.pop();
for (int i = 1;i <= n;++i ){
if ( Graph[st][i] == 1 ){
if( color[i] == -1 ){
color[i] = !color[st];
que.push(i);
}
if ( color[i] == color[st] )
return false;
}
}
}
return true;
}
bool just( int n ){
for ( int i = 1;i <= n;++i )
if ( color[i] == -1 && BFS(i,n) == 0 )
return false;
return true;
}
void init(){
memset(Graph,0,sizeof(Graph));
memset(color,-1,sizeof(color));
}
图论 二分图判断模板
最新推荐文章于 2020-03-12 23:42:38 发布