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 ...
随机推荐
- PostgreSQL的时间函数使用整理
PG的时间函数使用整理如下 1.获取系统时间函数 select now(); --2012-05-12 18:51:59.562+08 select current_timestamp; --2012 ...
- Cmake中的find_package功能
find_package其实在windows下扮演的角色并不是很重要.在Unix下就非常重要了,find_package可以根据cmake内置的.cmake的脚本去找相应的库的模块,当然,内建了很多库 ...
- wapPush
短信推送:wapPsuh简介
- 【转】 Ubuntu下配置USB转串口及串口工具配置--不错
原文网址:http://blog.csdn.net/dreambegin/article/details/6985028 注意:默认情况下ubuntu已经安装了USB转串口驱动(pl2303).我的是 ...
- Gray Code 解答
Question The gray code is a binary numeral system where two successive values differ in only one bit ...
- 【HDU1754】I Hate It(线段树)
update:单点替换 query:区间最值 #include <iostream> #include <cstring> #include <cstdlib> # ...
- linux group
groups 查看当前登录用户的组内成员 groups gliethttp 查看gliethttp用户所在的组,以及组内成员 whoami 查看当前登录用户名 /etc/group文件包含所有组 ...
- PHP 字符串替换 substr_replace 与 str_replace 函数
PHP 字符串替换 用于从字符串中替换指定字符串. 相关函数如下: substr_replace():把字符串的一部分替换为另一个字符串 str_replace():使用一个字符串替换字符串中的另一些 ...
- DBA 经典面试题(3)
这里的回答并不是十分全面,这些问题可以通过多个角度来进行解释,也许你不必在面试过程中给出完全详尽的答案,只需要通过你的解答使面试考官了解你对ORACLE概念的熟悉程度. 1.解释冷备份和热备份的不 ...
- Linux权限管理(笔记)
权限管理:r: w:x: 三类用户:u: 属主g: 属组o: 其它用户 chown: 改变文件属主(只有管理员可以使用此命令)# chown USERNAME file,... -R: 修改目录 ...