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" gives n points, and then gives N points each adjacent points, adjacent points can not be a color, ask at least a few colors;
Ideas
Staining operation on point I: Search from the smallest color, and when I direct adjacent (direct successor) nodes have already dyed the color, search for the next color.
That is, I staining, when and only if the adjacent junction of I have not dyed the color, and the color should be as small as possible.
Reference: http://www.cnblogs.com/lyy289065406/archive/2011/07/31/2122590.html
#include <iostream>#include<stdio.h>#include<string.h>using namespaceStd;typedefclass{ Public: intnext[ -]; intnum;} Point;intMain () {intN; while(~SCANF ("%d",&N) {getchar ();//Enter point* a=Newpoint[n+1]; for(intI=1; i<=n;i++{GetChar ();//Current Point GetChar ();//Colonif(a[i].num<0) {A[i].num=0;//Initialize}Charch; while((Ch=getchar ())! ='\ n') { inttmp=ch% ('A'-1);//The point adjacent to the current point a->1,b->2 ... a[i].next[++a[i].num]=tmp;//in the next array of A[i]}} intcolor[ -]; memset (color,0,sizeof(color)); color[1]=1;//1th must need a colorintMaxcolor=1;//need at least one color for(intI=1; i<=n;i++) {Color[i]=n+1;//Set the color of the first point to the maximumintvis[ -]; memset (Vis,false,sizeof(VIS)); for(intj=1; j<=a[i].num;j++) { intk=a[i].next[j];//Point J adjacent points of the first Iif(Color[k]) vis[color[k]]=true;//If he already has color, mark that color as true;} for(intj=1; i<=n;j++) { if(!vis[j]&&color[i]>j)//If J color is not used, and less than color[i] to assign color[i] to J; {Color[i]=J; Break; } } for(intI=1; i<=n;i++) { if(maxcolor<Color[i]) {Maxcolor=Color[i]; } if(maxcolor==4) Break;//The maximum color is not more than four kinds, according to the four-color Theorem} } if(maxcolor==1) printf ("1 channel needed.\n"); Elseprintf"%d channels needed.\n", Maxcolor);d elete A; } return 0;}
Channel allocation_ four-color theorem