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. php CI框架实现验证码功能和增强验证码安全性实战教程

    php CI框架实现验证码功能和增强验证码安全性实战教程 CodeIgniter简称CI是最流行的一个php MVC框架之一,本人讲从实际项目使用中写系列实战经验,有别与其他的理论讲解文章,会附上实战 ...

  2. MySQL数据库----基础操作

    一.知识储备 数据库服务器:一台计算机(对内存要求比较高) 数据库管理系统:如mysql,是一个软件 数据库:oldboy_stu,相当于文件夹 表:student,scholl,class_list ...

  3. cojs 强连通图计数1-2 题解报告

    OwO 题目含义都是一样的,只是数据范围扩大了 对于n<=7的问题,我们直接暴力搜索就可以了 对于n<=1000的问题,我们不难联想到<主旋律>这一道题 没错,只需要把方程改一 ...

  4. C++设计模式 之 “单一职责”模式:Decorator、Bridge

    part 1 “单一职责”模式 在软件组件的设计中,如果责任划分的不清晰,使用继承得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任. 典型模式 Decorato ...

  5. 20145106 java 实验四

    这次的实验是Android开发实验基础.Android开发是一个很大的工程,但是这次只是一个小小的入门. 首先将SDK文件复制到-根目录下,之后将Android Studio复制到电脑里. 并指明SD ...

  6. jQuery 源码分析:当 selector 传来一个函数时,怎么进行处理?

    本文章为 0.9 版本,将会在稍后润色更新.本文使用的 jQuery 版本为 3.4.0 我们知道使用 $ 操作符时,可以往里面塞很多类型的参数,字符串,对象,函数...,jQuery 会根据不同的参 ...

  7. Go第十篇之反射

    反射是指在程序运行期对程序本身进行访问和修改的能力.程序在编译时,变量被转换为内存地址,变量名不会被编译器写入到可执行部分.在运行程序时,程序无法获取自身的信息. 支持反射的语言可以在程序编译期将变量 ...

  8. bootstrap5

    列表组的使用 ul.list-group > li.list-group-item *5... 列表组中可以放置徽标: 在li中放置 span.badge. bootstrap中的情景类: 实际 ...

  9. RN的第一个API-----注册组件Appregistry

    首先解释下AppRegistry是JS运行所有React Native应用的入口  什么是入口? 1.在我们初始化一个react native项目的时候 默认的index.ios.js/index.i ...

  10. UTC和GMT时间辨析

    一.UTC和GMT 每个地区都有自己的本地时间,在网上以及无线电通信中时间转换的问题就显得格外突出. 整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信场合,为了统一起见,使用一个统 ...