Description

When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a strong signal. However, the channels used by each repeater must be carefully chosen so that nearby repeaters do not interfere with one another. This condition is satisfied if adjacent repeaters use different channels.

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

The input consists of a number of maps of repeater networks. Each map begins with a line containing the number of repeaters. This is between 1 and 26, and the repeaters are referred to by consecutive upper-case letters of the alphabet starting with A. For example, ten repeaters would have the names A,B,C,...,I and J. A network with zero repeaters indicates the end of 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

For each map (except the final one with no repeaters), print a line containing the minumum number of channels needed so that no adjacent channels interfere. The sample output shows the format of this line. Take care that channels is in the singular form when only one channel is required.

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.

【题意】给出n个点,再给出n个点各自相邻的点,相邻的点不能是一个颜色,问至少需要几个颜色;

【思路】

对点i的染色操作:从最小的颜色开始搜索,当i的直接相邻(直接后继)结点已经染过该种颜色时,搜索下一种颜色。

就是说i的染色,当且仅当i的临近结点都没有染过该种颜色,且该种颜色要尽可能小。

参考:http://www.cnblogs.com/lyy289065406/archive/2011/07/31/2122590.html

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef class
{
public:
int next[];
int num;
}point;
int main()
{
int n;
while(~scanf("%d",&n),n)
{
getchar();//回车
point* a=new point[n+];
for(int i=;i<=n;i++)
{
getchar();//当前的点
getchar();//冒号
if(a[i].num<)
{
a[i].num=;//初始化
}
char ch;
while((ch=getchar())!='\n')
{
int tmp=ch%('A'-);//与当前的点相邻的点A->1,B->2...
a[i].next[++a[i].num]=tmp;//存入a[i]的next数组中
}
}
int color[];
memset(color,,sizeof(color));
color[]=;//第一点一定需要一种颜色
int maxcolor=;//至少需要一种颜色
for(int i=;i<=n;i++)
{
color[i]=n+;//先把第i个点的颜色置为最大
int vis[];
memset(vis,false,sizeof(vis));
for(int j=;j<=a[i].num;j++)
{
int k=a[i].next[j];//第i个点的第j个相邻的点
if(color[k]) vis[color[k]]=true;//如果他已经有颜色的话,就把那种颜色标记为true;
}
for(int j=;i<=n;j++)
{
if(!vis[j]&&color[i]>j)//如果j种颜色没用过,并且小于color[i]就把color[i]赋值为j;
{
color[i]=j;
break;
}
}
for(int i=;i<=n;i++)
{
if(maxcolor<color[i])
{
maxcolor=color[i]; }
if(maxcolor==) break;//最大的颜色不超过四种,根据四色定理
} } if(maxcolor==)
printf("1 channel needed.\n");
else printf("%d channels needed.\n",maxcolor);
delete a;
} return ;
}

Channel Allocation_四色定理的更多相关文章

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

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

  2. POJ 1129 Channel Allocation 四色定理dfs

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

  3. Channel Allocation(四色定理 dfs)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10897   Accepted: 5594 Description When ...

  4. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

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

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

  6. Channel Allocation

    Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13231 Accepted: 6774 D ...

  7. poj1129 Channel Allocation(染色问题)

    题目链接:poj1129 Channel Allocation 题意:要求相邻中继器必须使用不同的频道,求需要使用的频道的最少数目. 题解:就是求图的色数,这里采用求图的色数的近似有效算法——顺序着色 ...

  8. poj 1129 Channel Allocation ( dfs )

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

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

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

随机推荐

  1. hdu 3948 Portal (kusral+离线)

    Portal Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  2. poj1190 生日蛋糕(深搜+剪枝)

    题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到 ...

  3. 安卓手机修改host

    电脑修改 注意:usb设置为调试模式 1.手机必须先root,小米可以安卓开发版系统即可 2.安卓 adb工具(android debug bridge) 3.依次执行下面的命令 1.adb root ...

  4. Bag of mice(CodeForces 148D )

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  5. 解决tomcat部署多个虚拟机时报IllegalStateException: Web app root system property already set to 的问题

    解决tomcat部署多个虚拟机时报IllegalStateException: Web app root system property already set to 的问题 在web.xml中添加如 ...

  6. Java:标示符 基本数据类型

    标示符: 在程序中自定义的一些名称,例如:变量.类名.方法名…… 组成有数字0~9.大小写英文字母.“$”和下划线“_”组成,且不能由数字开头,以及不能使用java已使用和保留的关键字. Java中的 ...

  7. js 数组 转

    1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...

  8. HTML元素,属性,基础标签

    元素,属性 元素 html有父元素和子元素,被包含的叫子元素,如html是head的父元素,他们是父子关系,head和body是兄弟关系 <html> <head></h ...

  9. AlarmManager手机闹钟简介

    1.void set(int type , long triggerAtTime , PendingIntent operation ) : 设置在 triggerAtTime时间启动由operati ...

  10. PHP后台

    一.ajax提交表单 先引入ajax.js function ajax(url, fnSucc, fnFaild) { //1.创建Ajax对象 var oAjax=null; if(window.X ...