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 ...
随机推荐
- LINQ / LINQ to SQL / LINQ to XXX 它们到底有什么区别
LINQ是新生事物,不过从不少文章和讨论上看来,这方面的概念也已经有点混沌不清了.因此我们经常可以看到这样的话: LINQ只能将数据表与实体属性一一对应…… LINQ开发指南:在LINQ中进行数据库字 ...
- JavaScript学习笔记-基础语法、类型、变量
基础语法.类型.变量 非数字值的判断方法:(因为Infinity和NaN他们不等于任何值,包括自身) 1.用x != x ,当x为NaN时才返回true; 2.用isNaN(x) ,当x为NaN或 ...
- 每天checklist所用到的T-CODE
1.1重点检查 作业 事务码 检查过程 检查R/3系统是否已经启动 · 登录到R/3系统 检查每日备份是否正常 DB12-Backup Logs:Overview · 检查数据库备份 · 检查数据库备 ...
- SharePoint 2013 Error - File names can't contain the following characters: & " ? < > # {} % ~ / \.
错误截图: 错误信息: --------------------------- Message from webpage --------------------------- File names ...
- Powershell 学习笔记【持续更新】
1. 判断一个对象是不是空可以用 $null来比较 2. 判断一个字符串是不是空的: [string]::IsNullOrEmpty(...) 3. 在powershell中把结果输出为一个CSV格式 ...
- APP One Link ,android and ios qrcode merge as One QRCode and one short link
Adroid and ios qrcode merge as One QRCode and one short link is publish , the web site is www.appone ...
- MVC的优点及不足之处
1. MVC的优点 (1) 可以为一个模型在运行时同时建立和使用多个视图.变化-传播机制可以确保所有相关的视图及时得到模型数据变化,从而使所有关联的视图和控制器做到行为同步. (2) 视图与控制器的可 ...
- Android自定义控件2--优酷菜单界面初始化
本文开始将逐步去实现下面优酷菜单的效果: 本文地址:http://www.cnblogs.com/wuyudong/p/5912538.html,转载请注明源地址. 本文首先来实现优酷菜单界面初始化工 ...
- js 模仿块级作用域(私有作用域)、私有变量
function outputNumbers(count){ var privateVariable = 10;//私有/局部变量,函数外部不能被访问 publicVariable = 20;//全局 ...
- Scrum敏捷项目管理精要
1. 简介: 敏捷项目管理在我们国家起步比较晚,成功运用的项目不多 百分之六十五的敏捷项目用户为scrum 2.互联网时代的特征,雷军的话: 专注,极致,口碑,快(敏捷项目开发就是要快速) 3.敏捷开 ...