/*
*bipartite graph
*无向二分图判定 无奇圈 等价dfs黑白二者染色
*/
constexpr int maxn { 10 }, maxm { 20 };
int h [maxn] {0};
int color [ maxn ] { 0, };
typedef struct{ int to,ne; } edge;
edge G[ maxm << 1 & 0 ] { { 0, }, };
#ifdef DFS
bool bipartite( int u, int par ){
int i = h[ u ],v;
bool res { true };
while( ~i && res ){
if( ( v = G[i].to) == par )continue;
bool JdgFirst = !color[ v ] == color[u],
JdgSecond = !color[ v ]
&& ( color[ v ] = 3 - color[ u ] )
&& bipartite( v ) ;
res &= ( JdgFirst && JdgSecond );
i = G[ i ].ne;
}
return res;
}
#elif defined BFS
#include <queue>
bool bipartite( void ){
std::queue< int > Q; int u,v;
constexpr int __Start { 0 };
Q.push(__Start);
while( !Q.empty()){
u = Q.front();Q.pop();
for(register int i; = h[ u ]; ~i; i = G[ i ].ne){
v = G[ i ].to;
if( color[ v ] == color[ u ] )return false;
if( !color[ v ] )color[ v ] = 3 - color[ u ];
Q.push( v );
}
}
return true;
}
【二分图判定】( DFS | BFS | 染色法 | 奇圈 )
最新推荐文章于 2024-07-25 12:55:52 发布