poj 1129(dfs+图的四色定理)
题目链接:http://poj.org/problem?id=1129
思路:根据图的四色定理,最多四种颜色就能满足题意,使得相邻的两部分颜色不同。而最多又只有26个点,因此直接dfs即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; bool map[][];
int mark[];
char str[];
int n,ans; bool Judge(int x,int color)
{
for(int i=;i<n;i++){
if(i!=x&&map[x][i]&&mark[i]==color)
return ;
}
return ;
} void dfs(int pos)
{
if(pos==n){
ans=min(ans,*max_element(mark,mark+n));
return ;
}
for(int i=pos;i<n;i++){
for(int j=;j<=;j++){
if(Judge(i,j)){
mark[i]=j;
dfs(i+);
}
}
}
} int main()
{
while(~scanf("%d",&n)&&n){
memset(map,false,sizeof(map));
for(int i=;i<n;i++){
scanf("%s",str);
for(int j=;j<strlen(str);j++){
map[i][str[j]-'A']=true;
map[str[j]-'A'][i]=true;
}
}
memset(mark,,sizeof(mark));
ans=;
mark[]=;
dfs();
if(ans==){
printf("1 channel needed.\n");
}else
printf("%d channels needed.\n",ans);
}
return ;
}
poj 1129(dfs+图的四色定理)的更多相关文章
- 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 ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...
- 四色定理+dfs(poj 1129)
题目:Channel Allocation 题意:要求A:BCD,A与B,C,D都不相同,求不同的值,典型的四色定理: #include <iostream> #include <a ...
- 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 ...
- poj 1129 Channel Allocation ( dfs )
题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...
- 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 ...
随机推荐
- 算法笔记_124:密码脱落(Java)
一 问题描述 X星球的考古学家发现了一批古代留下来的密码.这些密码是由A.B.C.D 四种植物的种子串成的序列.仔细分析发现,这些密码串当初应该是前后对称的(也就是我们说的镜像串).由于年代久远,其中 ...
- 算法笔记_081:蓝桥杯练习 算法提高 矩阵乘法(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 有n个矩阵,大小分别为a0*a1, a1*a2, a2*a3, ..., a[n-1]*a[n],现要将它们依次相乘,只能使用结合率,求最 ...
- JAVA 解密pkcs7(smime.p7m)加密内容 ,公钥:.crt 私钥:.pem 使用Bouncy Castle生成数字签名、数字信封
第三方使用公钥.crt加密后返回的内容,需要使用私钥解密.pem 返回内容格式如下 MIME-Version: 1.0 Content-Disposition: attachment; filenam ...
- 华为无线ap3010dn-agn刷成胖ap
刚买的华为ap3010dn-agn 版本为 v200R007C20SPC500 默认不带命令 ap-mode-switch [Huawei]dis version Huawei Versatile ...
- LaTeX 中使用三级标题
需要在导言区加入命令:\setcounter{secnumdepth}{4} 而后: \section{一级标题} \subsection{二级标题} \subsubsection{三级标题}
- Actors编程模型
Actors模型(Actor model)首先是由Carl Hewitt在1973定义, 由Erlang OTP (Open Telecom Platform) 推广,其 消息传递更加符合面向对象的原 ...
- python打包exe文件-ImportError: No module named 'queue'
我之前遇到的一个错误就是 File "site-packages\urllib3\packages\six.py", line 92, in __get__ File " ...
- Fiddler设置代理抓手机包
启动Fiddler,打开菜单栏中的 Tools > Fiddler Options,打开“Fiddler Options”对话框. 在Fiddler Options”对话框切换到“Connect ...
- ubuntu12.04打开某一个已安装的软件的方法
1.快捷键win+A,里面显示已安装的软件 2.打开左上角的dash home,即ubuntu标志图,输入想要打开的软件 还有其它方法,探索中... .
- SMP
SMP(Symmetrical Multi-Processing):对称多处理技术,是指在一个计算机上汇集了一组处理器(多CPU),各CPU之间共享内存子系统以及总线结构. 在smp系统中,所有的CP ...