POJ

来源:互联网 发布:好看的校园网络电影 编辑:程序博客网 时间:2024/06/05 00:30


题目翻译:

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

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


题解:

当一个点染色后,更新它相邻的点, 将相邻的点标记上他的颜色,表示相邻的点不能再使用这个颜色。。。。至于什么四色定理,还没去看...


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stack>#include<vector>using namespace std;int color[33],vis[33][33];vector<int>vec[33];char s[111];int ans;void dfs(int u){    int i,j,v;    for(j=1; j<=26; j++)        if(vis[u][j]==0) break;    color[u]=j;    ans=max(ans,j);    for(i=0; i<vec[u].size(); i++)    {        v=vec[u][i];        if(color[v]) continue;        vis[v][j]=1;    }    for(i=0; i<vec[u].size(); i++)    {        v=vec[u][i];        if(color[v]) continue;        dfs(v);    }}int main(){    int n,i,j;    int v,u;    while(scanf("%d",&n) && n)    {        for(i=1; i<=26; i++) vec[i].clear();        while(n--)        {            scanf("%s",s);            u=s[0]-'A'+1;            for(i=2; i<strlen(s); i++)            {                v=s[i]-'A'+1;                vec[u].push_back(v);                vec[v].push_back(u);            }            memset(color,0,sizeof(color));            memset(vis,0,sizeof(vis));        }        ans=0;        for(i=1;i<=26;i++)            if(color[i]==0) dfs(i);        if(ans==1)        {            printf("%d channel needed.\n",ans);        }        else        {            printf("%d channels needed.\n",ans);        }    }    return 0;}

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. 




0 0
原创粉丝点击