题目链接: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+图的四色定理)的更多相关文章

  1. Channel Allocation (poj 1129 dfs)

    Language: Default Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12 ...

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

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

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

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

  4. POJ 1129 Channel Allocation 四色定理dfs

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

  5. 四色定理+dfs(poj 1129)

    题目:Channel Allocation 题意:要求A:BCD,A与B,C,D都不相同,求不同的值,典型的四色定理: #include <iostream> #include <a ...

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

  7. poj 1129 Channel Allocation ( dfs )

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

  8. POJ 1129 Channel Allocation(DFS)

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

  9. POJ 1129 Channel Allocation DFS 回溯

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

随机推荐

  1. iOS AVPlayer视频播放器

    代码地址如下:http://www.demodashi.com/demo/11168.html 一.运行效果 二.实现过程 ①.创建播放器avPlayer //创建播放器 url = [url str ...

  2. 【微信小程序】转载:微信小程序之购物车功能

    前言 以往的购物车,基本都是通过大量的 DOM 操作来实现.微信小程序其实跟 vue.js 的用法非常像,接下来就看看小程序可以怎样实现购物车功能. 需求 先来弄清楚购物车的需求. 单选.全选和取消, ...

  3. Struts2中防止表单重复提交,global-results定义全局结果处理

    1.在表单中加入<s:token/>标签 2.在动作类中加入token的拦截器 <!--如果单单写 name="token" 会丧失 defaultStack 拦 ...

  4. 深入浅出REST架构 REST架构概述

    http://www.nowamagic.net/librarys/veda/detail/885何为REST? REST是英文Representational State Transfer的缩写,中 ...

  5. unity3d协同不同设备的代码

    unity3d的脚本代码中,Update()函数每一帧都会运行一次. 假设有这两台设备:一台超级四路泰坦计算机,一台旧手机,它们一起运行一个赛跑游戏,Update()函数每一帧运行一次,游戏中的小人就 ...

  6. AAAA block

    [self AAAA:^(BOOL isSuccessed, id userInfo, NSString *errorMsg) { NSLog(@"AAAA: %d, userInfo: % ...

  7. Paper Reading 1 - Playing Atari with Deep Reinforcement Learning

    来源:NIPS 2013 作者:DeepMind 理解基础: 增强学习基本知识 深度学习 特别是卷积神经网络的基本知识 创新点:第一个将深度学习模型与增强学习结合在一起从而成功地直接从高维的输入学习控 ...

  8. FZU 2087 统计树边【MST相关】

     Problem 2087 统计树边 Accept: 212    Submit: 651 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Prob ...

  9. web前端规范

    无论是从技术角度还是开发视角,对于web前端开发规范文档都有一定规范,本文就css3和html5的发展前景总结了一系列的web开发文档,仅供大家参考. 规范目的:为提高团队协作效率, 便于后台人员添加 ...

  10. worker

    <script id="worker" type="app/worker"> addEventListener('message', functio ...