POJ 1129

此题的本质问题就是对无向图的着色,使颜色使用最少的问题

#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;
}


 

原文地址:https://www.cnblogs.com/lj030/p/3002321.html