题目大意建模:

一个有N个节点的无向图,要求对每个节点进行染色,使得相邻两个节点颜色都不同,问最少需要多少种颜色?

那么题目就变成了一个经典的图的染色问题

例如:N=7

A:BCDEFG

B:ACDEFG

C:ABD

D:ABCE

E:ABDF

F:ABEG

G:ABF

画成图就是:

首先考虑四色定理:任何一张地图只用四种颜色就能使具有共同边界的国家着上不同的颜色

judge(int x,int y)枚举判断x的邻接点中是否着色y颜色的

1.正向考虑dfs(int num,int color)从第一个点开始从前往后用color种颜色给num个点着色

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char ch[];
bool vis[];
int n,col[],graph[][];
bool judge(int x,int y)
{//判断在x点是否可以着色y
for(int i=;i<n;i++){
if(graph[x][i])
if(col[i]==y) return false;
}
return true;
}
bool dfs(int num,int color)
{
if(num>n)
return true;
for(int i=;i<num;i++){//dfs()函数含义体现在此处for循环
if(!vis[i]){
vis[i]=true;
for(int j=;j<=color;j++){
if(judge(i,j)){
col[i]=j;//着色
if(dfs(num+,color))
return true;
}
}
vis[i]=false;
}
}
return false;
}
int main()
{
while(scanf("%d",&n),n){
memset(graph,,sizeof(graph));
for(int i=;i<n;i++){
cin>>ch;for(int j=;j<strlen(ch);j++)
graph[ch[]-'A'][ch[j]-'A']=;
}
for(int i=;i<=;i++){
memset(vis,,sizeof(vis));
if(dfs(,i)){
if(i==)
printf("1 channel needed.\n");
else
printf("%d channels needed.\n",i);
break;
}
}
}
}

2.反向考虑dfs(int num,int color)从第n个点开始从后往前用color种颜色给剩下的num个点着色,同时减少vis[30]数组的开辟

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char ch[];
int n,col[],graph[][];
bool judge(int x,int y)
{
for(int i=;i<n;i++){
if(graph[x][i])
if(col[i]==y) return false;
}
return true;
}
bool dfs(int num,int color)
{
if(!num)
return true;//dfs()函数含义体现在此处for循环
for(int i=num-;i>=;i--){//或者for(int i=0;i<num;i++)
if(!col[i]){
for(int j=;j<=color;j++){
if(judge(i,j)){
col[i]=j;
if(dfs(num-,color))
return true;
}
}
col[i]=;
}
}
return false;
}
int main()
{
while(scanf("%d",&n),n){
memset(graph,,sizeof(graph));
for(int i=;i<n;i++){
cin>>ch;for(int j=;j<strlen(ch);j++)
graph[ch[]-'A'][ch[j]-'A']=;
}
for(int i=;i<=;i++){
memset(col,,sizeof(col));
if(dfs(n,i)){
if(i==)
printf("1 channel needed.\n");
else
printf("%d channels needed.\n",i);
break;
}
}
}
}

3.dfs(int num,int color)从第num个点开始用color种颜色着色,函数含义体现在自身

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char ch[];
bool vis[];
int n,col[],graph[][];
bool judge(int x,int y)
{
for(int i=;i<n;i++){
if(graph[x][i])
if(col[i]==y) return false;
}
return true;
}
bool dfs(int num,int color)
{
if(num>n)
return true;
for(int i=num-;i<n;i++){//和第一种正向考虑的区别
if(!vis[i]){
vis[i]=;
for(int j=;j<=color;j++){
if(judge(i,j)){
col[i]=j;
if(dfs(num+,color))
return true;
}
}
vis[i]=;
}
}
return false;
}
int main()
{
while(scanf("%d",&n),n){
memset(graph,,sizeof(graph));
for(int i=;i<n;i++){
cin>>ch;for(int j=;j<strlen(ch);j++)
graph[ch[]-'A'][ch[j]-'A']=;
}
for(int i=;i<=;i++){
memset(vis,,sizeof(vis));
if(dfs(,i)){
if(i==)
printf("1 channel needed.\n");
else
printf("%d channels needed.\n",i);
break;
}
}
}
}

