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 题意:要求相邻中继器必须使用不同的频道,求需要使用的频道的最少数目. 题解:就是求图的色数,这里采用求图的色数的近似有效算法——顺序着色 ...
随机推荐
- Java中ArrayList问题:删除一个ArrayList中的重复元素,注意留意一个问题
该问题有两种方法: 一 利用两个数组,此法简单,不讨论 二 利用一个数组,从第0个开始依次取元素,并在其后元素中查找是否有该元素,有则删掉后面的重复元素,依次遍历.---但是这种情况要特别注意,当后续 ...
- iOS AppIcon + launchImage+iPhone 屏幕分辨率相关知识
本文主要包含不同iOS 版本的尺寸,分辨率,以及appIcon,launchImage 对不同iOS 版本的适配问题 以下是主要主要的参考资料 https://developer.apple.com/ ...
- Linux软件安装,RPM与YUM
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3843955.html ...
- iOS开发UI篇——Button基础
一.简单说明 一般情况下,点击某个控件后,会做出相应反应的都是按钮 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 二.按钮的三种状态 1. normal(普通状态) ...
- JavaScript HTML DOM EventListener
JavaScript HTML DOM EventListener addEventListener() 方法 实例 点用户点击按钮时触发监听事件: document.getElementById(& ...
- String、StringBuffer、StringBuilder
也说String. String:不可变字符序列. StringBuffer:线程安全的可变字符序列. StringBuilder:StringBuffer的非线程安全实现,JDK1.5+. publ ...
- hbase的存储体系
一.了解hbase的存储体系. hbase的存储体系核心的有Split机制,Flush机制和Compact机制. 1.split机制 每一个hbase的table表在刚刚开始的时候,只有一个regio ...
- SGU 179.Brackets light
时间限制:0.25s 空间限制:12M 题意 给定一个合法的仅由'(',')'组成的括号序列,求它的下一个合法排列.假定'('<')'. Solution: ...
- Vim简明教程【CoolShell】
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- centos6.2下搭建Web服务器
1.安装Apache2 yum install httpd 2.启动 方法一:service httpd start 方法二:/etc/init.d/httpd start //浏览http://ip ...