Strategic game
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 7490   Accepted: 3483

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

Source

一道简单的树形DP,状态转移方程:和上一次做的题目基本一样
  dp[node][0]+=dp[tree[node][i]][1];

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

  1. poj 1463(树形dp)

    题目链接:http://poj.org/problem?id=1463 思路:简单树形dp,如果不选父亲节点,则他的所有的儿子节点都必须选,如果选择了父亲节点,则儿子节点可选,可不选,取较小者. #i ...

  2. (探讨贴)POJ 1463 树形DP解法的不正确性

    POJ1463是一个典型的树状DP题. 通常解法如下代码所示: using namespace std; ; ]; int pre[maxn]; int childcnt[maxn]; int n; ...

  3. poj 1463树形dp 树的最小覆盖

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  4. 树形dp compare E - Cell Phone Network POJ - 3659 B - Strategic game POJ - 1463

    B - Strategic game POJ - 1463   题目大意:给你一棵树,让你放最少的东西来覆盖所有的边   这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处 ...

  5. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  6. poj 2486( 树形dp)

    题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...

  7. poj 3140(树形dp)

    题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...

  8. HDU 1054 Strategic Game(最小点覆盖+树形dp)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...

  9. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

随机推荐

  1. 逃离迷宫(HDU 1728 BFS)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. DataTables获取表单输入框数据

    $(document).ready(function() { var table = $('#example').DataTable(); $('button').click(function() { ...

  3. 关于cookie, iphone及chrome的异同

    http://www.blogjava.net/jjshcc/archive/2010/06/13/323517.html http://stackoverflow.com/questions/295 ...

  4. Could not open a connection to your authentication agent

    执行ssh-add ~/.ssh/rsa  就会遇到上述错误了 解决方案: 先执行  eval `ssh-agent`  (是-键上的那个`) 再执行 ssh-add ~/.ssh/rsa成功 ssh ...

  5. JS获取按下的键盘字符

    <html> <head> KeyPress Test!<hr> <script language="javascript"> fu ...

  6. Hdu2860-Regroup(种类并查集)

    Problem Description When ALPC42 got to a panzer brigade, He was asked to build software to help them ...

  7. shell编程while

    脚本编程:    顺序结构    选择结构        if        case    循环结构        for        while        until        whil ...

  8. DirectX 开发环境配置

      想使用DirectX开发游戏,前提是要搭建DirectX的开发环境啦. 下面我们就一起学习下DirectX开发环境搭建 1. 首先是DirectX SDK安装, 下载地址是: http://www ...

  9. python学习之路-11 多线程、多进程、协程

    python内置队列模块 queue queue的四种队列 q = queue.Queue() # 先进先出队列 q = queue.LifoQueue() # 后进先出队列 q = queue.Pr ...

  10. HDU 4362 Dragon Ball 线段树

    #include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...