poj 1129 Channel Allocation(图着色,DFS)
题意:
N个中继站,相邻的中继站频道不得相同,问最少需要几个频道。
输入输出:
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个点的无向图,对每个点进行染色,相邻的点颜色不得一致,最少需多少种颜色。DFS即可。
代码:
<span style="font-family:Microsoft YaHei;">int nmax;
bool mapp[30][30], is[30], coll[30];
int col[30]; void dfs(int u){
mem(coll,false);
rep(i,0,25){
if(u!=i && mapp[u][i] && col[i]){
coll[col[i]] = true;
}
}
rep(i,26,1) if(coll[i]){
nmax = i;
break;
}
rep(i,1,26) if(!coll[i]){
col[u] = i;
nmax = max(nmax,i);
break;
}
rep(i,0,25){
if(u!=i && mapp[u][i] && !col[i])
dfs(i);
}
} int main(){
int n;
char s[100]; while(scanf("%d",&n),n){
mem(mapp,false);
mem(is,false);
mem(col,0); while(n--){
scanf("%s",s);
int l=strlen(s);
is[s[0]-'A'] = true;
rep(i,2,l-1){
mapp[s[0]-'A'][s[i]-'A'] = true;
mapp[s[i]-'A'][s[0]-'A'] = true;
is[s[i]-'A'] = true;
}
} nmax = 0;
rep(i,0,25){
if(is[i] && !col[i]) dfs(i); //对i进行染色,并从i开始把那个集合中的点都染上色
} if(nmax==1) printf("%d channel needed.\n",nmax);
else printf("%d channels needed.\n",nmax);
}
}
</span>
poj 1129 Channel Allocation(图着色,DFS)的更多相关文章
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- POJ 1129 Channel Allocation(DFS)
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13173 Accepted: 67 ...
- POJ 1129 Channel Allocation DFS 回溯
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15546 Accepted: 78 ...
- poj 1129 Channel Allocation ( dfs )
题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...
- POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...
- poj 1129 Channel Allocation
http://poj.org/problem?id=1129 import java.util.*; import java.math.*; public class Main { public st ...
- PKU 1129 Channel Allocation(染色问题||搜索+剪枝)
题目大意建模: 一个有N个节点的无向图,要求对每个节点进行染色,使得相邻两个节点颜色都不同,问最少需要多少种颜色? 那么题目就变成了一个经典的图的染色问题 例如:N=7 A:BCDEFG B:ACDE ...
- Channel Allocation (poj 1129 dfs)
Language: Default Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12 ...
- POJ 1129:Channel Allocation 四色定理+暴力搜索
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13357 Accepted: 68 ...
随机推荐
- TP5.0版本mysql查询语句 闭包
Db::name('tiwen') ->where('user_id', $user_id) ->where(function ($query) { $query->where(fu ...
- 微信小程序view不能换行 text实现转义换行符
在html中可以直接使用<br />换行,但是小程序wxml中使用<br />无效,可以换成\n Page({ data: { title: '至少5个字\n请多说些感受吧' ...
- TP5 windows中执行定时任务
1 首先先写个自定义命令文件 比如 Test 2 在网站根目录下建立文件 crond.bat ,内容:(把你在cmd上操作流程写一遍) D: cd workspace\wamp\tp5 D:\PHPW ...
- nginx使用用户真实IP做hash(解决经过CND后ip_hash失效问题)
在nginx中常用的有以下四种负载均衡的算法,分别是:round-robin.ip-hash.least-connected和weighted.当然在实际生产中或许使用最多的就是ip-hash了,一般 ...
- ecshop调用指定栏目下的文章的方法
打开 index.php 添加 fun函数一个,需放在<php与?>中间. /** * 获得指定栏目的文章列表. * @param int $cid 栏目ID * @param int $ ...
- win10环境charles抓包unknow问题
win10环境,charles已经安装了证书,但是抓包的时候还是unknow,让人头疼. 1.确保证书安装成功. 2.检查charles设置是否正确. 进入Charles - > Proxy - ...
- 启动Jenkins后无法访问,如何排错
做IT工作,使用各种工具的时候,遇到错误都是一堆英文,对于英语不好的人,看到报错可能就会心烦,我刚开始就是这种状态.后来,遇到问题,首先复制报错信息到百度上搜索,没有人请教的时候,你不能坐等问题自己解 ...
- centos 7 部署 zookeeper
Zookeeper集群搭建 由于公司缓存方案改进,准备采用codis集群作为主要的缓存解决方案(codis:国内豌豆荚开发的redis集群解决方案,已开源,github地址:https://githu ...
- Python3模块调用你真的会吗?不懂就来看一看?
前言 学习Python自动化框架的时候,各种文件会相互之间的调用.刚学的时候是不是很头疼!有木有!!一步步告诉你如何调用文件里的类和方法. 经常会调用同目录下的文件还有跨文件的调用 调用同目录下文件A ...
- R7000 电脑调整亮度
R7000 电脑亮度太亮,想调整亮度,fn+F5,F6 不起作用,需要调整显卡的设置