POJ 1129 Channel Allocation (enum) _ Enumeration

Source: Internet
Author: User
Description

When a radio station are broadcasting over a very large area repeaters are used to retransmit the signal so every rec Eiver has a strong signal. However, the channels used by each repeater must is carefully chosen so and nearby do is repeaters with one Other. This condition was 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 are have to write a program that reads in a description of a repeater network and determines the minimum number of channel s 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 repeaters are referred to by consecutive upper-case letters of the alphabet starting H 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 the repeaters B, C, D and H are adjacent to the repeater A. The describes those adjacent to repeater A, the second those adjacent to B, and is on for the repeaters. If A repeater is isn't adjacent to no other and its line has the form

A:

The repeaters are listed in alphabetical order.

The adjacency is a symmetric relationship; If a is adjacent to B, then B are necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent-repeaters does not have no line segment s that cross.


Output

For each map (except the "final one with no repeaters"), print a line containing the minumum number of channels needed so th At no adjacent channels interfere. The sample output shows the format of this line. Take care that channels was in the singular form at only one one channel is required.


Sample Input

2
A:
B:
4
a:bc
b:acd
c:abd
d:bc
4
a:bcd
b:acd
c:abd
D: ABC
0


Sample Output

1 channel needed.
3 channels needed.
4 channels needed. 


the

Give a picture, ask at least several colors to be able to dye this picture, where adjacent points can not have the same color.


train of Thought

According to the four-color theorem, the answer is maximum 4.

First coloring the dots and then placing the adjacent dots as other colors (to ensure that they are adjacent to each other).

Similarly, for other no staining points also perform this operation, in the process of staining to ensure that the number of colors as little as possible, specific look at the code.


AC Code

#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];  The color of each point is void Solve () {memset (color,0,sizeof (color));
    Initialize the non-staining int ans=1; for (int ni=0; ni<n; ni++)//Enumerate each point {if (color[ni]==0)//If the current point is not stained {bo   OL isc[5]= {false}; According to the four-color principle, the maximum has four colors for (int i=0; i< (int) g[ni].size (); i++)//Enumerate the temporary points and mark its color isc[color[g[ni][i]
            ]]=true; for (int i=1; i<=4; i++)//Find a color that is different from the adjacent point if (isc[i]==false) {Color[n        I]=i;     Coloring Ans=max (ans,i);
                The maximum number of color types currently in use 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 ();
        I didn't add this at first, WA over for (int i=2; i< (int) strlen (c); i++) g[ni].push_back (c[i]-' A ');
    } solve ();
return 0; }

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.