poj 1129 Channel Allocation
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的更多相关文章
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- POJ 1129 Channel Allocation(DFS)
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13173 Accepted: 67 ...
- POJ 1129 Channel Allocation DFS 回溯
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15546 Accepted: 78 ...
- poj 1129 Channel Allocation ( dfs )
题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...
- POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...
- 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 ...
- PKU 1129 Channel Allocation(染色问题||搜索+剪枝)
题目大意建模: 一个有N个节点的无向图,要求对每个节点进行染色,使得相邻两个节点颜色都不同,问最少需要多少种颜色? 那么题目就变成了一个经典的图的染色问题 例如:N=7 A:BCDEFG B:ACDE ...
- Channel Allocation (poj 1129 dfs)
Language: Default Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12 ...
- POJ 1129:Channel Allocation 四色定理+暴力搜索
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13357 Accepted: 68 ...
随机推荐
- php生成json和js解析json
php生成json $value=array("name"=>"dfas");echo json_encode($value); js解析json var ...
- eclipse安装github插件egit
http://jingyan.baidu.com/article/4853e1e529483c1909f726c3.html help->InstallNew Software,这是安装插件推荐 ...
- 用VS2013+VELT进行Linux开发
快乐虾 http://blog.csdn.net/lights_joy/(QQ群:Visual EmbedLinux Tools 375515651) 欢迎转载,但请保留作者信息 1.1.1 什么是 ...
- UESTC_方老师和农场 2015 UESTC Training for Graph Theory<Problem L>
L - 方老师和农场 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- Zigzag Iterator 解答
Question Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...
- Hdu2437-Jerboas(取余数判重搜索)
Jerboas are small desert-living animals, which resemble mice with a long tufted tail and very long h ...
- Ajax_post发送
$('#img_file_del_3').click(function() { var data={name:$('#img_file_del_3').attr('name')}; var url=' ...
- java框架BeanUtils及路径问题练习
内省----->一个变态的反射 BeanUtils主要解决 的问题: 把对象的属性数据封装 到对象中. 使从文件中读取的数据往对象中赋值更加简单: BeanUtils的好处: 1. ...
- 用window.print()打印指定div里面的内容
用window.print()打印指定div里面的内容 今天客户让添加个打印证照功能,直接用window.print()打印的是整个页面,而用以下方法就可以只打印证明了 <!--window.p ...
- Git 上传本地命令
1.首先建立一个文件夹用以测试 2.在test中写入一个main.c的文件 其内容如下: 3.然后就建立一个git仓库了 4.然后就是把内容加进去了,上传上去 5.然后我们看下git log的信息 6 ...