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. Unix/Linux环境C编程入门教程(39) shell命令之系统管理

    df命令 用于检测文件系统的磁盘空间占用和空余情况,可以显示所有文件系统对节点和磁盘块的使用情况.命令的使用格式如下: df  [选项] 常用参数及含义如下表所示. df -a:显示所有文件系统的磁盘 ...

  2. 用VS2013+VELT进行Linux开发

    快乐虾 http://blog.csdn.net/lights_joy/(QQ群:Visual EmbedLinux Tools 375515651) 欢迎转载,但请保留作者信息 1.1.1  什么是 ...

  3. linux下tcpdump命令详解

    简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的 ...

  4. 认识什么是SEO

    何为SEO? SEO是由英 文Search Engine Optimization缩写而来, 中文意译为“搜索引擎优化”,是指在了解搜索引擎自然排名机制的基础上,对网站进行内部及外部的调整优化,改进网 ...

  5. Spoj1771-Yet Another N-Queen Problem(精确覆盖)

    Description After solving Solution to the n Queens Puzzle by constructing, LoadingTime wants to solv ...

  6. Android学习笔记__3__Android应用程序组成

    Android开发必须要了解构造块,Android应用程序是由里有六个重要组成部分组成的,这六种构造块如下:  ◆Activity ◆Intent Receiver ◆Service ◆Content ...

  7. UDP通讯协议

    常见的通讯协议有udp和tcp. 先来简单了解一下这两个协议各自的特点: UDP: --将数据及源.目的封装在数据包中,不需要建立连接: --每个数据包的大小限制在64k以内: --因无连接,是不可靠 ...

  8. Biorhythms(中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127339   Accepted: 40342 Des ...

  9. 用window.print()打印指定div里面的内容

    用window.print()打印指定div里面的内容 今天客户让添加个打印证照功能,直接用window.print()打印的是整个页面,而用以下方法就可以只打印证明了 <!--window.p ...

  10. Android——编译odex保护

    编译过android源代码的可能试验过改动编译类型.android的初始化编译配置可參考Android--编译系统初始化设置 一.TARGET_BUILD_VARIANT=user 当选择的编译类型为 ...