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 ...
随机推荐
- Oracle多行记录合并处理
1:效果如下图所示: 表T1: CREATE TABLE T1 ( WEEKWORKID VARCHAR2(20) , DD VARCHAR2(20) ) 表T2 CREATE TABLE T2 ( ...
- Netbeans7.4+Weblogic11g+Spring3.2.4操作JdbcTemplate
第一步:在Weblgic11g管理界面:http://localhost:7001/console 中建立数据源,我建立的jndi名称为:sjsstjndi 第二步:需要导入spring的以下包:使用 ...
- LeetCode_Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 浅谈程序员创业(要有一个自己的网站,最好的方式还是自己定位一个产品,用心把这个产品做好。或者满足不同需求的用户,要有特色)good
浅谈程序员创业 ——作者:邓学彬.Jiesoft 1.什么是创业? 关于“创业”二字有必要重新学习一下,找了两个相对权威定义: 创业就是创业者对自己拥有的资源或通过努力能够拥有的资源进行优化整合,从而 ...
- 单线多拨,傻瓜式openwrt单线多拨叠加速率教程
http://bbs.pceva.com.cn/thread-98362-1-1.html
- SQL省市区三级表结构
-- 表的结构 areaDROP TABLE area;CREATE TABLE area ( id int NOT NULL , areaID int NOT NULL, area va ...
- 快速批量导入庞大数据到SQL SERVER数据库(ADO.NET)
原文地址:http://www.cnblogs.com/chenxizhang/archive/2008/11/11/1331060.html 如果你需要在程序中批量插入成千上万行的数据,你会怎么编写 ...
- [转]ReactJS入门教程
Refference From:http://www.cocoachina.com/webapp/20150721/12692.html 现在最热门的前端框架有AngularJS.React.Boot ...
- sem_timedwait的用法
#include <semaphore.h> int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout); Link ...
- 打印log 保存log
using UnityEngine; using System.Collections; using System.IO; using System; using System.Text; names ...