首页 > 代码库 > Channel Allocation

Channel Allocation

Description

When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a strong signal. However, the channels used by each repeater must be carefully chosen so that nearby repeaters do not interfere with one another. This condition is satisfied if adjacent repeaters use different channels. 

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

The input consists of a number of maps of repeater networks. Each map begins with a line containing the number of repeaters. This is between 1 and 26, and the repeaters are referred to by consecutive upper-case letters of the alphabet starting with A. For example, ten repeaters would have the names A,B,C,...,I and J. A network with zero repeaters indicates the end of 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

For each map (except the final one with no repeaters), print a line containing the minumum number of channels needed so that no adjacent channels interfere. The sample output shows the format of this line. Take care that channels is in the singular form when only one channel is required.

Sample Input

2A:B:4A:BCB:ACDC:ABDD:BC4A:BCDB:ACDC:ABDD:ABC0

Sample Output

1 channel needed.3 channels needed.4 channels needed. 

Source

Southern African 2001
 
 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 const int ms=26; 7 int n; 8 int b[ms]; 9 int c[ms];10 int edge[ms][ms];11 void input()12 {13     char str[ms+3];14     int i,j;15     memset(edge,0,sizeof(edge));16     memset(c,-1,sizeof(c));17     for(i=0;i<n;i++)18     {19         scanf("%s",str);20         int len=strlen(str);21         for(j=2;j<len;j++)22         {23             edge[str[0]-A][str[j]-A]=1;24             edge[str[j]-A][str[0]-A]=1;25         }26     }27 28 }29 void solve()30 {31     int i,j,ans=0;32     for(i=0;i<n;i++)33     {34         memset(b,0,sizeof(b));35         for(j=0;j<n;j++)36         {37             if(edge[i][j]&&c[j]!=-1)38                 b[c[j]]=1;39         }40         for(j=0;j<=i;j++)41             if(b[j]==0)42                 break;43         c[i]=j;44     }45     for(i=0;i<n;i++)46         if(ans<c[i])47             ans=c[i];48     ans++;49     if(ans==1)50         printf("%d channel needed.\n",ans);51     else52         printf("%d channels needed.\n",ans);53     return ;54 }55 int main()56 {57     while(scanf("%d",&n)==1&&n)58     {59         input();60         solve();61     }62     return 0;63 }

 

Channel Allocation