您的位置:首页 > 其它

POJ 1129

2012-07-01 11:30 246 查看
此题的本质问题就是对无向图的着色,使颜色使用最少的问题

#include <stdio.h>
#define maxn 27
typedef struct Node
{
int next[maxn];
int tot;
}node;
int main()
{
int n;
while(scanf("%d",&n)&&n!=0)
{
getchar();//吸收回车
node map[maxn];
int i,j,k;
for(i=0;i<n;i++)//建图
{
map[i].tot=0;
getchar();//吸收每行的首字母,因为是字典序输入
getchar();//吸收冒号
char c;
while((c=getchar())!='\n')
{
int t=c-'A';
map[i].next[map[i].tot++]=t;
}
}
int color[maxn]={0};//每个节点的颜色
int mc=1;//所用颜色总数
for(i=0;i<n;i++)//对图的节点进行染色
{
int visit[maxn]={0};//标记邻节点的颜色
for(j=0;j<map[i].tot;j++)
{
if(color[j])
visit[color[j]]=1;
}
for(k=1;k<maxn;k++)
{
if(!visit[k])
{
color[i]=k;
break;
}
}
if(color[i]>mc)
mc=color[i];
}
if(mc==1)
printf("1 channel needed.\n");
else
printf("%d channels needed.\n",mc);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struct ini c