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

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

POJ 1129 Channel Allocation (枚舉)

2019-11-11 02:09:16
字體:
來源:轉載
供稿:網友

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;}
上一篇:JDBC

下一篇:PAT A1073. Scientific Notation (20)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永靖县| 临沧市| 门源| 海安县| 陇南市| 大新县| 咸宁市| 临漳县| 读书| 奉贤区| 蓬安县| 临夏市| 永平县| 屏边| 满城县| 健康| 山阳县| 绩溪县| 张家港市| 双城市| 平武县| 昌图县| 昭觉县| 阿鲁科尔沁旗| 屏山县| 丹棱县| 广元市| 衡山县| 疏勒县| 齐齐哈尔市| 溧水县| 论坛| 新竹县| 枞阳县| 柏乡县| 施甸县| 嵊泗县| 平武县| 波密县| 定远县| 饶河县|