Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10897   Accepted: 5594

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.
题意:类似于染色问题,给定N个顶点的无向图,对这N个顶点染色,要求任意相邻两个顶点染不同的颜色,问最少需要染多少种颜色; 思路:开始是用的枚举法,把每个节点的度降序排列,每次选一个未被染色的顶点染一种新的颜色,并且不与该顶点相邻的点也染同一种颜色,数据过了,不知道为什么WA;
后来参考的深搜,对dfs有了更深的了解了;
 #include<stdio.h>
#include<string.h> int n,map[][];
int vis[][];//vis[i][j]表示第i个节点被染第j种颜色;
int ans; void cmp()
{
int res = -;
for(int i = ; i < ; i++)
{
for(int j = ; j < n; j++)
if(vis[j][i] && res < i)
res = i;
}
if(ans > res)
ans = res;
}
void dfs(int x)
{
if(x >= n)
{
cmp();
return;
}
for(int i = ; i < ; i++)
{
bool flag = true;
for(int j = ; j < n; j++)
{
//如果节点x的前驱或后继已经染了第i种色,节点x就不能染i色;
if((map[j][x]&&vis[j][i])||(map[x][j]&&vis[j][i]))
{
flag = false;
break;
}
}
if(flag)
{
vis[x][i] = ;//不满足上个条件,节点x染i色;
dfs(x+);//继续染下一个节点;
vis[x][i] = ;
}
}
}
int main()
{
char s[];
while(~scanf("%d",&n) && n)
{
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
for(int i = ; i < n; i++)
{
scanf("%s",s);
int u = s[]-'A';
for(int j = ; s[j]; j++)
{
int v = s[j]-'A';
map[u][v] = ;
}
}
ans = ;
vis[][] = ;//假设第一个节点已染第一个色
dfs();//从第二个节点开始染色;
ans += ;
if(ans == )
printf("1 channel needed.\n");
else
printf("%d channels needed.\n",ans);
}
return ;
}
												

Channel Allocation(四色定理 dfs)的更多相关文章

  1. POJ 1129 Channel Allocation 四色定理dfs

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

  2. POJ 1129:Channel Allocation 四色定理+暴力搜索

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13357   Accepted: 68 ...

  3. Channel Allocation(DFS)

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

  4. POJ 1129 Channel Allocation(DFS)

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

  5. POJ-1129 Channel Allocation (DFS)

    Description When a radio station is broadcasting over a very large area, repeaters are used to retra ...

  6. Channel Allocation (poj 1129 dfs)

    Language: Default Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12 ...

  7. POJ 1129 Channel Allocation DFS 回溯

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

  8. 四色定理+dfs(poj 1129)

    题目:Channel Allocation 题意:要求A:BCD,A与B,C,D都不相同,求不同的值,典型的四色定理: #include <iostream> #include <a ...

  9. poj1129 Channel Allocation(染色问题)

    题目链接:poj1129 Channel Allocation 题意:要求相邻中继器必须使用不同的频道,求需要使用的频道的最少数目. 题解:就是求图的色数,这里采用求图的色数的近似有效算法——顺序着色 ...

随机推荐

  1. RHEL7安装配置TigerVNC

    TigerVNC使用非加密的链接,默认会被firewalld blocked 掉,想要 vnc正常工作就需要让firewalld开放相应的端口才行. vnc默认的端口号为5900,而每个vnc win ...

  2. 在Weex中定制自定义组件

    1.配置自定义组件 public class MyViewComponent extends WXComponent{ public MyViewComponent(WXSDKInstance ins ...

  3. Uri、UriMatcher、ContentUris详解

    http://blog.csdn.net/feng88724/article/details/6331396 1.Uri 通用资源标志符(Universal Resource Identifier, ...

  4. org.springframework.beans.factory.BeanCreationException: 求教育!

    2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...

  5. PropertyPlaceholderConfigurer的用法(使用spring提供的类读取数据库配置信息.properties)

    http://www.cnblogs.com/wanggd/archive/2013/07/04/3172042.html(写的很好)

  6. SQL中使用的一些函数问题

    abs()取绝对值ceil()上取整floor()下取整initcap()使串中的所有单词的首字母变为大写substr()取子串 这些函数都是oracle的sql内置函数.

  7. 解决ubuntu下的文本编辑器gedit的乱码问题

    缺省配置下,用 Ubuntu 的文本编辑器(Gedit)打开GB18030(繁体中文用户请将这里的出现的GB18030替换成BIG5或BIG5-HKSCS)类型的中文编码文本文件时,将会出现乱码. 出 ...

  8. SpringMVC4+thymeleaf3的一个简单实例(篇三:页面参数获取)

    本篇将通过示例介绍页面参数是如何传递到后台的.我们继续沿用之前搭好的程序结构,如果你不知道,请参照前两篇.为方便跳转页面,我们在首页以及zoolist.html页面都加上彼此地址的链接:首页: zoo ...

  9. FlightGear 视角控制

    Flightgear提供了非常灵活的模块化功能 这里就简要记录一下视角切换功能 首先,需要了解一下Flightgear中的property tree的主要内容,这里暂略. http://wiki.fl ...

  10. SGU 165.Basketball

    题意       输入n个在[1.95,2.05]范围内的数.       保证他们的平均数为2.00.       现在要求把这些数调整出一个顺序,       使得任意长度为K的子段和与2.00* ...