POJ Channel Allocation 1129(dfs+四色定理)

来源:互联网 发布:知乎js游戏引擎 编辑:程序博客网 时间:2024/04/28 10:31

Channel Allocation
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 14514 Accepted: 7391

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. 

Source

Southern African 2001

题意:

当一个广播电台在一个非常大的地区,广播站会用中继器来转播信号以使得每一个接收器都能接收到一个强烈的信号。然而,每个中继器必须慎重选择使用,使相邻的中继器不互相干扰。如果相邻的中继器使用不同的频道,那么就不会相互干扰。

由于无线电频道是一有限的,一个给定的网络所需的中继频道数目应减至最低。编写一个程序,读取一个中继网络,然后求出需要的最低的不同频道数。



分析:就是染色问题,给出相邻关系,问最少用几种颜色,保证相邻的不会有重色

有个四色定理,四色定理的本质就是在平面或者球面无法构造五个或者五个以上两两相连的区域,就是给相邻的区域涂颜色,最多用四中颜色(用于剪枝)


#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>using namespace std;int mp[30][30],v[30],n,flag;void dfs(int x){    int i,j;    if(x==n+1)    {        flag=1;        int max1=1;        for(i=1;i<=n;i++)            max1=max(v[i],max1);        if(max1==1)        {            printf("1 channel needed.\n");        }        else        {            printf("%d channels needed.\n",max1);        }        return ;    }    for(i=1;i<=4;i++)//四色    {        for(j=1;j<=n;j++)        {            if(mp[x][j] && v[j]==i)                break;        }        if(j==n+1)        {            v[x]=i;            dfs(x+1);            if(flag)                return ;        }    }}int main(){    int m,i,j;    char str[100];    int l,x,y;    while(scanf("%d",&n)&&n)    {        flag=0;        memset(mp,0,sizeof(mp));        memset(v,0,sizeof(v));        for(i=0;i<n;i++)        {            scanf("%s",str);            l=strlen(str);            x=str[0]-'A'+1;            for(j=2;j<l;j++)            {                y=str[j]-'A'+1;                mp[x][y]=1;            }        }        dfs(1);    }    return 0;}























0 0
原创粉丝点击