/*
极简单一题,但是没有解出。
题目要求:图是强连通的,无向
要求对图中节点染色,只有两种颜色可以选择
判断是否存在一种情况使相邻的两节点颜色都不同
思路:
深度优先搜索
*/
#include <cstdio>
#include <cstring>
int n,m;
bool graph[210][210];
int color[210];
bool dfs(int k,int c)
{
for(int i=1;i<=n;i++)
{
if(graph[k][i]==1)
{
if(color[i]==c)
return false;
else if(color[i]==-1)
{
color[i]=1-c;
if(!dfs(i,1-c))
return false;
}
}
}
return true;
}
int main()
{
//freopen("data.in","r",stdin);
int u,v;
while(1)
{
scanf("%d",&n);
if(n==0)
break;
memset(graph,false,sizeof(graph));
memset(color,-1,sizeof(color));
scanf("%d",&m);
if(m==0)
break;
for(int i=0;i<m;i++)
{
scanf("%d %d",&u,&v);
u++;v++;
graph[u][v]=graph[v][u]=1;
}
color[1]=0;
//the graph will be strongly connected
if(!dfs(1,0))
printf("NOT BICOLORABLE.\n");
else
printf("BICOLORABLE.\n");
}
return 0;
}
10004 - Bicoloring***
最新推荐文章于 2025-11-27 14:26:45 发布
本文探讨了一道关于图染色的经典问题,要求在一个强连通的无向图中使用两种颜色进行染色,并确保相邻节点颜色不同。文章通过深度优先搜索(DFS)的方法实现了这一目标,并提供了完整的代码实现。
1098

被折叠的 条评论
为什么被折叠?



