Strategic game(POJ 1463 树形DP)
| Time Limit: 2000MS | Memory Limit: 10000K | |
| Total Submissions: 7490 | Accepted: 3483 |
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
Source
dp[node][1]+=min(dp[tree[node][i]][0],dp[tree[node][i]][1]); 然后+1
一开始写成 dp[node][1]+=dp[tree[node][i]][0];
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define Max 1506
int dp[Max][],fa[Max],tree[Max][];
int n;
int num[Max];
void tree_dp(int node)
{
int i;
for(i=;i<num[node];i++)
{
tree_dp(tree[node][i]);
dp[node][]+=dp[tree[node][i]][];
dp[node][]+=min(dp[tree[node][i]][],dp[tree[node][i]][]);
}
dp[node][]++;
return;
}
int main()
{
int i,j;
int a,b;
int k,root,f;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
memset(dp,,sizeof(dp));
memset(tree,,sizeof(tree));
for(i=;i<Max;i++)
fa[i]=-;
for(i=;i<n;i++)
{
scanf("%d%*c%*c%d%*c",&f,&k);
num[f]=k;
for(j=;j<k;j++)
{
scanf("%d",&tree[f][j]);
fa[tree[f][j]]=f;
}
}
root=;
while(fa[root]!=-)
root=fa[root];
tree_dp(root);
printf("%d\n",min(dp[root][],dp[root][]));
}
return ;
}
Strategic game(POJ 1463 树形DP)的更多相关文章
- poj 1463(树形dp)
题目链接:http://poj.org/problem?id=1463 思路:简单树形dp,如果不选父亲节点,则他的所有的儿子节点都必须选,如果选择了父亲节点,则儿子节点可选,可不选,取较小者. #i ...
- (探讨贴)POJ 1463 树形DP解法的不正确性
POJ1463是一个典型的树状DP题. 通常解法如下代码所示: using namespace std; ; ]; int pre[maxn]; int childcnt[maxn]; int n; ...
- poj 1463树形dp 树的最小覆盖
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...
- 树形dp compare E - Cell Phone Network POJ - 3659 B - Strategic game POJ - 1463
B - Strategic game POJ - 1463 题目大意:给你一棵树,让你放最少的东西来覆盖所有的边 这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处 ...
- Fire (poj 2152 树形dp)
Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...
- poj 2486( 树形dp)
题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...
- poj 3140(树形dp)
题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...
- HDU 1054 Strategic Game(最小点覆盖+树形dp)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...
- POJ 2342 树形DP入门题
有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...
随机推荐
- 逃离迷宫(HDU 1728 BFS)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- DataTables获取表单输入框数据
$(document).ready(function() { var table = $('#example').DataTable(); $('button').click(function() { ...
- 关于cookie, iphone及chrome的异同
http://www.blogjava.net/jjshcc/archive/2010/06/13/323517.html http://stackoverflow.com/questions/295 ...
- Could not open a connection to your authentication agent
执行ssh-add ~/.ssh/rsa 就会遇到上述错误了 解决方案: 先执行 eval `ssh-agent` (是-键上的那个`) 再执行 ssh-add ~/.ssh/rsa成功 ssh ...
- JS获取按下的键盘字符
<html> <head> KeyPress Test!<hr> <script language="javascript"> fu ...
- Hdu2860-Regroup(种类并查集)
Problem Description When ALPC42 got to a panzer brigade, He was asked to build software to help them ...
- shell编程while
脚本编程: 顺序结构 选择结构 if case 循环结构 for while until whil ...
- DirectX 开发环境配置
想使用DirectX开发游戏,前提是要搭建DirectX的开发环境啦. 下面我们就一起学习下DirectX开发环境搭建 1. 首先是DirectX SDK安装, 下载地址是: http://www ...
- python学习之路-11 多线程、多进程、协程
python内置队列模块 queue queue的四种队列 q = queue.Queue() # 先进先出队列 q = queue.LifoQueue() # 后进先出队列 q = queue.Pr ...
- HDU 4362 Dragon Ball 线段树
#include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...