国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ 1129 Channel Allocation (枚舉)

2019-11-11 01:34:14
字體:
來源:轉載
供稿:網友

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.

題意

給出一張圖,問至少需要幾種顏色才能給這張圖染色,其中相鄰點不能有相同的顏色。

思路

根據四色定理得出,答案最大是4。

首先對一點著色,然后把它相鄰的點置為其他顏色(保證他們之間相鄰的也是不同顏色)。

同理,對于其他沒有進行染色的點也執行該種操作,在染色的過程中要保證顏色數盡可能少,具體看代碼。

AC 代碼

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#include<vector>vector<int>G[26];int n,color[26]; //每個點的顏色void solve(){ memset(color,0,sizeof(color)); //初始化無染色 int ans=1; for(int ni=0; ni<n; ni++) //枚舉每一個點 { if(color[ni]==0) //如果當前點沒有染色 { bool isc[5]= {false}; //根據四色原理,最大有四種顏色 for(int i=0; i<(int)G[ni].size(); i++) //枚舉臨接的點并標記它的顏色 isc[color[G[ni][i]]]=true; for(int i=1; i<=4; i++) //找出一個與相鄰點不同的顏色 if(isc[i]==false) { color[ni]=i; //著色 ans=max(ans,i); //當前所使用的最大顏色種數 break; } } } printf(ans==1?"%d channel needed./n":"%d channels needed./n",ans);}int main(){ char c[105]; while(~scanf("%d%*c",&n)&&n) { for(int ni=0; ni<n; ni++) { gets(c); G[ni].clear(); //一開始沒加這個,WA過 for(int i=2; i<(int)strlen(c); i++) G[ni].push_back(c[i]-'A'); } solve(); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵宝市| 祁阳县| 应城市| 遂溪县| 宁强县| 彝良县| 郸城县| 通化县| 赣榆县| 丹巴县| 浦北县| 北碚区| 平利县| 昆明市| 保山市| 邓州市| 明星| 富平县| 启东市| 乐亭县| 沁源县| 城口县| 股票| 莆田市| 台南市| 栖霞市| 蓬安县| 右玉县| 高雄市| 蓝田县| 申扎县| 桂平市| 苍山县| 上虞市| 海阳市| 绵竹市| 尖扎县| 海伦市| 休宁县| 东山县| 虞城县|