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. 我们用一根树枝两端连接的结点的编号来 ...
随机推荐
- Sicily 1156. Binary tree
题目地址:1156. Binary tree 思路: 如何愉快地前序输出呢,要在存储数据的时候根据位置来存放数据! 一开始被自己蠢哭,一直以为第一个输入的就是根结点(例子的祸害呀啊啊啊!!!!),结果 ...
- Selinux 禁用
Selinux是对于强制访问控制的实现,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件. 对于新手来说会影响我们的操作.一般情况下是不需要的,所以禁用他,如果需要的情况下,我们可以 ...
- 你不了解PHP的10件事情
看到有人翻译的<10 things you (probably) didn’t know about PHP>,发现在此次之前2.8两条并不知道,1.3虽然熟知但是去没有实际应用. 由于阅 ...
- Python新手学习基础之运算符——算术运算符
算术运算符 之前文章在介绍变量类型的时候,其实已经用过了很多算术符,比如+.-.*././/.** 等,除此之外,还有一个符号是之前内容没提到的,就是 % ,用来返回除法余数的运算符号. 假设有变量x ...
- Java学习笔记--String StringBuffer StringBuilder
String StringBuffer StringBuilder String http://docs.oracle.com/javase/7/docs/api/ 中文: http://www.cn ...
- 进程外组件通信之免注册com通信【原创】
最近在搞进程外组件通信的东西,写了个demo,免注册的,一直没调通,其实就是两个问题卡了好几天,也没找到有用的资料,试了好几天终于才解决,现简单记录下来,免得大家跟我走一样的弯路.下面com端程序名称 ...
- Netbeans 注释模板配置
工具->模板->展开Java 选中Java类->在编辑器中打开 修改如下: <#if package?? && package != ""& ...
- IIC总线协议---以存储芯片at24c64为例
IIC总线协议 前言:年前给老师做个红外抄表系统,,现在对当中用到的一些模块总结一下. 1.只有在总线空闲时才允许启动数据传送. 2.在数据传送过程中,当时钟线为高电平时,数据线必须保持稳定状态,不允 ...
- 利用好CSS,实现Qt控件美化
一.CSS概念 级联样式表 (CSS) 包含应用于网页中的元素的样式规则.CSS 样式定义元素的显示方式以及元素在页中的放置位置.可以创建一个通用规则,只要 Web 浏览器遇到一个元素实例,或遇到一个 ...
- Delphi TcxTreeListColumn 的 ImageComboBox 用法
1.设置图片如下: 设置默认值: if Trim(FQuery.fieldByName('cPersonCategory').AsString) = '' then begin CNode.Value ...