Channel allocation (poj 1129 DFS)

Source: Internet
Author: User

Language:DefaultChannel allocation
Time limit:1000 ms   Memory limit:10000 K
Total submissions:12367   Accepted:6325

Description

When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every extends er 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 shoshould be minimized. 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. for example, ten repeaters wocould 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. 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 valid Ric relationship; if A is adjacent to B, then B is necessarily adjacent to. 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 (could t 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. 

Source

Southern African 2001

N indicates the number of radio stations. The number of the radio station is from A to Z. Then, we will give you the adjacent relationship between them so that you can find the minimum required frequency. The same frequency is not allowed between any two adjacent radio stations.

Train of Thought: the data is not big, up to 26, DFS is violent, and the graph is saved in the adjacent table. Color [x] = I indicates the I frequency used by Station X.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <string> # include <map> # include <stack> # include <vector> # include <set> # include <queue> # pragma comment (linker, "/Stack: 102400000,102400000") # define maxn 30 # define maxn 2005 # define mod 1000000009 # define INF 0x3f3f3f # define PI ACOs (-1.0) # define EPS 1e-6typedef long ll; using namespace STD; struct EDG E {int U, V; int next;} edge [maxn * maxn]; int N, edgenum; int head [maxn]; int color [maxn]; void addedge (int u, int v) {edge [edgenum]. V = V; edge [edgenum]. next = head [u]; head [u] = edgenum ++;} bool isok (int x) {for (INT I = head [X]; I! =-1; I = edge [I]. next) {If (color [x] = color [edge [I]. v]) return false;} return true;} bool DFS (INT point_num, int color_num) {If (point_num> N) return true; For (INT I = 1; I <= color_num; I ++) {color [point_num] = I; If (isok (point_num) {If (DFS (point_num + 1, color_num) return true ;} color [point_num] = 0;} return false;} int main () {While (scanf ("% d", & N) {getchar (); memset (Head,-1, sizeof (head); memset (Co Lor, 0, sizeof (color); edgenum = 0; For (INT I = 1; I <= N; I ++) {getchar (); char ch; while (CH = getchar () {If (CH = '\ n') break; addedge (I, ch-'A' + 1 );}} for (INT I = 1; I <= N; I ++) // from 1 ~ N enumerative Number of color types {If (DFS (1, I) {if (I = 1) // note that a frequency uses a channel, use channels printf ("1 channel needed. \ n "); else printf (" % d channels needed. \ n ", I); break ;}} return 0;}/* 2a: B: 4A: BCB: ACDC: abdd: bc4a: bcdb: ACDC: abdd: abc0 */


Channel allocation (poj 1129 DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.