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. 使用ffmpeg录音

    官方教程:http://ffmpeg.org/ffmpeg.html 录音方法: 开始找到了这个方法,但是不行呀,好像是没有这个oss吧. oss 是linux 下的声音相关的东西,与alsa 一样, ...

  2. [Android分享] 如何解决Android 5.0中出现的警告:Service Intent must be explicit

    Android 5.0程序运行报Service Intent must be explicit错误,原因是5.0的service必须显式调用 改成 Intent intent = new Intent ...

  3. pyqt5表格qtablewidget

    表格用到控件QTableWidget,还有一个类似的叫QTableView的. 设置单元格里的内容用方法:setItem(0,0,str) 第一个0表示第一行, 第二个0表示第一列, 第三个参数是写入 ...

  4. Android ndk第一步,构建jni headers

    转载请注明出处:http://www.cnblogs.com/fpzeng/p/4281801.html 源码请见 https://github.com/fpzeng/HelloJNI PC系统: u ...

  5. 测试一下PHP官方的新一代PHP加速插件ZendOpcache的性能及配置

    过程不表,都比较顺利 参考如下URL: http://www.lvtao.net/server/ZendOpcache.html 大家知道目前PHP的缓存插件一般有三个:APC.eAccelerato ...

  6. WCF基于Cookie回传的系列(概述)

    1  WCF的基本知识(不作细述,园子里有很多的经典的文章系列) 2 WCF的执行过程 3 让服务通信像浏览器发送请求应答一样回传Cookie,并实现Cookie在不同的服务间共享 4  基于共享后的 ...

  7. VC的话有必要认真听,但却不用急着照办

    本文来自著名风险投资人 Fred Wilson 的博客 AVC,他在 2016 年 8 月 23 日的这篇文章<Understanding VCs>里用简单的语言揭秘了 VC(风险投资人) ...

  8. HTML5的local storage存储的数据到底存到哪去了

    原文地址:http://zhidao.baidu.com/link?url=m6p5MLv0R46lDCd_Vnrry4XOMbdCwgV5fzs3tj5Jeyht1nPkAZ9OrO23njYBY1 ...

  9. URAL 1036

    题目大意:求前N位与后N位各个位和相等且总和等于S的2N位数的个数. KB     64bit IO Format:%I64d & %I64u 数据规模:1<=N<=50,0< ...

  10. 第28讲 UI组件之 ListView和ArrayAdapter

    第28讲 UI组件之 ListView和ArrayAdapter 1. Adapter 适配器 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的 ...