Channel Allocation(四色定理 dfs)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10897 | Accepted: 5594 |
Description
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
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
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)的更多相关文章
- POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...
- POJ 1129:Channel Allocation 四色定理+暴力搜索
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13357 Accepted: 68 ...
- Channel Allocation(DFS)
Channel Allocation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
- POJ 1129 Channel Allocation(DFS)
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13173 Accepted: 67 ...
- POJ-1129 Channel Allocation (DFS)
Description When a radio station is broadcasting over a very large area, repeaters are used to retra ...
- Channel Allocation (poj 1129 dfs)
Language: Default Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12 ...
- POJ 1129 Channel Allocation DFS 回溯
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15546 Accepted: 78 ...
- 四色定理+dfs(poj 1129)
题目:Channel Allocation 题意:要求A:BCD,A与B,C,D都不相同,求不同的值,典型的四色定理: #include <iostream> #include <a ...
- poj1129 Channel Allocation(染色问题)
题目链接:poj1129 Channel Allocation 题意:要求相邻中继器必须使用不同的频道,求需要使用的频道的最少数目. 题解:就是求图的色数,这里采用求图的色数的近似有效算法——顺序着色 ...
随机推荐
- RHEL7安装配置TigerVNC
TigerVNC使用非加密的链接,默认会被firewalld blocked 掉,想要 vnc正常工作就需要让firewalld开放相应的端口才行. vnc默认的端口号为5900,而每个vnc win ...
- 在Weex中定制自定义组件
1.配置自定义组件 public class MyViewComponent extends WXComponent{ public MyViewComponent(WXSDKInstance ins ...
- Uri、UriMatcher、ContentUris详解
http://blog.csdn.net/feng88724/article/details/6331396 1.Uri 通用资源标志符(Universal Resource Identifier, ...
- org.springframework.beans.factory.BeanCreationException: 求教育!
2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...
- PropertyPlaceholderConfigurer的用法(使用spring提供的类读取数据库配置信息.properties)
http://www.cnblogs.com/wanggd/archive/2013/07/04/3172042.html(写的很好)
- SQL中使用的一些函数问题
abs()取绝对值ceil()上取整floor()下取整initcap()使串中的所有单词的首字母变为大写substr()取子串 这些函数都是oracle的sql内置函数.
- 解决ubuntu下的文本编辑器gedit的乱码问题
缺省配置下,用 Ubuntu 的文本编辑器(Gedit)打开GB18030(繁体中文用户请将这里的出现的GB18030替换成BIG5或BIG5-HKSCS)类型的中文编码文本文件时,将会出现乱码. 出 ...
- SpringMVC4+thymeleaf3的一个简单实例(篇三:页面参数获取)
本篇将通过示例介绍页面参数是如何传递到后台的.我们继续沿用之前搭好的程序结构,如果你不知道,请参照前两篇.为方便跳转页面,我们在首页以及zoolist.html页面都加上彼此地址的链接:首页: zoo ...
- FlightGear 视角控制
Flightgear提供了非常灵活的模块化功能 这里就简要记录一下视角切换功能 首先,需要了解一下Flightgear中的property tree的主要内容,这里暂略. http://wiki.fl ...
- SGU 165.Basketball
题意 输入n个在[1.95,2.05]范围内的数. 保证他们的平均数为2.00. 现在要求把这些数调整出一个顺序, 使得任意长度为K的子段和与2.00* ...