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 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

  1. 2
  2. A:
  3. B:
  4. 4
  5. A:BC
  6. B:ACD
  7. C:ABD
  8. D:BC
  9. 4
  10. A:BCD
  11. B:ACD
  12. C:ABD
  13. D:ABC
  14. 0

Sample Output

  1. 1 channel needed.
  2. 3 channels needed.
  3. 4 channels needed.
  4.  
  5. 实际用时:21min
    原因:....心理准备
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <vector>
  4. using namespace std;
  5. const int maxn=50;
  6. int n;
  7. vector <int > G[maxn];
  8. int color[maxn];
  9. int cnt;
  10. bool dfs(int s,int c){
  11. color[s]=c;
  12. for(int i=0;i<G[s].size();i++){
  13. int to=G[s][i];
  14. if(color[to]==c){color[s]=-1;return false;}
  15. if(color[to]==-1){
  16. for(int i=0;i<=cnt;i++){
  17. if(i==cnt)cnt++;
  18. if(i!=c&&dfs(to,i))break;
  19. }
  20. }
  21. }
  22. return true;
  23. }
  24. char buff[50];
  25. int main(){
  26. while(scanf("%d",&n)==1&&n){
  27. gets(buff);
  28. for(int i=0;i<n;i++){
  29. gets(buff);
  30. G[i].clear();
  31. for(int j=2;buff[j];j++){
  32. G[i].push_back(buff[j]-'A');
  33. }
  34. }
  35. memset(color,-1,sizeof(color));
  36. cnt=1;
  37. for(int i=0;i<n;i++)if(color[i]==-1)dfs(i,0);
  38. if(cnt==1)printf("1 channel needed.\n");
  39. else printf("%d channels needed.\n",cnt);
  40. }
  41. return 0;
  42. }

  

快速切题 poj1129 Channel Allocation的更多相关文章

  1. poj1129 Channel Allocation(染色问题)

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

  2. poj1129 Channel Allocation

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14361   Accepted: 73 ...

  3. POJ-1129 Channel Allocation (DFS)

    Description When a radio station is broadcasting over a very large area, repeaters are used to retra ...

  4. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  5. Channel Allocation

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

  6. Channel Allocation (poj 1129 dfs)

    Language: Default Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12 ...

  7. Channel Allocation(DFS)

    Channel Allocation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) ...

  8. POJ 1129 Channel Allocation(DFS)

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13173   Accepted: 67 ...

  9. POJ 1129 Channel Allocation DFS 回溯

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15546   Accepted: 78 ...

随机推荐

  1. Eclipse的快捷键使用总结

    最近一直在使用Idea开发项目,导致之前一直使用的Eclipse快捷键忘记的差不多了,现在稍微整理了一些,方便以后可以快速切换回来. 常用的Eclipse快捷键总结: Ctrl+S 保存当前正在编辑的 ...

  2. 骗访问量的机房人物列传by xMinh

    作者:$xMinh$ 人物列传·Refun(Aufun,虚凡,人赢) 机房最人赢的人赢,上过表白墙的男人 在宿舍公然开设情感讲座和人赢培训班,教学成果显著,他的徒弟要么gay了要么凉了 认识的人极其广 ...

  3. testlogin

    !/usr/bin/env python coding:utf-8 import requests import unittest import json,urllib class testlogin ...

  4. Cocos2d-x学习笔记(一)环境搭建与项目创建

    可运行的代码可以说明一切问题. 环境需安装VS201x + Python2.7 + Cocos2d-x-2.2.5.(Linux下参考链接:http://www.cocos2d-x.org/wiki/ ...

  5. DATEDIFF 和 DATEADD

    /* DATEDIFF函数计算两个日期之间的小时.天.周.月.年等时间间隔总数 语法 DATEDIFF(interval, date1, date2[, firstdayofweek[, firstw ...

  6. 分析 HTML 代码并提取数据

    在前面的内容中,我们已经学习了 HTML.CSS 和 XPath 的基础知识.从真实世界的网页中获取数据,关键在于如何编写合适的 CSS 或者 XPath 选择器.本节介绍一些确定选择器的简单方法.假 ...

  7. python将xml转换成json数据

    # -*- coding: utf-8 -*- import requests import xmltodict import json def get_response(request_url): ...

  8. 《剑指offer》第三十三题(二叉搜索树的后序遍历序列)

    // 面试题33:二叉搜索树的后序遍历序列 // 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果. // 如果是则返回true,否则返回false.假设输入的数组的任意两个数字都 ...

  9. URAL 1183 Brackets Sequence

    URAL 1183 思路:区间dp,打印路径,详见http://www.cnblogs.com/widsom/p/8321670.html 代码: #include<iostream> # ...

  10. Codeforces 847I - Noise Level

    847I - Noise Level 思路:bfs. 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...