POJ1129---Channel Allocation(dfs+四色猜想)

                                            Channel Allocation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19810   Accepted: 9946

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

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. 

题意不是很难,有n个广播电台,告诉你他们是否连接,规定:连接的电台不能使用同一种调频信号,不然会相互干扰,问你最少使用几种调频信号可以使得广播电台之间不受干扰

这个是表面的意思,可以通过贪心来解出最终答案,样例过了但是未能AC。。。

其实这道题可以转换为高中数学上学过的图形染色问题(一个图形由几部分组成,相邻的两个部分不能使用同一种颜色,问你最少可以使用多少颜色可以染遍全图)最少的不一定,但是最多的知道,嘻嘻,这就是传说中的世界近代三大数学难题之一的四色猜想,即四种颜色就够了,虽然说四色猜想至今没有被信服性的证明,但是大量的实践证明他是正确的,所以是可以用的。

首先要存图,因为数据量最大才26,所以可以使用最简单暴力的邻接矩阵来存,至于枚举,可以使用循环,但是要用到的变量会多而复杂,还是使用dfs吧。

搜索的思想是 枚举每一个点,然后去查找与它相邻接的所有点的颜色是否与将要染的颜色相同,如果相同就换一种颜色,不相同就染上再继续染,直到全部染完为止,寻找最大的色号即可

注意单词单复数的格式化输出~~~

#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
inline int _read() {
    char ch = getchar();
    int sum = 0;
    while (!(ch >= '0' && ch <= '9'))ch = getchar();
    while (ch >= '0' && ch <= '9')sum = sum * 10 + ch - 48, ch = getchar();
    return sum;
}
const int inf=0x3f3f3f3f;
const int mm=30;

int n;
string str;
bool mp[mm][mm];
int color[mm];
int res;

void dfs(int v){//给编号为v的点染色
	if(v>n){
		int maxx=-1;
		for(int i=1;i<=n;i++)
			if(color[i]>maxx)
				maxx=color[i];
		if(maxx<res)
			res=maxx;
		return ;
	}
	for(int i=1;i<=4;i++){ 
		bool flag=true;
		for(int j=1;j<v;j++)
			if(mp[v][j]&&color[j]==i){
				flag=false;
				break;
			}
		if(!flag)
			continue;
		color[v]=i;
		dfs(v+1);
	}	
}

int main()
{
	while(scanf("%d",&n)&&n){
		mem(mp,false);
		for(int i=1;i<=n;i++){
			cin>>str;
			for(int j=2;j<str.length();j++)
				mp[str[0]-'A'+1][str[j]-'A'+1]=true;
		}
		color[1]=1;
		res=4;
		dfs(2);
		if(res==1)
			printf("1 channel needed.\n");
		else 
			printf("%d channels needed.\n",res);
	}

	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章