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. eclipse安装github插件egit

    http://jingyan.baidu.com/article/4853e1e529483c1909f726c3.html help->InstallNew Software,这是安装插件推荐 ...

  2. LeetCode_Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  3. MVC 自定义错误处理

    1. Application_Error namespace Libaray.Web{ public class MvcApplication : System.Web.HttpApplication ...

  4. jQuery.trim(str)

    描述: 去掉字符串起始和结尾的空格. jQuery 代码: $.trim(" hello, how are you? "); 结果: "hello, how are yo ...

  5. sql server数据建表

    use edudbgoif exists(select * from sysobjects where name='department')drop table departmentcreate ta ...

  6. WEB打印插件Lodop

    Lodop.C-Lodop使用说明及样例   Lodop(标音:劳道谱,俗称:露肚皮)是专业WEB控件,用它既可裁剪输出页面内容,又可用程序代码直接实现 复杂打印.控件功能强大,却简单易用,所有调用如 ...

  7. mysqldump命令详解(转载)

    1.简介 mysqldump为MySQL逻辑备份工具,产生一系列SQL语句,之后重新执行以产生备份的库.表及数据.也可产生CSV.XML等格式的数据.适用于各类引擎的表. 运行mysqldump需一定 ...

  8. 分割视图控制器(UISplitViewController) 改_masterColumnWidth 导致在 IOS 10中出现闪退

    默认UISplitViewController的Master和Detail的宽度是固定的,可以通过下面的方式来改变 [splitViewController setValue:[NSNumber nu ...

  9. New Year Transportation(水)

    New Year Transportation Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  10. Git 如何回到过去,然后 再 回到将来

    回到过去: git log 然后 git reset --hard commit ID (那段长长代码 40位) 再,回到将来git reflog 然后 git reset --hard 前面那个代码 ...