2013
11-09

# Channel Allocation

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.

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.

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.

2
A:
B:
4
A:BC
B:ACD
C:ABD
D:BC
4
A:BCD
B:ACD
C:ABD
D:ABC
0

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

import java.util.*;
import java.io.*;
/*
*【题意简述】信道分配。给定一个无向图，为每顶点填上颜色，要求满足相邻的顶点颜色不同，
* 问最少的颜色数是多少?
*【分析】利用四色定理，直接枚举颜色数+DFS，其中DFS是暴力枚举每个顶的颜色，以便找到一个可行解。
*/
public class Main{

public static int[][] g=new int[26][26];

public static int solve(int n){
int i,j,cnum;
boolean tag=true;
// 无边图只用1色即可
for(i=0;i< n && tag;i++){
for(j=i+1;j< n && tag;j++){
if(g[i][j]==1)
tag=false;
}
}
if(tag)
return 1;

for(cnum=2;cnum<=4;cnum++)    // 枚举答案+dfs
{
int[] x=new int[n];
Arrays.fill(x,-1);
if(DFS(x,0,cnum,n))
return cnum;
}
return -1;
}

//DFS的复杂度是颜色数^顶点数(4^26，其中可行性剪枝剪掉了很多分支)
public static boolean DFS(int[] x,int vnum, int cnum,int n){
if(vnum == n) return true;    // v的顶点都上色，可行解
for(int i=0;i< cnum;i++){    // 如果某个顶点没有颜色填，返回上一层
x[vnum] = i;
if(check(vnum,x,i,n))
if(DFS(x,vnum+1,cnum,n)) // 合法，枚举下一个顶点
return true;
}
return false;
}

// 判断相邻的顶点是否有涂过这种颜色
public static boolean check(int vnum,int[] x,int t,int n){
boolean find=true;
for(int i=0;i< n && find;i++){
if(g[vnum][i]==1 && x[i]==t)
find=false;
}
return find;
}

public static void main(String rgs[]) throws Exception
{
Scanner cin = new Scanner(new BufferedInputStream(System.in));
int i,j,n=cin.nextInt();
while(n!=0){
for(i=0;i< n;i++)
Arrays.fill(g[i],0);
for(i=0;i< n;i++){
String s = cin.next();
for(j=2;j< s.length();j++)
g[i][s.charAt(j)-'A']=1;
}
int count=solve(n);
if(count==1)
System.out.println(count+" channel needed.");
else
System.out.println(count+" channels needed.");
n=cin.nextInt();
}
}
}