Channel Allocation_四色定理

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[30];
    int num;
}point;
int main()
{
    int n;
    while(~scanf("%d",&n),n)
    {
        getchar();//回车
        point* a=new point[n+1];
        for(int i=1;i<=n;i++)
        {
            getchar();//当前的点
            getchar();//冒号
            if(a[i].num<0)
            {
                a[i].num=0;//初始化
            }
            char ch;
            while((ch=getchar())!=‘\n‘)
            {
                int tmp=ch%(‘A‘-1);//与当前的点相邻的点A->1,B->2...
                a[i].next[++a[i].num]=tmp;//存入a[i]的next数组中
            }
        }
        int color[30];
        memset(color,0,sizeof(color));
        color[1]=1;//第一点一定需要一种颜色
        int maxcolor=1;//至少需要一种颜色
       for(int i=1;i<=n;i++)
        {
            color[i]=n+1;//先把第i个点的颜色置为最大
            int vis[30];
            memset(vis,false,sizeof(vis));
            for(int j=1;j<=a[i].num;j++)
            {
                int k=a[i].next[j];//第i个点的第j个相邻的点
                if(color[k]) vis[color[k]]=true;//如果他已经有颜色的话,就把那种颜色标记为true;
            }
            for(int j=1;i<=n;j++)
            {
                if(!vis[j]&&color[i]>j)//如果j种颜色没用过,并且小于color[i]就把color[i]赋值为j;
                {
                    color[i]=j;
                    break;
                }
            }
            for(int i=1;i<=n;i++)
            {
                if(maxcolor<color[i])
                {
                    maxcolor=color[i];

                }
                if(maxcolor==4) break;//最大的颜色不超过四种,根据四色定理
            }

        }

        if(maxcolor==1)
            printf("1 channel needed.\n");
        else printf("%d channels needed.\n",maxcolor);
delete a;
    }

    return 0;
}
时间: 2024-10-12 21:53:38

Channel Allocation_四色定理的相关文章

poj 1129 Channel Allocation(四色定理)

1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<queue> 7 #include<algorithm> 8 #include<map> 9 #include<iomanip> 10 #include<climits>

DFS/四色定理/poj 1129 Channel Allocation

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int n; 5 int a[30][30]; 6 int c[30]; 7 8 bool pd(int x,int color) 9 { 10 for (int i=1;i<x;i++) 11 if (a[x][i]==1 && c[i]==color) return false; 12 return true; 13 } 14 1

POJ 1129-Channel Allocation(四色定理+迭代深搜)

题目链接:传送门 题意:n个信号站,给出连接情况,要用信号覆盖所有信号站,要求相连的信号站不能用同一个信号. 等价问题==无向图染色==四色定理(每个平面地图都可以只用四种颜色来染色,而且没有两个邻接的区域颜色相同.已证明) 思路:深搜一条路(枚举颜色,判断当前点用已有的颜色能不能染,如不能则加一种颜色,判断强判就行了),搜到头答案就出来了..然后返回就可以了 注意单复数.. #include <algorithm> #include <iostream> #include <

POJ 1129 Channel Allocation(暴力搜--涂色问题)

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13295   Accepted: 6806 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 s

poj1129 Channel Allocation(染色问题)

题目链接:poj1129 Channel Allocation 题意:要求相邻中继器必须使用不同的频道,求需要使用的频道的最少数目. 题解:就是求图的色数,这里采用求图的色数的近似有效算法——顺序着色算法(实质是一种贪心策略:在给任何一个顶点着色时,采用其邻接顶点中没有使用的,编号最小的颜色). 注:中继器网络是一个平面图,即图中不存在相交的边. 看讨论后发现这组数据,AC代码没过orz: 6 A:BEF B:AC C:BD D:CEF E:ADF F:ADE 正确答案应该是3 : A(1)B(

四色定理+dfs(poj 1129)

题目:Channel Allocation 题意:要求A:BCD,A与B,C,D都不相同,求不同的值,典型的四色定理: #include <iostream> #include <algorithm> #include <stdlib.h> #include <time.h> #include <cmath> #include <cstdio> #include <string> #include <cstring&

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

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 7427 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 s

【转】POJ1129-Channel Allocation:DFS 四色定理 剪枝枚举

#include<iostream> #include<cstdio> #include<cstring> using namespace std; bool g[26][26]; int used[26]; int n; //id 是当下着色结点 color是限制的颜色数量 bool dfs(int id, int color) { bool flag;// 标记 节点id 染第i种色成功与否 for(int i=0; i<color; i++) { flag=

golang控制channel的出入口

golang控制channel的出入口 我们常常使用channel来在多个goroutine之间做数据通讯,但是chan作为函数的入参我们应该怎么写呢?也许有人觉得这个问题比较傻,不过这个还真的是我今天才知道的. 首先我们看看下面的代码: func main() { c := make(chan int) go in(c) go out(c) time.Sleep(time.Second) } func in(c chan int) { for i := 0; i < 10; i++ { c <