hdu1054 树状dp
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Description
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
For example for the tree:         
the solution is one soldier ( at the node 1).        
Input
- the number of nodes
 - the description of each node in the following format 
node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifiernumber_of_roads
or
node_identifier:(0) 
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.
Output
Sample Input
4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)
Sample Output
1
2
题目大意:给你一个树状结构,每一个节点都可以放置士兵,可以看守住与 该节点相邻的所有边,问你最少需要多少个节点。
思路分析:树的最小点覆盖问题,首先确定状态f[i[0]表示在根节点i上不放置士兵看守住这棵子树需要的最少士兵,f[i][0]
则代表根结点i上不放置士兵看守住所需要的最少士兵,状态转移方程f[i][1]+=min(f[j][1],f[j][0]),f[i][0]+=f[j][1]
初始化f[i][[1]=1,f[i][0]=0; 代码:
/*树的最小点覆盖
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1500+10;
int fat[maxn];
int dp[maxn][2];
bool vis[maxn];//标记
int n;
void dfs(int x)
{
//cout<<x<<endl;
vis[x]=true;
dp[x][1]=1;
dp[x][0]=0;
for(int i=0;i<n;i++)
{
if(!vis[i]&&fat[i]==x)
{
dfs(i);
dp[x][1]+=min(dp[i][0],dp[i][1]);//子节点可放可不放
dp[x][0]+=dp[i][1];
}
}
}
int main()
{
int r,num;
int a;
int i;
while(scanf("%d",&n)!=EOF)
{
// cout<<n<<endl;
memset(fat,0,sizeof(fat));
memset(vis,false,sizeof(vis));
for(int k=0;k<n;k++)
{
scanf("%d:(%d)",&r,&num);
//cout<<r<<" "<<num<<endl;
for(int i=1;i<=num;i++)
{
scanf("%d",&a);
fat[a]=r;
// cout<<a<<endl;
}
}
// for(int i=0;i<n;i++)
//cout<<fat[i]<<endl;
for(i=0;i<n;i++)
{
if(!fat[i])
{
//cout<<i<<endl;
dfs(i);
cout<<min(dp[i][0],dp[i][1])<<endl;
break;
}
} }
return 0;
}
思考了下我这样写是有弊端的,这是一个无向图,然而我建树的时候默认了先出的数为根,后出的为子节点,这样是不对的。。。然而数据水过,自己又用链式前向星
存图写了一些,效率大大提高
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=+;
struct node
{
int to;
int next;
};
node edge[*maxn];
int head[maxn];
int dp[maxn][];
bool vis[maxn];//把一个无向 图变成有向图
int tot;
int n;
void add(int x,int y)
{
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot++;
}
void init()
{
memset(head,-,sizeof(head));
memset(vis,false,sizeof(vis));
tot=;
int r,num;
int a;
for(int i=;i<n;i++)
{
scanf("%d:(%d)",&r,&num);
for(int i=;i<num;i++)
{
scanf("%d",&a);
add(a,r);
add(r,a);
}
}
}
void dfs(int nod)
{
vis[nod]=true;
dp[nod][]=,dp[nod][]=;
for(int i=head[nod];i!=-;i=edge[i].next)
{
int v=edge[i].to;//这条边指向的点
if(!vis[v])
{
dfs(v);
dp[nod][]+=min(dp[v][],dp[v][]);
dp[nod][]+=dp[v][];
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
dfs();
printf("%d\n",min(dp[][],dp[][]));
}
}
hdu1054 树状dp的更多相关文章
- 树状DP (poj 2342)
		
题目:Anniversary party 题意:给出N各节点的快乐指数,以及父子关系,求最大快乐指数和(没人职员愿意跟直接上司一起玩): 思路:从底向上的树状DP: 第一种情况:第i个员工不参与,F[ ...
 - poj3659树状DP
		
Cell Phone Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6273 Accepted: 225 ...
 - hdu 1561 The more, The Better_树状dp
		
题目链接 题意:给你一棵树,各个节点都有价值(除根节点),从根节点出发,选择m个节点,问最多的价值是多小. 思路:很明显是树状dp,遍历树时背包最优价值,dp[i][k]=max{dp[i][r]+d ...
 - poj 2342 Anniversary party_经典树状dp
		
题意:Ural大学有n个职员,1~N编号,他们有从属关系,就是说他们关系就像一棵树,父节点就是子节点的直接上司,每个职员有一个快乐指数,现在要开会,职员和职员的直接上司不能同时开会,问怎才能使开会的快 ...
 - 树状DP HDU1520 Anniversary party
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:职员之间有上下级关系,每个职员有自己的happy值,越高在派对上就越能炒热气氛.但是必须是 ...
 - [Codeforces743D][luogu CF743D]Chloe and pleasant prizes[树状DP入门][毒瘤数据]
		
这个题的数据真的很毒瘤,身为一个交了8遍的蒟蒻的呐喊(嘤嘤嘤) 个人认为作为一个树状DP的入门题十分合适,同时建议做完这个题之后再去做一下这个题 选课 同时在这里挂一个选取节点型树形DP的状态转移方程 ...
 - HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)
		
Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...
 - poj2486--Apple Tree(树状dp)
		
Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7789 Accepted: 2606 Descri ...
 - 洛谷P2015 二叉苹果树(树状dp)
		
题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...
 
随机推荐
- [每日一题] OCP1z0-047 :2013-07-25 权限――角色与对象权限
			
有疑问可以去itpub讨论:http://www.itpub.net/thread-1804842-1-1.html 按题意,操作如下: 1.创建一个角色r1 sys@OCM> create r ...
 - 简单实用 “易忘” 的SQL 语句语法,新老皆宜
			
--创建数据库 create database 数据库名 on primary ( name='数据库名_data', filename='数据库储存路径', size=数据库初始大小(MB), ...
 - 转载:对#!/bin/sh的认识
			
转载网址:http://blog.163.com/hashes@yeah/blog/static/16867631220101029847420/ 对#!/bin/sh的认识 第一次学shell编程, ...
 - phpquery笔记
			
下载phpquery包 require('phpQuery/phpQuery.php');//加载 for($i=1168;$i<=10000;$i++){ phpQuery::newDocum ...
 - jquery 点点滴滴小记
			
字符截取显示 var text = $(".content").text(); var textNum = text.length; var textInt = text.slic ...
 - 子类重载父类的方法“parent:方法名”
			
在PHP中不能定义重名的函数,也包括不能再同一个类中定义重名的方法,所以也就没有方法重载.单在子类中可以定义和父类重名的方法,因为父类的方法已经在子类中存在,这样在子类中就可以把从父类中继承过来的方法 ...
 - android DatePickerDialog配合edittext实现按日期查询
			
我们从网上一搜DatePickerDialog相关实现,大多都是默认的形式,也就是不带取消按钮.下边上我的代码:我将代码简单的封装到一个工具类里边 public static DatePickerDi ...
 - webpack,react,babel
			
window搭建webpack,react,babel傻瓜教程 首先现在的webpack教程已经很多了,写这篇的原因是因为自己在从小白开始的搭建过程中,并没有找到比较好的教程,花费了很多的时间,s ...
 - MongoDB资料--Java驱动, Hadoop驱动, Spark使用
			
MongoDB数据库备份: mongodump -h 192.168.1.160 -d MapLoc -o /usr/local/myjar/mongo/MapLoc/数据库还原:mongoresto ...
 - void (*fun)(void);
			
2440test程序中的Main.c中在结构体中有这么一句: void (*fun)(void); 后查阅资料得知这句代码的意思是: 定义一个函数指针. 比如:定义一个指向函数的指针,该函数有一个整形 ...