Language:
Default
Channel Allocation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12367   Accepted: 6325

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.

Source

题意:给你一个n,代表电台的数量。电台的编号是从A到Z。然后给你他们之间的邻接关系。让你求出最小须要的频率数。要求随意两个相邻的电台之间不同意用同一频率。

思路:数据不大,最多26。dfs暴力,用邻接表存图,color[x]=i表示x号电台使用i频率。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #include <map>
  8. #include <stack>
  9. #include <vector>
  10. #include <set>
  11. #include <queue>
  12. #pragma comment (linker,"/STACK:102400000,102400000")
  13. #define maxn 30
  14. #define MAXN 2005
  15. #define mod 1000000009
  16. #define INF 0x3f3f3f3f
  17. #define pi acos(-1.0)
  18. #define eps 1e-6
  19. typedef long long ll;
  20. using namespace std;
  21.  
  22. struct Edge
  23. {
  24. int u,v;
  25. int next;
  26. }edge[maxn*maxn];
  27.  
  28. int N,edgenum;
  29. int head[maxn];
  30. int color[maxn];
  31.  
  32. void addedge(int u,int v)
  33. {
  34. edge[edgenum].v=v;
  35. edge[edgenum].next=head[u];
  36. head[u]=edgenum++;
  37. }
  38.  
  39. bool ISok(int x)
  40. {
  41. for (int i=head[x];i!=-1;i=edge[i].next)
  42. {
  43. if (color[x]==color[edge[i].v])
  44. return false;
  45. }
  46. return true;
  47. }
  48.  
  49. bool dfs(int point_num,int color_num)
  50. {
  51. if (point_num>N)
  52. return true;
  53. for (int i=1;i<=color_num;i++)
  54. {
  55. color[point_num]=i;
  56. if (ISok(point_num))
  57. {
  58. if (dfs(point_num+1,color_num))
  59. return true;
  60. }
  61. color[point_num]=0;
  62. }
  63. return false;
  64. }
  65.  
  66. int main()
  67. {
  68. while (scanf("%d",&N)&&N)
  69. {
  70. getchar();
  71. memset(head,-1,sizeof(head));
  72. memset(color,0,sizeof(color));
  73. edgenum=0;
  74. for (int i=1;i<=N;i++)
  75. {
  76. getchar();
  77. getchar();
  78. char ch;
  79. while (ch=getchar())
  80. {
  81. if (ch=='\n')
  82. break;
  83. addedge(i,ch-'A'+1);
  84. }
  85. }
  86. for (int i=1;i<=N;i++)//从1~N枚举颜色种类数
  87. {
  88. if (dfs(1,i))
  89. {
  90. if (i==1)//注意一个频率是用channel。多个时用channels
  91. printf("1 channel needed.\n");
  92. else
  93. printf("%d channels needed.\n",i);
  94. break;
  95. }
  96. }
  97. }
  98. return 0;
  99. }
  100. /*
  101. 2
  102. A:
  103. B:
  104. 4
  105. A:BC
  106. B:ACD
  107. C:ABD
  108. D:BC
  109. 4
  110. A:BCD
  111. B:ACD
  112. C:ABD
  113. D:ABC
  114. 0
  115. */

版权声明:本文博主原创文章,博客,未经同意不得转载。

Channel Allocation (poj 1129 dfs)的更多相关文章

  1. poj 1129(dfs+图的四色定理)

    题目链接:http://poj.org/problem?id=1129 思路:根据图的四色定理,最多四种颜色就能满足题意,使得相邻的两部分颜色不同.而最多又只有26个点,因此直接dfs即可. #inc ...

  2. POJ 1129 Channel Allocation(DFS)

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

  3. POJ 1129 Channel Allocation DFS 回溯

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

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

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

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

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

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

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

  7. Channel Allocation(DFS)

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

  8. poj1129 Channel Allocation

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

  9. poj1129 Channel Allocation(染色问题)

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

随机推荐

  1. android 自己定义通知栏遇到的问题

    首先看报错信息: E/AndroidRuntime(12220): FATAL EXCEPTION: main E/AndroidRuntime(12220): Process: gn.com.and ...

  2. Java提高篇(三二)-----List总结

    前面LZ已经充分介绍了有关于List接口的大部分知识,如ArrayList.LinkedList.Vector.Stack,通过这几个知识点能够对List接口有了比較深的了解了.仅仅有通过归纳总结的知 ...

  3. malloc函数的一种简单的原理性实现

    malloc()是C语言中动态存储管理的一组标准库函数之一.其作用是在内存的动态存储区中分配一个长度为size的连续空间.其参数是一个无符号整形数,返回值是一个指向所分配的连续存储域的起始地址的指针 ...

  4. 在IIS上发布一个WebService,再发布一个网站调用这个WebService(实例)

    首先描述一下先决条件:IIS可用,VS2005可用. 好,现在开始: 首先写一个WebService并把它发布到IIS上: 在IIS上的默认网站下新建一个“虚拟目录”,取名为“webservice1” ...

  5. Oracle Dataguard 介绍

    Oracle DataGuard介绍 一. DataGuard的基本原理 当某次事务处理对生产数据库中的数据作出更改时,Oracle数据库将在一个联机重做日志文件里记录此次更改.在DataGuard中 ...

  6. 设计模式六大原则(4):接口隔离原则(Interface Segregation Principle)

    接口隔离原则: 使用多个专门的接口比使用单一的总接口要好. 一个类对另外一个类的依赖性应当是建立在最小的接口上的. 一个接口代表一个角色,不应当将不同的角色都交给一个接口.没有关系的接口合并在一起,形 ...

  7. 打开或导入项目,从脱机 Outlook 数据文件 (.ost)

    打开或导入项目,从脱机 Outlook 数据文件 (.ost) Microsoft Outlook 2010 doesn\rquote t 支持手动打开或导入项目,从一个 脱机 Outlook 数据文 ...

  8. u-boot: Error: Can&#39;t overwrite &quot;ethaddr&quot;

    When try to execute following command, It reports error as following: --->8--- U-Boot> setenv ...

  9. Ubuntu 上 hi3531 交叉编译环境 arm-hisiv100nptl-linux 建设过程

    安装SDK 1.Hi3531 SDK包的位置     在"Hi3531_V100R001***/01.software/board"夹,你可以看到一个 Hi3531_SDK_Vx. ...

  10. php按照奖品百分比随机抽奖代码分析

    这个忘记从哪里copy过来了 /** * 概率算法 * @param array $probability * @return integer|string */ function get_rand( ...