Strategic Game

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9631    Accepted Submission(s): 4492

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

The input file 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_identifier
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.

For example for the tree:

the solution is one soldier ( at the node 1).

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 table:

 
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
 
 
题意:求最少多少个点可以覆盖这一整颗树,每个点可以覆盖他的父亲节点和子节点
题解:如果父亲结点没放哨兵,那么子结点肯定要放置哨兵,如果父亲放置了哨兵,那么子结点可以考虑放或者不放。 并且存在兄弟节点的情况,
   每次处理完当前节点后就去处理兄弟节点,如此我们就可以用一个dp[maxn][2]的数组来存储当前的状态
   dp[node][1]+=min(dp[f][0],dp[f][1]);
代码如下:
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. using namespace std;
  6. const int maxn = ;
  7. int f[maxn];
  8. int son[maxn];
  9. int bro[maxn];
  10. int dp[maxn][];
  11. int vis[maxn];
  12. void dfs(int node){
  13. dp[node][]=;
  14. dp[node][]=;
  15. int f=son[node];
  16. while(f!=-){
  17. dfs(f);
  18. dp[node][]+=min(dp[f][],dp[f][]);
  19. dp[node][]+=dp[f][];
  20. f=bro[f];
  21. }
  22. }
  23. int main(){
  24. int n;
  25. while(cin>>n){
  26. int root=;
  27. memset(vis,,sizeof(vis));
  28. memset(son,-,sizeof(son));
  29. memset(f,,sizeof(f));
  30. for(int i=;i<n;i++) {
  31. int num,cnt;
  32. scanf("%d:(%d)",&num,&cnt);
  33. for(int j=;j<cnt;j++)
  34. {
  35. int num1;
  36. cin>>num1;
  37. bro[num1] = son[num]; //同一个父节点的上一个兄弟
  38. son[num] = num1; //num的儿子是num1
  39. f[num1] = ; //num1是有父亲的,为下面找根结点做准备
  40. }
  41. }
  42. for(int i=;i<n;i++)
  43. {
  44. if(!f[i]) //如果没有父亲就可以做为根结点
  45. {
  46. root = i;
  47. break;
  48. }
  49. }
  50. dfs(root);
  51. cout<<min(dp[root][],dp[root][])<<endl;
  52. }
  53. }

HDU 1054树形DP入门的更多相关文章

  1. HDU 1561 树形DP入门

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. HDU 1520 树形DP入门

    HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...

  3. HDU - 1520 树形DP入门题

    写了两种DP,第一种是按照自己习惯来xjb敲的,第二种参考别人 熟悉一下树形DP的套路 dp[i][]是维护i及以下的关系最优值的,所以我觉得两次DP记忆搜索之间不清-1应该是正确的(也就做了一次加法 ...

  4. HDU 1561 树形DP(入门)

    题目链接:  HDU 1561 The more, The Better #include <iostream> #include <cstdio> #include < ...

  5. (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520

    题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...

  6. 树形dp 入门

    今天学了树形dp,发现树形dp就是入门难一些,于是好心的我便立志要发一篇树形dp入门的博客了. 树形dp的概念什么的,相信大家都已经明白,这里就不再多说.直接上例题. 一.常规树形DP P1352 没 ...

  7. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  8. POJ 2342 树形DP入门题

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

  9. 树形DP入门详解+题目推荐

    树形DP.这是个什么东西?为什么叫这个名字?跟其他DP有什么区别? 相信很多初学者在刚刚接触一种新思想的时候都会有这种问题. 没错,树形DP准确的说是一种DP的思想,将DP建立在树状结构的基础上. 既 ...

随机推荐

  1. php数据加密及数据存储和传输

    一.前言 个人认为,PHP是世界上为数不多,最人性化的语言. 虽然是二次开发.弱类型语言,由C/C++编写的PHP引擎去解析.但是,其代码优雅性和其运行速度不亚于,其他编译语言. 二.PHP数据加密 ...

  2. python3 练习题100例 (二十)

    #!/usr/bin/env python3# -*- coding: utf-8 -*-"""练习二十:判断一个年份是否是闰年公历闰年计算方法:1.普通年能被4整除且不 ...

  3. 寻找物体的凸包 opencv

    凸包的含义: 在二维平面上给定点集,凸包就是将最外层的点连接起来构成的凸多边形.并且这个凸多边形能包含点集中所有的点.OPENCV中: convexHull函数用于寻找图像点集中的凸包.它有六个输入参 ...

  4. ### Cause: java.lang.reflect.UndeclaredThrowableException

    ### Cause: java.lang.reflect.UndeclaredThrowableException Caused by: org.apache.ibatis.exceptions.Pe ...

  5. CONDENSE命令により、文字列から冗長スペースが削除

    CONDENSE 命令により.文字列から冗長スペースが削除されます. CONDENSE c [NO-GAPS]. この命令により.項目 c に先行空白が含まれる場合は削除され.その他の空白列がある場合 ...

  6. Hibernate SQL方言

    RDBMS 方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB2 OS3 ...

  7. hash算法和常见的hash函数 [转]

       Hash,就是把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就是散列值. 这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能 会散列成相同的输出,而不 ...

  8. 《Cracking the Coding Interview》——第18章:难题——题目10

    2014-04-29 04:22 题目:给定一堆长度都相等的单词,和起点.终点两个单词,请从这堆单词中寻找一条变换路径,把起点词变成终点词,要求每次变换只能改一个字母. 解法:Leetcode中有Wo ...

  9. Restful API实战

    简介:随着移动互联网的发展,客户端层出不穷,app,web,微信端等等,而后端业务逻辑基于是一致的,如何做到业务逻辑“一次编写,随时接入”?答案是通过远程调用API,而目前比较火的方案是“Restfu ...

  10. Spring Boot多数据源配置(二)MongoDB

    在Spring Boot多数据源配置(一)durid.mysql.jpa 整合中已经讲过了Spring Boot如何配置mysql多数据源.本篇文章讲一下Spring Boot如何配置mongoDB多 ...