B - 树形dp

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?

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 input contains several data sets in text format. Each data set represents a tree with the following description:

  • 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

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:

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的更多相关文章

  1. 树状DP (poj 2342)

    题目:Anniversary party 题意:给出N各节点的快乐指数,以及父子关系,求最大快乐指数和(没人职员愿意跟直接上司一起玩): 思路:从底向上的树状DP: 第一种情况:第i个员工不参与,F[ ...

  2. poj3659树状DP

    Cell Phone Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6273   Accepted: 225 ...

  3. hdu 1561 The more, The Better_树状dp

    题目链接 题意:给你一棵树,各个节点都有价值(除根节点),从根节点出发,选择m个节点,问最多的价值是多小. 思路:很明显是树状dp,遍历树时背包最优价值,dp[i][k]=max{dp[i][r]+d ...

  4. poj 2342 Anniversary party_经典树状dp

    题意:Ural大学有n个职员,1~N编号,他们有从属关系,就是说他们关系就像一棵树,父节点就是子节点的直接上司,每个职员有一个快乐指数,现在要开会,职员和职员的直接上司不能同时开会,问怎才能使开会的快 ...

  5. 树状DP HDU1520 Anniversary party

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:职员之间有上下级关系,每个职员有自己的happy值,越高在派对上就越能炒热气氛.但是必须是 ...

  6. [Codeforces743D][luogu CF743D]Chloe and pleasant prizes[树状DP入门][毒瘤数据]

    这个题的数据真的很毒瘤,身为一个交了8遍的蒟蒻的呐喊(嘤嘤嘤) 个人认为作为一个树状DP的入门题十分合适,同时建议做完这个题之后再去做一下这个题 选课 同时在这里挂一个选取节点型树形DP的状态转移方程 ...

  7. 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 ...

  8. poj2486--Apple Tree(树状dp)

    Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7789   Accepted: 2606 Descri ...

  9. 洛谷P2015 二叉苹果树(树状dp)

    题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...

随机推荐

  1. Struts2 过滤器与拦截器

    学习Struts2时,发现有过滤器和拦截器,他们貌似都是一样的功能,但是为什么会有2个不同的名称呢?肯定是有区别的,所以打算自己整理一下. 过滤器,是在java web中,你传入的request,re ...

  2. nodejs安装过程及视频地址

    说实话在安装的过程中遇到了很多问题,包括npm install connect出错,主要是我之前安装了0.10版本之后安装4.X版本造成的,后面卸载没有卸载完全造成的,后面也就好了,网上说了很多重新设 ...

  3. sql解释执行顺序

    一.查询的逻辑执行顺序 (1) FROM left_table (3) join_type JOIN right_table (2) ON join_condition (4) WHERE where ...

  4. php 练习一 5月5日

    练习题一:通过登录者找到他的好友并显示在页面上 <title>无标题文档</title> <style type="text/css"> * { ...

  5. sql delete output

    select * into #student1 from student select * from #student1 create table #temp2( id int not null,na ...

  6. 对JAVA的static深刻理解(结合C语言的思考)

    public class statictest { String X = "我是非静态变量"; static int butterfly =0; static String sta ...

  7. javascript 中ASCII字符值转换

    char-->ascii    var a = "123";    a.charAt(1).charCodeAt();ascii-->char   String.fro ...

  8. Android 编程:calledfromWrongThreadException 的原因

    子线程更新UI会发生Android.view.ViewRoot$CalledFromWrongThreadException异常的解决方法 子线程更新UI 显然假如你的程序需要执行耗时的操作的话,假如 ...

  9. 【转】Service Intent must be explicit的解决方法

    原文网址:http://blog.csdn.net/shenzhonglaoxu/article/details/42675287 今天在学习android的Service组件的时候,在Android ...

  10. VC++如何在程序中用代码注册和卸载ocx控件(代码)

    方法一:在dos或Windows命令行下运行:regsvr32 ocxname.ocx 注册 示例:regsvr32 netshare.ocx     //注册netshare.ocx控件regsvr ...