#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 105;
int color[maxn];
vector<int>G[maxn];
bool bipartite( int u ) //起点
{
for( int i = 0; i < G[u].size(); i ++ )
{
int v = G[u][i];
if( color[v] == color[u] ) //结点已着色 且和结点u颜色冲突
return false;
if( !color[v] )
{
color[v] = 3 - color[u];
if( !bipartite(v) )
return false;
}
}
return true;
}
int main()
{
int n,m;
int a,b;
scanf("%d%d",&n,&m);
memset(color,0,sizeof(color));
color[1]=1; //起始点先标记为黑色
for( int i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
if( bipartite(1) )
puts("YES");
else
puts("NO");
}
判断是否为二分图 染色法
最新推荐文章于 2022-04-06 00:36:29 发布