Poj1129--dfs, four-color problem

Source: Internet
Author: User

POJ 1129 DFS quad-color issue channel Allocation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12799 Accepted: 6558

Description

When a radio station was broadcasting over a very large area, repeaters was used to retransmit the signal so, every rec Eiver has a strong signal. However, the channels used by each repeater must being carefully chosen so that nearby repeaters does not interfere with one an Other. This condition are satisfied if adjacent repeaters use different channels.

Since The radio frequency spectrum is a precious resource and the number of channels required by a given network of repeaters should be minimised. You has to write a program that reads in a description of a repeater network and determines the minimum number of the 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, and the repeaters be referred to by consecutive upper-case letters of the alphabet starting wit H A. For example, ten repeaters would has 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 is 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 isn't adjacent to any other, it line has the form

A:

The repeaters is listed in alphabetical order.

Note that the adjacency is a symmetric relationship; If A is adjacent to B and then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not having any 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 are in the singular form if only one channel is required.

Sample Input

2a:b:4a:bcb:acdc:abdd:bc4a:bcdb:acdc:abdd:abc0

Sample Output



Test instructions: Coloring in a given diagram, so that all the nodes are colored and the neighboring nodes cannot be the same color, the minimum required color
Idea: DFS, by the four-color theorem can be seen up to only four colors, so just traverse four colors, from Node 1, Node 2, ... node n, using the greedy selection of the nature from the color 1, the first solution must be the optimal solution, the diagram is not necessarily a connected graph, so only DFS (U) {...} The boundary is n, not directly to the surrounding extension
/*Poj1129_dfs 16ms*/#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>using namespacestd;Const intmaxn= -;Const intInf= (1<< -);intN;vector<int>G[MAXN];intCOLOR[MAXN];BOOLvis[maxn][6];//I node disable color Jintans,cnt;BOOLFlag;voidPutintUintCor) {Color[u]=cor;  for(intI=0; I<g[u].size (); i++){        intv=G[u][i]; Vis[v][cor]=1; }}voidRemov (intUintCor) {Color[u]=0;  for(intI=0; I<g[u].size (); i++){        intv=G[u][i]; Vis[v][cor]=0; }}voidDfsintu) {    if(flag)return; if(Color[u])return; if(u==n+1) {ans=CNT; Flag=1;return; }     for(intI=1; i<=4; i++){        if(!Vis[u][i])            {put (u,i); BOOLtag=0; if(i>CNT) {Tag=1; CNT++; } DFS (U+1);            Remov (U,i); if(tag) cnt--; }    }}intMain () { while(cin>>n,n) {         for(intI=1; i<=n;i++) g[i].clear ();        GetChar ();  for(intu=1; u<=n;u++){            Charch; CIN>>ch>>ch;  while((Ch=getchar ())! ='\ n') G[u].push_back (ch-'A'+1); } ans=cnt=0; Flag=0; memset (color,0,sizeof(color)); memset (Vis,0,sizeof(VIS)); DFS (1); if(ans==1) printf ("1 channel needed.\n"); Elseprintf"%d channels needed.\n", ans); }    return 0;}
Poj_1129_dfs

Poj1129--dfs, four-color problem

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.