题目:http://poj.org/problem?id=1129

题意:求最小m,使平面图能染成m色,相邻两块不同色
由四色定理可知顶点最多需要4种颜色即可。我们于是从1开始试到3即可。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<iomanip>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std; int n,G[][],vis[][],ans;
void pri()
{
int f=-;
for(int i=; i<; i++)
{
for(int j=; j<n; j++)
if(vis[j][i]&&f<i)
f=i;
}
if(ans>f)
ans=f;
}
void dfs(int x)
{
if(x>=n)
{
pri();
return;
}
for(int i=; i<; i++)
{
int f=;
for(int j=; j<n; j++)
{
if((G[j][x]&&vis[j][i])||(G[x][j]&&vis[j][i]))
{
f=;
break;
}
}
if(f)
{
vis[x][i]=;
dfs(x+);
vis[x][i]=;
}
}
}
int main()
{
char s[];
while(cin>>n&&n)
{
memset(G,,sizeof(G));
memset(vis,,sizeof(vis));
for(int i=; i<n; i++)
{
cin>>s;
int u=s[]-;
for(int j=; s[j]; j++)
{
int v=s[j]-;
G[u][v]=;
}
}
ans=;
vis[][]=;
dfs();
ans+=;
if(ans == )
printf("1 channel needed.\n");
else
printf("%d channels needed.\n",ans);
}
return ;
}

poj 1129 Channel Allocation ( dfs )的更多相关文章

  1. POJ 1129 Channel Allocation DFS 回溯

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

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

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

  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

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

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

  6. poj 1129 Channel Allocation

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

  7. PKU 1129 Channel Allocation(染色问题||搜索+剪枝)

    题目大意建模: 一个有N个节点的无向图,要求对每个节点进行染色,使得相邻两个节点颜色都不同,问最少需要多少种颜色? 那么题目就变成了一个经典的图的染色问题 例如:N=7 A:BCDEFG B:ACDE ...

  8. Channel Allocation (poj 1129 dfs)

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

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

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

随机推荐

  1. php框架-yii

    安装 修改权限问题:runtime;web/assets(mac上) 配置cookie加密串 config/web.php 修改cookieValidationKey L12 配置数据库 控制器: 默 ...

  2. MongoDB之【增加用户认证、增加用户、删除用户、修改用户密码、读写权限、只读权限】

    说明:增加用户是针对数据库进行操作 1.进入到数据库 use dbname 2.针对当前数据库添加用户 权限是针对当前数据 1.添加并验证用户 > use admin > db.addUs ...

  3. “Cache-control”常见的取值有private、no-cache、max-age、must-revalidate等

    网页的缓存由HTTP消息头中的"Cache-Control" 来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为pri ...

  4. android studio笔记之编译运行错误

    错误类型: Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major错 ...

  5. HTTP的长短连接、长短轮询的区别(转载)

    引言 最近刚到公司不到一个月,正处于熟悉项目和源码的阶段,因此最近经常会看一些源码.在研究一个项目的时候,源码里面用到了HTTP的长轮询.由于之前没太接触过,因此LZ便趁着这个机会,好好了解了一下HT ...

  6. [Git]代码管理工具简单使用

    1 Git简介 Git是分布式的版本控制系统,是Linux内核开发者林纳斯·托瓦兹(Linus Torvalds)为更好地管理Linux内核开发而设计.与CVS.Subversion一类的集中式版本控 ...

  7. spring mvc 初步接触学习笔记

    1.使用maven导入spring mvc web 的jar 包 最新语句 <dependency> <groupId>org.springframework</grou ...

  8. epoll_create, epoll_ctl和epoll_wait

    参考代码 #include <iostream> #include <sys/socket.h> #include <sys/epoll.h> #include & ...

  9. General Palindromic Number (进制)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  10. Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey

    Mapped Statements collection does not contain value for后面是什么类什么方法之类的: 错误原因有几种: 1.mapper.xml中没有加入name ...