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 ...
随机推荐
- jmeter 录制排除模式
jmeter录制时,静态的资源不需要,可以在录制的时候直接排除. .*\.(bmp|css|js|gif|icov|jpeg|png|swf|woff|woff2|htm|html).* .*\.(j ...
- centos查找大文件
首先到相当的目录下面,按下面方式查找 find . -type f -size +800M -print0 | xargs -0 ls -lah或者从根目录(/)开始查找find / -type f ...
- git pull 时remote: HTTP Basic: Access denied解决方案
当qian windows用户密码过期更改了密码后,操作git pull 拉取远程仓库代码或git push时报错 如下:remote: HTTP Basic: Access denied Auth ...
- P3480-[POI2009]KAM-Pebbles【阶梯博弈】
正题 题目链接:https://www.luogu.com.cn/problem/P3480 题目大意 \(n\)个石头堆上进行\(\text{Nim}\)游戏,不过需要满足每次操作前后都有\(a_i ...
- Dapr + .NET Core实战(十一)单机Dapr集群
如何单机部署Dapr集群 第十篇讲过了K8S集群下如何使用Dapr运行程序,但是很多人一直在问如何单机下进行Dapr的负载,这节课我们来聊聊如何单机进行Dapr的负载. 首先要说的是单机下,通过 da ...
- break和continue关键字
什么是break break 跳出最里层的循环,并且继续执行该循环下面的语句 就是强制退出循环 示例 package struct; public class ForDemo05 { public s ...
- 20 行代码:Serverless 架构下用 Python 轻松搞定图像分类和预测
作者 | 江昱 前言 图像分类是人工智能领域的一个热门话题.通俗解释就是,根据各自在图像信息中所反映的不同特征,把不同类别的目标区分开来的图像处理方法. 它利用计算机对图像进行定量分析,把图像或图像中 ...
- MySQL的详细讲解
目录 Mysql的架构与历史 MySQL的逻辑架构 更新中---- Mysql的架构与历史 MySQL的逻辑架构 第二层的架构是所有的跨引擎的功能实现的地方,例如:存储,触发器,视图等. 第三层半酣了 ...
- 洛谷3244 落忆枫音 (拓扑图dp+式子)
题目大意就是 给你一个DAG 然后添加一条边\(x->y\) ,询问以1为根的生成树的个数 QWQ 首先假设没有添加的边 答案就应该是 \[ans=\prod_{i=1}^{n} in[i] \ ...
- 破解安装pyhotn
1.网址 https://www.jetbrains.com/pycharm/download/#section=windows,打开页面,点击下载专业版 2.这是下载好的文件,双击运行即可. //详 ...