poj1129 Channel Allocation
Channel Allocation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14361 | Accepted: 7311 |
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.
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.
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
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
无向图染色问题。如果缺乏有关染色的知识,可以从这样的角度思考:求解的染色顺序对结果没有影响,这就意味这我们可以用循环来解决这个问题。只需枚举点兵观察与当前点相邻并且已经染色了的点的颜色就可以求出该点的颜色,不需要考虑未染色的邻点。
15859881 | ksq2013 | 1129 | Accepted | 696K | 0MS | G++ | 956B | 2016-08-01 09:52:17 |
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> using namespace std; int n,mxn,col[27]; bool g[27][27],vis[27]; inline void read() { mxn=1; memset(g,0,sizeof(g)); memset(col,0,sizeof(col)); getchar(); for(int i=1;i<=n;i++){ char ch; getchar();getchar(); while((ch=getchar())!='\n'){ int u=i,v=ch-'A'+1; g[u][v]=g[v][u]=1; } } } inline void solve() { col[1]=1; for(int i=1;i<=n;i++){ memset(vis,0,sizeof(vis)); for(int j=1;j<=n;j++) if(g[i][j]) vis[col[j]]=1; col[i]=n+1; for(int k=1;k<=n;k++) if(!vis[k]) col[i]=min(col[i],k); mxn=max(mxn,col[i]); } } int main() { while(scanf("%d",&n)&&n){ read(); solve(); if(mxn>1)printf("%d channels needed.\n",mxn); else puts("1 channel needed."); } return 0; }