暴力搜索

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
bool graph[][];
char ch[];
int col[],colornum;
int n,mincolor;
bool judge(int x,int y)
{
for(int i=;i<n;i++){
if(graph[x][i])
if(col[i]==y) return false;
}
return true;
}
bool flag;
void dfs(int i)
{
if(flag) return;
if(i==n){//剪枝
mincolor=colornum;
flag=;
}//判断用过的颜色里是否有可用的
for(int j=;j<=colornum;j++){
if(judge(i,j)){
col[i]=j;
dfs(i+);
col[i]=;//还原到dfs(i+1)之前的状态,因为可能还可以染其他颜色
}
}
colornum++;//上面颜色都不行的话再选用一种新的颜色
col[i]=colornum;
dfs(i+);
col[i]=;//还原到dfs(i+1)之前的状态
colornum--;//还原,因为i都没被染色,所以新加的颜色肯定要退回去
}
int main()
{
while(scanf("%d",&n),n){
memset(graph,,sizeof(graph));
for(int i=;i<n;i++){
cin>>ch;
for(int j=;j<strlen(ch);j++)
graph[ch[]-'A'][ch[j]-'A']=;
}
flag=,colornum=;
memset(col,,sizeof(col));
dfs();//注意 不是dfs(1),因为第一个点的编号为0
if(mincolor==)
printf("1 channel needed.\n");
else
printf("%d channels needed.\n",mincolor);
}
}

PKU 1129 Channel Allocation(染色问题||搜索+剪枝)的更多相关文章

  1. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  2. POJ 1129:Channel Allocation 四色定理+暴力搜索

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13357   Accepted: 68 ...

  3. POJ 1129 Channel Allocation(DFS)

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13173   Accepted: 67 ...

  4. POJ 1129 Channel Allocation DFS 回溯

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15546   Accepted: 78 ...

  5. poj 1129 Channel Allocation ( dfs )

    题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...

  6. POJ 1129 Channel Allocation 四色定理dfs

    题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...

  7. poj 1129 Channel Allocation

    http://poj.org/problem?id=1129 import java.util.*; import java.math.*; public class Main { public st ...

  8. 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 ...

  9. poj1129 Channel Allocation(染色问题)

    题目链接:poj1129 Channel Allocation 题意:要求相邻中继器必须使用不同的频道,求需要使用的频道的最少数目. 题解:就是求图的色数,这里采用求图的色数的近似有效算法——顺序着色 ...

随机推荐

  1. ThinkPHP项目笔记之RBAC(权限相关视频讲解篇

    感谢互联网,只要你愿意找,没有找不到的免费资源 网址:http://www.studyfox.cn/143.html

  2. MD5的各种实现——也是醉了

    MD5即Message-Digest Algorithm 5(信息-摘要算法5).用于确保信息传输完整一致. 是计算机广泛使用的杂凑算法之中的一个(又译摘要算法.哈希算法),主流编程语言普遍已有MD5 ...

  3. SVN入门2

    TortoiseSVN 以简单易用的安装包的形式发布.双击安装文件并按照提示操作.安装文件会照顾其余的事情.安装结束后不要忘记重启电脑. Import(导入) 导入.导出是以服务器上的版本库为中心的. ...

  4. Laravel5.1 搭建博客 --编译前端文件

    上篇文章写了Gulp编译前端文件,这篇记录下在搭建博客中使用Gulp 1 引入bootstrap和js 1.1 首先先在项目本地安装Bower sudo npm install bower 1.2 创 ...

  5. IT 运行在云端,而云运行在 Linux 上

    导读 IT 正在逐渐迁移到云端.那又是什么驱动了云呢?答案是 Linux. 当连微软的 Azure 都开始拥抱 Linux 时,你就应该知道这一切都已经改变了.不管你接不接受, 云正在接管 IT 已经 ...

  6. GIF动画录制工具(写教程时用的比较小巧的gif工具)

    1  软件小巧实用,只有1m 2  gif效果还可以 3  绿色,无需安装 很多地方能下载,百度就行. 下载地址: http://www.downxia.com/downinfo/41427.html

  7. Spring Cloud Feign 使用OAuth2

    Spring Cloud 微服务架构下,服务间的调用采用的是Feign组件,为了增加服务安全性,server之间互相调用采用OAuth2的client模式.Feign使用http进行服务间的通信,同时 ...

  8. 170310、Jenkins部署Maven多环境项目(dev、beta、prod)的参数设置

    使用Jenkins配置Git+Maven的自动化构建: http://blog.csdn.net/xlgen157387/article/details/50353317 在一个多开发和生产环境的项目 ...

  9. MyBatis映射示例

    resultMap 返回结果的映射 resultMap的id是这个映射的名字,可在查询语句中引用表示此查询返回该结果: t ype是模型对象的类名,也可以写成别名(简化作用) 简化成别名时,需要先注册 ...

  10. Static Import Constant interface

    Static Import https://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html In order t ...