http://poj.org/problem?id=1129

 import java.util.*;
import java.math.*;
public class Main {
public static boolean flag=false;
public static int ans=0;
public static void main(String []args)
{
Scanner cin=new Scanner(System.in);
int n;
String str;
while(cin.hasNext())
{
ans=1;
int [][]g=new int[100][100];
int []hash=new int[100];
n=cin.nextInt();
if(n==0) break;
for(int i=0; i<n; i++)
{
str=cin.next();
for(int j=2; j<(int)str.length(); j++)
{
g[str.charAt(0)-'A'][str.charAt(j)-'A']=1;
}
}
flag=false;
dfs(0,1,g,n,hash);
if(ans==1)
{
System.out.println("1 channel needed.");
}
else
{
System.out.println(ans+" channels needed.");
}
}
}
public static int deal(int id,int co,int n,int g[][],int hash[])
{
for(int i=0; i<n; i++)
{
if(g[id][i]==1&&co==hash[i])
{
return 0;
}
}
return 1;
}
public static void dfs(int num,int m1,int g[][],int n,int hash[])
{
if(flag) return;
if(num>=n)
{
flag=true;
return;
}
for(int i=1; i<=m1; i++)
{
if(deal(num,i,n,g,hash)==1)
{
hash[num]=i;
dfs(num+1,m1,g,n,hash);
hash[num]=0;
}
}
if(flag==false)
{
ans++;
dfs(num,m1+1,g,n,hash);
}
}
}

poj 1129 Channel Allocation的更多相关文章

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

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

  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 ( dfs )

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

  5. POJ 1129 Channel Allocation 四色定理dfs

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

  6. poj 1129 Channel Allocation(图着色,DFS)

    题意: N个中继站,相邻的中继站频道不得相同,问最少需要几个频道. 输入输出: Sample Input 2 A: B: 4 A:BC B:ACD C:ABD D:BC 4 A:BCD B:ACD C ...

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

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

  8. Channel Allocation (poj 1129 dfs)

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

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

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

随机推荐

  1. BZOJ 1001 狼抓兔子

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子 ...

  2. bzoj2741(分块+可持久化Trie)

    题意中文我就不说了 解析: 分块+可持久化Trie,先得到前缀异或值,插入到Trie中,然后分块,对每一块,处理出dp[i][j](i代表第几块,j代表第几个位置),dp[i][j]代表以第i块开始的 ...

  3. mac下识别国产android手机

    mac下识别国产android手机困扰了我很久,这几天总算在google帮助下找到了解决方法. 在~/.android/下找到adb_usb.ini,如果不存在则创建.通过“系统信息”查看到插入的an ...

  4. 用CSS3实现对图片的放大效果

    用CSS3对图片放大效果 .right_div .topicons li a:hover img{     -webkit-transform:scale(1.5,1.5);     -moz-tra ...

  5. 设置root远程连接ubuntu上mysql

    1.安装mysql,如果是root用户,直接执行一下命令.如果非root,则需要用sudo命令 a. apt-get install mysql-server b. apt-get isntall m ...

  6. [ES7] Object.observe + Microtasks

    ES6: If you know about the Javascirpt's event loop. You know that any asyns opreations will be throw ...

  7. UCTF Final-Hackventure

    抽出世间将UCTF Final中的hackventure给记录下,算是个总结.题目是有一个游戏,游戏地图是随机生成的,用户可以攻打Server,如果3个Server都被攻占的话,那么用户就赢了,但是并 ...

  8. 继承之后的使用注意事项_ArrayStoreException

    今天在看Core In Java第五章节时,看到一个很感兴趣的知识点,如下: 在Java中,子类数组的引用可以转换成超类数组的引用,而不需要采用强制转换.但是,在超类数组的引用添加超类类型引用对象之后 ...

  9. JavaScript自我学习之解析与执行

    如果想要学好JavaScript那么我们首先必须要知道浏览器JavaScript引擎是如何解释执行JavaScript代码的,作为一名菜鸟,从自己学习JavaScript的过程来说,真心觉得不了解这些 ...

  10. Thread.sleep(0)的意义& 多线程详解

    我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题: 假设现在是 2008-4-7 12:00:00.000,如果我调用 ...