【poj1129】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. 

Source

Southern African 2001

题解

题意,给定一个N个节点的无向图,求颜色最少的染色方案使相邻点的颜色不同

由于n<27,可以直接搜索

记下相邻的节点,从小到大枚举颜色,当相邻节点用过的时候,这个颜色就不能再用。

#include<iostream>
#include<cstdio>
using namespace std;
struct nod
{
    int t[27],s;
}node[27];
int n;
int main()
{
    while (cin>>n)
    {
        if (!n) break;
        getchar();
        for (int i=1;i<=n;i++)
        {
            char ch;
            ch=getchar();
            getchar();
            node[i].s = 0;
            while ((ch = getchar() )!=‘\n‘)
            {
                int j = ch - ‘A‘+1;
                node[i].t[++node[i].s] = j;
            }
        }
        int color[27] = {0};
        color[1] = 1;//节点i的颜色
        int sumcolor = 1;
        for (int i=2;i<=n;i++)
        {
            bool vis[27] = {false};
            color[i] =i + 1;
            for (int j = 1;j<=node[i].s;j++)//枚举后继,后继已经染色,那么这种颜色不可用
                if (color[j])
                    vis[color[j]] = true;
            for (int k=1;k<=sumcolor+1;k++)
                if (!vis[k] && k<color[i])
                {
                    color[i] = k;
                    break;
                }
            if (color[i]>sumcolor)    sumcolor = color[i];
        }
        printf("%d ",sumcolor);
        if (sumcolor>1) printf("channels needed.\n");
            else printf("channel needed.\n");
    }
}
时间: 2024-10-25 01:16:52

【poj1129】Channel Allocation的相关文章

【转】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=

【转】Netty那点事(三)Channel中的Pipeline

[原文]https://github.com/code4craft/netty-learning/blob/master/posts/ch3-pipeline.md Channel是理解和使用Netty的核心.Channel的涉及内容较多,这里我使用由浅入深的介绍方法.在这篇文章中,我们主要介绍Channel部分中Pipeline实现机制.为了避免枯燥,借用一下<盗梦空间>的“梦境”概念,希望大家喜欢. 一层梦境:Channel实现概览 在Netty里,Channel是通讯的载体,而Chann

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(

【JAVA】【NIO】3、Java NIO Channel

Java NIO和流量相似,但有些差异: ·通道可读写,流仅支持单向.读或写 ·异步通道读取 ·通道读写器,他们是和Buffer交替 道的实现 下面是Java NIO中最重要的通道的实现: ·FileChannel ·DatagramChannel ·SocketChannel ·ServerSocketChannel FileChannel从文件读数据或写进文件 DatagramChannel通过UDP在网络上读写数据 SocketChannel通过TCP在网络上读写数据 ServerSock

快速切题 poj1129 Channel Allocation

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

【转】优秀的Java程序员必须了解GC的工作原理

一个优秀的Java程序员必须了解GC的工作原理.如何优化GC的性能.如何与GC进行有限的交互,因为有一些应用程序对性能要求较高,例如嵌入式系统.实时系统等,只有全面提升内存的管理效率 ,才能提高整个应用程序的性能.一个优秀的Java程序员必须了解GC的工作原理.如何优化GC的性能.如何与GC进行有限的交互,因为有一些应用程序对性能要求较高,例如嵌入式系统.实时系统等,只有全面提升内存的管理效率 ,才能提高整个应用程序的性能.本篇文章首先简单介绍GC的工作原理之后,然后再对GC的几个关键问题进行深

【转】《windows核心编程》读书笔记

这篇笔记是我在读<Windows核心编程>第5版时做的记录和总结(部分章节是第4版的书),没有摘抄原句,包含了很多我个人的思考和对实现的推断,因此不少条款和Windows实际机制可能有出入,但应该是合理的.开头几章由于我追求简洁,往往是很多单独的字句,后面的内容更为连贯. 海量细节. 第1章    错误处理 1.         GetLastError返回的是最后的错误码,即更早的错误码可能被覆盖. 2.         GetLastError可能用于描述成功的原因(CreatEvent)

论文阅读(BaiXiang——【CVPR2016】Multi-Oriented Text Detection with Fully Convolutional Networks)

BaiXiang--[CVPR2016]Multi-Oriented Text Detection with Fully Convolutional Networks 目录 作者和相关链接 方法概括 方法细节 创新点和贡献 实验结果 问题讨论 总结与收获点 作者和相关链接 作者: paper下载 方法概括 Step 1--文本块检测: 先利用text-block FCN得到salient map,再对salient map进行连通分量分析得到text block: Step 2--文本线形成:

【RMAN】利用备份片还原数据库(中)

[RMAN]利用备份片还原数据库 [RMAN]利用备份片还原数据库(上): http://blog.itpub.net/26736162/viewspace-1621581/ 在上一篇blog中我们介绍了采用dbms_backup_restore来找回控制文件并恢复整个数据库的方法,本篇blog我们介绍采用创建临时库来找回控制文件的方法. 1.1.1  方法二:尝试采用创建临时库来找回控制文件 由于RMAN必须工作在MOUNT模式,所有的数据文件都丢失,无法通过只重建控制文件将其启动到MOUNT