| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 13152 | Accepted: 6721 |
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.
Source
#include <cstdio>
#include <cstring>
int n;
int a[30][30],c[30];
char s[30];
bool p;
int col;
bool ok(int x)
{
for(int i=0;i<n;i++)
{
if(a[x][i] && c[x]==c[i])
return false;
}
return true;
}
void dfs(int x)
{
if(p)
return;
if(x>=n)
{
p=true;
return ;
}
for(int j=1;j<=col;j++)
{
c[x]=j;
if(ok(x))
{
dfs(x+1);
if(p)
return ;
}
c[x]=0;
}
}
int main()
{
while(scanf("%d",&n)!=EOF && n)
{
p=false;
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
for(int i=0;i<n;i++)
{
scanf("%s",s);
int m=strlen(s);
int x=s[0]-'A';
for(int j=2;j<m;j++)
{
int y=s[j]-'A';
a[x][y]=1;
a[y][x]=1;
}
}
for(col=1;col<=4;col++)
{
dfs(0);
if(p)
break;
}
if(col==1)
printf("1 channel needed.\n");
else
printf("%d channels needed.\n",col);
}
return 0;
}
794


&spm=1001.2101.3001.11974&articleId=46855285&d=1&t=3&u=ccb56e9950624c2f9bde376456c697e9)

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



