POJ 1129

Channel Allocation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11276 Accepted: 5781

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

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. 

题目大意就是相邻的广播站不能互相干扰,然后问你最少要几个不同的频道数。
思路很简单,因为只有26个字母所以可以暴搜,但是如果数据大了不可取,这道题有一个条件叫做任意两个频道不相交,那么这道题可以转化成相邻两个点着不同颜色的问题,
进而转化成了一道基本的四色定理的题目。首先判断每个节点与其相邻的节点是否颜色相同,如果相同则需要换颜色,否则就可以继续用这种颜色涂下去,直到N个节点全部上
色成功后,输出所用的颜色数- - 当然颜色数必然小于等于4,在遍历的过程中可以用DFS扫过去就O了。。 一个上午就做了这一道题 效率太低了TVT 还好是1A的,下面贴代码
= =!
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int map[30][30];
int  a[30];
int n;

int  check(int k)
{
    int i;
    for(i=0;i<n;i++)
    {
        if(map[i][k]==1&&a[i]==a[k])//直接相邻的两点颜色相同
            return 1;
    }
    return 0;
}

int dfs(int s,int c)
{
    if(s==n)
        return n;

        for(a[s]=1;a[s]<=c;a[s]++)
        {
            if(check(s)==0)//如果颜色不同或者木有直接相邻
                return dfs(s+1,c);//继续用这个颜色去着色
        }
    return 0;
}


int main()
{
    int i,j;
    char color[30];
    while(cin>>n)
    {
        if(n==0)
            return 0;
        memset(map,0,sizeof(map));
        memset(a,0,sizeof(a));
        for(i=0;i<n;i++)
        {
            cin>>color;
            for(j=2;color[j]!='\0';j++)//尼玛注意J=2是因为J=1输进去的是个冒号o(╯□╰)o
            {
                map[i][color[j]-'A']=1;//这个是标记每个节点直接相邻的点。。。
            }
        }
        for(i=1;i<=4;i++)
        {
            if(dfs(0,i)!=0)//如果没有返回0证明着色成功
                break;

        }
        if(i==1)
            cout<<"1 channel needed."<<endl;
        else
            cout<<i<<" "<<"channels needed."<<endl;
    }


    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值