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 题意:要求相邻中继器必须使用不同的频道,求需要使用的频道的最少数目. 题解:就是求图的色数,这里采用求图的色数的近似有效算法——顺序着色 ...
随机推荐
- spring mvc DispatcherServlet详解之拾忆工具类utils
DispatcherServlet的静态初始化 /** * Name of the class path resource (relative to the DispatcherServlet cla ...
- 【Android】面试宝典
Android面试 1. 内容介绍................................................................................... ...
- 9.22 noip模拟试题
水灾(sliker.cpp/c/pas) 1000MS 64MB 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY ...
- vs在winform中不给力哈-错误不提示
我的操作系统是windows Server 2008 x64,运行winform的时候,对Dictionary累加值.运行的时候,项目一闪而过,于是我在Project的Properties上选择运行的 ...
- Orace数据库锁表的处理与总结<摘抄与总结一>
TM锁(表级锁)类型共有5种,分别称为共享锁(S锁).排它锁(X锁).行级共享锁(RS锁).行级排它锁(RX锁).共享行级排它锁(SRX锁) 当Oracle执行DML语句时,系统自动在所要操作的表上申 ...
- oracle-snapshot too old 示例
一.快照太老例子: 1.创建一个很小的undo表空间,并且不自动扩展. create undo tablespace undo_small datafile '/u01/app/oracl ...
- iOS 常见错误:CALayer position contains NaN: [14 nan]
Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contai ...
- NSDate,NSCalendar,NSTimer,NSTimeZone
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. ...
- JavaScript Window Screen
window.screen 对象包含有关用户屏幕的信息. Window Screen window.screen对象在编写时可以不使用 window 这个前缀. 一些属性: screen.availW ...
- ASP.NET网站实现中英文转换(本地化资源)
主要内容: 1. 简单例子 2. 进一步认识Localization 3. 语言转换 4. 解决方案 一. 简单例子 下面通过一个简单的例子来说明利用Localization来实现本地化是那么的简单, ...