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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

POJ 1129 Channel Allocation (枚舉)

2019-11-11 02:11:46
字體:
供稿:網(wǎng)友

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.

題意

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

思路

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

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

同理,對于其他沒有進(jìn)行染色的點(diǎn)也執(zhí)行該種操作,在染色的過程中要保證顏色數(shù)盡可能少,具體看代碼。

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]; //每個(gè)點(diǎn)的顏色void solve(){ memset(color,0,sizeof(color)); //初始化無染色 int ans=1; for(int ni=0; ni<n; ni++) //枚舉每一個(gè)點(diǎn) { if(color[ni]==0) //如果當(dāng)前點(diǎn)沒有染色 { bool isc[5]= {false}; //根據(jù)四色原理,最大有四種顏色 for(int i=0; i<(int)G[ni].size(); i++) //枚舉臨接的點(diǎn)并標(biāo)記它的顏色 isc[color[G[ni][i]]]=true; for(int i=1; i<=4; i++) //找出一個(gè)與相鄰點(diǎn)不同的顏色 if(isc[i]==false) { color[ni]=i; //著色 ans=max(ans,i); //當(dāng)前所使用的最大顏色種數(shù) 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(); //一開始沒加這個(gè),WA過 for(int i=2; i<(int)strlen(c); i++) G[ni].push_back(c[i]-'A'); } solve(); } return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴海县| 滨州市| 东乌| 即墨市| 林西县| 桐梓县| 湖北省| 仁布县| 香港| 大新县| 平罗县| 成安县| 沙河市| 黄龙县| 荣昌县| 新安县| 北京市| 炎陵县| 朔州市| 延长县| 二连浩特市| 白河县| 黄山市| 三河市| 全椒县| 临城县| 类乌齐县| 西峡县| 鹿邑县| 江北区| 永泰县| 定襄县| 白沙| 毕节市| 文成县| 环江| 乌拉特前旗| 清水县| 班玛县| 浦县| 刚察县|