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

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

POJ 1129 Channel Allocation (枚舉)

2019-11-11 02:10:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(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.

題意

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

思路

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

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

同理,對(duì)于其他沒(méi)有進(jìn)行染色的點(diǎn)也執(zhí)行該種操作,在染色的過(guò)程中要保證顏色數(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)); //初始化無(wú)染色 int ans=1; for(int ni=0; ni<n; ni++) //枚舉每一個(gè)點(diǎn) { if(color[ni]==0) //如果當(dāng)前點(diǎn)沒(méi)有染色 { 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(); //一開始沒(méi)加這個(gè),WA過(guò) for(int i=2; i<(int)strlen(c); i++) G[ni].push_back(c[i]-'A'); } solve(); } return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 邯郸市| 新竹县| 九江县| 平度市| 隆德县| 花垣县| 嘉定区| 泰兴市| 蒲江县| 云龙县| 壶关县| 永宁县| 两当县| 纳雍县| 临沧市| 宜春市| 娱乐| 永嘉县| 荥经县| 高台县| 洛隆县| 蓬安县| 鹤岗市| 卢氏县| 延边| 怀集县| 陇川县| 图木舒克市| 饶阳县| 都安| 神木县| 偏关县| 海原县| 洪江市| 南溪县| 施秉县| 高陵县| 兰西县| 张家口市| 万荣县| 丹巴县|