poj 1129 Channel Allocation ( dfs )
题目: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 )的更多相关文章
- POJ 1129 Channel Allocation DFS 回溯
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15546 Accepted: 78 ...
- 迭代加深搜索 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
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...
- 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
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 ...
随机推荐
- Clone table header and set as the first element, and replace header's th with td
Clone table header and replace header's th with td var tableHeaderRow = '#tableId tbody tr:nth-child ...
- 完美解决IE6中fixed抖动问题的方法
我们可以通过position:fixed来实现元素的固定效果,如网页中底部的"回到顶部菜单",底部的toolbar,对联广告等等,可惜fixed属性在IE6及以下是不支持的.通常的 ...
- MySql 服务端与客户端下载地址
mysql官网的注册,要上传户口,才能下载. 在网上搜了个下载地址. mysql-5.6.8-rc http://mysql.stu.edu.tw/Downloads/MySQL-5.6/mysql- ...
- ARM-Linux S5PV210 UART驱动(6)----platform device的添加
开发板是飞凌OK210 arch/arm/mach-s5pv210/mach-smdkc110.c 首先是UART的寄存器默认配置信息: /* Following are default values ...
- ContextMenuStrip控件
设置单击窗体右键菜单 注意如果想在form1中显示右键菜单,那么要设置form1的属性
- iOS中touches事件,addtarget ...action和GestureRecognizer详解
刚学完uiview,uicontrol类,许多人知道 touchesBegain,touchesMoved,touchesEnd,GestureRecognizer的用途,但仔细考虑这些事件之间的关系 ...
- poj 3463 Sightseeing( 最短路与次短路)
http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- mac忘记密码的解决办法
开机, 启动时按"cmd+S".这时,你会进入Single User Model,出现像DOS一样的提示符 #root>.请在#root>下 输入 (注意空格, 大小写 ...
- iphone绘图的几个基本概念CGPoint、CGSize、CGRect、CGRectMake、window(窗口)、视图(view)
我一般情况下不会使用interface builder去画界面,而是用纯代码去创建界面,不是装B,而是刚从vi转到xcode不久,不太习惯interface builder而已.当然如果需要我也会使用 ...
- IDEA 创建maven-web project失败一例
今天使用IDEA创建WEB-APP总是失败,经排查原来是MAVEN环境没配置好!!! 配置: M2_HOME--->maven解压目录 path---->%M2_HOME%\bin\