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 题意:要求相邻中继器必须使用不同的频道,求需要使用的频道的最少数目. 题解:就是求图的色数,这里采用求图的色数的近似有效算法——顺序着色 ...
随机推荐
- AndroidStudio工程文件导入Jar包和So第三方库
AndroidStudio 导入Jar包和第三方So库 在android开发中,需要导入许多第三方的jar包和so库来支持,包括像许多第三方的支持平台--友盟,环信.融云.极光推送.微博.腾讯等第三方 ...
- Java 406
项目改名之后, 项目上传后,报错,406,可是项目本地是可以跑起来的, 联系管理员,管理员改了个/etc/httpd/conf/workers2.properties 里面,将本次的项目加入进去就OK ...
- oracle数组学习资料
--oracle数组,所谓数组就是 字段的 个数,数组应该很有用 --可变数组 declare type v_ar is varray(10) of varchar2(30); my_ar v ...
- ie67 设置最小宽度最小高度
1.最小宽度 min-width:1003px; _width:expression((document.documentElement.clientWidth||document.body.clie ...
- 【HDU4366】【DFS序+分块】Successor
Problem Description Sean owns a company and he is the BOSS.The other Staff has one Superior.every st ...
- Fedora 21 安装Infinality
原文地址: Fedora 21 用infinality美化你的字体 http://blog.csdn.net/element207/article/details/41746683 安装infinal ...
- centos7/RHEL7安装LibreOffice
1.下载 wget http://download.documentfoundation.org/libreoffice/testing/4.4.0/rpm/x86_64/LibreOfficeDev ...
- 获取IP城市
新浪的接口 : http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 多地域测试方法:http://int.dpool.sina. ...
- 对象的内置属性和js的对象之父Object()
js中对象有constructor,valueOf(),toString()等内置属性和方法; 创建一个空对象的方法: var o = {}; 或者 var o= new Object(); o.co ...
- C# Winform 双屏显示
双屏显示1 // 利用WinForm中的Screen类,即可比较方便地实现多窗体分别在多个屏幕上显示. //•获取当前系统连接的屏幕数量: Screen.AllScreens.Count(); //• ...