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. 题目大意:一张图中,相邻两点不能涂同一种颜色,要把整张图都涂上,最少需要多少种颜色。
题目解析:数据量不大,DFS即可。根据四色原理,最多只有四种颜色。先将第一个点涂上一种颜色(这是必然的),然后一个点一个点涂下去,当涂到最后一个点时,合计当前方案的颜色种数,然后更新最优解。 代码如下:
 # include<iostream>
# include<cstdio>
# include<set>
# include<string>
# include<cstring>
# include<algorithm>
using namespace std;
int n,ans,col[],mp[][];
bool ok(int p,int c)
{
for(int i=;i<n;++i)
if(mp[p][i]&&col[i]==c)
return false;
return true;
}
void dfs(int p)
{
if(p==n-){
for(int k=;k<=;++k){
if(ok(p,k)){
col[p]=k;
set<int>s;
for(int i=;i<n;++i){
s.insert(col[i]);
}
if(ans>s.size())
ans=s.size();
col[p]=;
}
}
return ;
}
for(int i=;i<=;++i){
if(ok(p,i)){
col[p]=i;
dfs(p+);
col[p]=;
}
}
}
int main()
{
while(scanf("%d",&n)&&n)
{
string p;
memset(mp,,sizeof(mp));
for(int i=;i<n;++i){
cin>>p;
for(int j=;j<p.size();++j)
mp[i][p[j]-'A']=mp[p[j]-'A'][i]=;
}
memset(col,,sizeof(col));
ans=;
col[]=;
dfs();
if(ans==){
printf("%d channel needed.\n",ans);
}else
printf("%d channels needed.\n",ans);
}
return ;
}

POJ-1129 Channel Allocation (DFS)的更多相关文章

  1. POJ 1129 Channel Allocation(DFS)

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13173   Accepted: 67 ...

  2. Channel Allocation(DFS)

    Channel Allocation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) ...

  3. poj 1129 Channel Allocation(图着色,DFS)

    题意: N个中继站,相邻的中继站频道不得相同,问最少需要几个频道. 输入输出: Sample Input 2 A: B: 4 A:BC B:ACD C:ABD D:BC 4 A:BCD B:ACD C ...

  4. POJ 1129 Channel Allocation 四色定理dfs

    题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...

  5. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  6. POJ 1129 Channel Allocation DFS 回溯

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15546   Accepted: 78 ...

  7. POJ 3009-Curling 2.0(DFS)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 5125 Desc ...

  8. 题解报告:poj 1321 棋盘问题(dfs)

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  9. POJ 2251 Dungeon Master(dfs)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

随机推荐

  1. margin显示怪异,外边距合并问题

    很多时候我们使用两个div,内层的div设置文字,需要垂直居中与上层div,但是怎么设置样式都不行,vertical-align:middle也不行. 代码: <div style=" ...

  2. Linux 安装JDK(jdk-8u121-linux-x64.tar.gz)

    Linux 安装JDK(jdk-8u121-linux-x64.tar.gz) 一.JDK准备 1.1 文件名称 jdk-8u121-linux-x64.tar.gz 1.2 下载地址 http:// ...

  3. Quartz框架调用Demo

    Quartz框架调用Demo 任务调度在JAVA应用程序中运用的十分普遍,掌握QUARTZ是必备的技能; 官网:http://www.quartz-scheduler.org/ 下载最新1.80资源包 ...

  4. redis.windows.conf 参数说明

    1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运行时,Redis默认会把pid写入/var/ru ...

  5. updateByPrimaryKeySelective更新失败

    问题:使用Mybatis中Mapper内置方法updateByPrimaryKeySelective更新失败. 发现:控制台打印出来的sql语句发现where条件出现所有属性. 解决:映射的实体类没有 ...

  6. vim常用的配置

    .vimrc配置 syntax enable syntax on set relativenumber set autoindent set tabstop=4 set expandtab " ...

  7. Python3基础 if else 格式 输入一个整数并判断是8吗

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. Bootloader之uBoot简介

    本文转载自:http://blog.ednchina.com/hhuwxf/1915416/message.aspx 一.Bootloader的引入 从前面的硬件实验可以知道,系统上电之后,需要一段程 ...

  9. javascript 构造函数类和原型 prototyp e定义的属性和方法的区别

    1.把方法写在原型中比写在构造函数中消耗的内存更小,因为在内存中一个类的原型只有一个,写在原型中的行为可以被所有实例共享,实例化的时候并不会在实例的内存中再复制一份而写在类中的方法,实例化的时候会在每 ...

  10. StringUtils类常用的方法讲解

    StringUtils 方法的操作对象是 Java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 ...