Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9896 | Accepted: 5057 |
Description
Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.
Input
Following the number of repeaters is a list of adjacency relationships. Each line has the form:
A:BCDH
which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form
A:
The repeaters are listed in alphabetical order.
Note that the adjacency is a symmetric relationship; if A is adjacent to B, then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not have any line segments that cross.
Output
Sample Input
2 A: B: 4 A:BC B:ACD C:ABD D:BC 4 A:BCD B:ACD C:ABD D:ABC 0
Sample Output
1 channel needed. 3 channels needed. 4 channels needed.
代码:
#include<stdio.h>
#include<string.h>
int map[27][27];
int color[27];//记录每个点的着色情况;
int n;
int ok(int s) //判断是否满足要求;
{
for(int k=0;k<n;k++)
{
if( map[s][k]&&color[s]==color[k] ) //相连且颜色相同;
return 0;
}
return 1;
}
int DFS(int s,int ni) //开始的位置,颜色的多少;
{
if( s==n ) //所有的点着色成功;
return 1;
for( color[s]=1;color[s]<=ni;color[s]++ ) //假定了ni种颜色;
{
if(!ok(s)) //不满足要求;
continue;
else
return DFS( s+1 , ni ); //对下一个点进行着色;
}
return 0;
}
int main()
{
int i,j;
while(scanf("%d",&n),n)
{
memset(map,0,sizeof(map));
memset(color,0,sizeof(color));
char s[30];
for(i=0;i<n;i++)
{
scanf("%s",s);
int len=strlen(s);
for(j=2;j<len;j++)
map[i][s[j]-'A']=1;
}
for(i=1;i<=4;i++) //四色定理,枚举使用的颜色;
{
if(DFS(0,i))
break;
}
if(i>1)
printf("%d channels needed.\n",i);
else
printf("1 channel needed.\n");
}
return 0;
}