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;
}

 

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