POJ1129Channel Allocation[迭代加深搜索 四色定理]
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14601 | Accepted: 7427 |
Description
Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.
Input
Following the number of repeaters is a list of adjacency relationships. Each line has the form:
A:BCDH
which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form
A:
The repeaters are listed in alphabetical order.
Note that the adjacency is a symmetric relationship; if A is adjacent to B, then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not have any line segments that cross.
Output
Sample Input
2
A:
B:
4
A:BC
B:ACD
C:ABD
D:BC
4
A:BCD
B:ACD
C:ABD
D:ABC
0
Sample Output
1 channel needed.
3 channels needed.
4 channels needed.
Source
题意:给一个地图涂色,最多几种颜色就可以
根据四色定理,迭代加深搜索就行了,最多到四种
//
// main.cpp
// poj2157
//
// Created by Candy on 9/30/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N=;
int n;
char s[N];
struct edge{
int v,ne;
}e[N*N];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
}
int col[N],num=,has[N],maxcol,flag;
inline bool check(int u){
for(int i=h[u];i;i=e[i].ne)
if(col[e[i].v]==col[u]) return ;
return ;
}
void dfs(int d){
if(d>n) {flag=;return;}
if(flag) return;
for(int cur=;cur<=maxcol;cur++){
col[d]=cur;
if(check(d)) dfs(d+);
col[d]=;
} }
int main(int argc, const char * argv[]) {
while(scanf("%d",&n)!=EOF&&n){
cnt=;memset(h,,sizeof(h));memset(col,,sizeof(col));
for(int i=;i<=n;i++){
scanf("%s",s+);
int len=strlen(s+);int u=s[]-'A'+;
for(int j=;j<=len;j++){
ins(u,s[j]-'A'+);
}
}
for(maxcol=;maxcol<=;maxcol++){
flag=;
dfs();
if(flag){
if(maxcol==)printf("1 channel needed.\n");
else printf("%d channels needed.\n",maxcol);
break;
}
}
} }
POJ1129Channel Allocation[迭代加深搜索 四色定理]的更多相关文章
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神
题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...
- C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains
此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...
- UVA11212-Editing a Book(迭代加深搜索)
Problem UVA11212-Editing a Book Accept:572 Submit:4428 Time Limit: 10000 mSec Problem Description ...
随机推荐
- HTML5移动开发学习笔记
最近做webapp项目过程中,发现坑还是挺多的,以下是网络收集一些开发中的常见问题及知识汇总,以便查阅,慢慢更新:). meta 基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 < ...
- iOS:JSON格式字符串转字典,字典转JSON格式字符串
在iOS开发中,和服务器交互中,经常用到字典和JSON格式字符串相互转换. 代码如下: 1.JSON格式字符串转字典 + (NSDictionary *)dictionaryWithJsonStrin ...
- 简单代码在ABAP中实现声音的播放
这段代码的功能是在SAP里面实现声音的播放,可以用作程序提醒功能,和SAP里面’噹噹噹’那个声音的意思差不多.将来在项目中遇到客户想要SAP ABAP发出一点声音的时候就可以参考一下这个程序. REP ...
- php强制转换类型和CMS远程管理插件的危险
远程管理插件是十分受WordPress站点管理员欢迎的工具,它们允许用户同时对多个站点执行相同的操作,如,更新到最新的发行版或安装插件.然而,为了实现这些操作,客户端插件需要赋予远程用户很大的权限.因 ...
- sharepoint2013- Office web app server2013详细的安装和部署
前提条件 Office web app server2013不能跟sharepoint server2013安装在同一台服务器上,如果安装在同一台服务器上将提示如下错误: 后来查询资料: 按照官方文 ...
- 100个高质量Java开发者博客
ImportNew注:原文中还没有100个.作者希望大家一起来推荐高质量的Java开发博客,然后不段补充到这个列表.欢迎你也参与推荐优质的Java开发博客.(声明一下:我们的数学不是体育老师教的!:) ...
- mysql java Cannot find the driver in the classpath!
确保你的mysql-connector-java有没有配置好, 如何配置: 从oracle上把mysql-connector-java下下来放到java_home里面的extensions里面,然后在 ...
- Android音乐播放器源码(歌词.均衡器.收藏.qq5.0菜单.通知)
一款Android音乐播放器源码,基本功能都实现了 qq5.0菜单(歌词.均衡器.收藏.qq5.0菜单.通知) 只有向右滑动出现,菜单键和指定按钮都还没有添加. 源码下载:http://code.66 ...
- sql 如果关联表 没有值 设置 默认值
SELECT *FROM ( SELECT t.task_name, t.status AS task_status, coalesce( r.task_ref_id, 999 ) AS task_ ...
- HTML5设计网页熔岩灯导航(navigation bar)插件 已经加上完整源代码
导航栏(navigation bar): 1.指位于页眉区域的,在页眉横幅图片上边或下边的一排水平导航按钮,它起着链接博客的各个页面的作用. 2.网页设计中不可缺少的部分,它是指通过一定的技术手段,为 ...