zoukankan      html  css  js  c++  java
  • HDU 1054 Strategic Game(树形dp)

    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
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int maxn=1510;
     6 bool father[maxn];//父节点,判断一个子节点有没有父节点
     7 int brother[maxn];//兄弟节点,存入一个父节点下的多个节点
     8 int son[maxn];//存入子节点
     9 int dp[maxn][2];//dp,在这个节点选择放还是不放
    10 int n,m,res,root; 
    11 
    12 void tree(int r){
    13     dp[r][0]=0;//该节点不放哨兵 
    14     dp[r][1]=1;//该节点放哨兵
    15     int x=son[r];//x为该节点最后标记的子节点
    16     while(x!=-1){ //只要还有子节点,继续 
    17         tree(x);//纵向遍历该节点
    18         dp[r][1]+=min(dp[x][0],dp[x][1]);//父节点放了,子节点可以选择放与不放
    19         dp[r][0]+=dp[x][1];//父节点不放,子节点要放
    20         x=brother[x];//横向遍历上一个兄弟节点 
    21     } 
    22 }
    23 
    24 int main(){
    25     while(scanf("%d",&n)!=EOF){
    26         int p,q;
    27         memset(son,-1,sizeof(son));
    28         memset(father,0,sizeof(father));
    29         for(int i=1;i<=n;i++){
    30             scanf("%d:(%d)",&p,&m);
    31             for(int i=1;i<=m;i++){
    32                 scanf("%d",&q);
    33                 brother[q]=son[p];//同一个父节点的上一个兄弟 
    34                 son[p]=q;//p的子节点是q,同时,如果m>1,输入的下一个q2的brother[q2]=q,实现之间的连接 
    35                 father[q]=1;//标记q有父节点 
    36             }
    37         }
    38         for(int i=0;i<n;i++){
    39             if(!father[i]){//如果i没有父节点,那i就是根节点 
    40                 root=i;//标记i为根节点 
    41                 break;
    42             }
    43         }
    44         tree(root);
    45         res=min(dp[root][1],dp[root][0]);
    46         printf("%d
    ",res); 
    47     }
    48     return 0;
    49 }
     
  • 相关阅读:
    公有云、私有云与混合云到底有什么区别?
    分享两个“整人”的脚本语言代码
    最强搜书工具集合
    关于云服务器恢复快照时后服务器变得卡顿等问题的解决方法
    Centos8.0编译安装稳定最新版的nginx
    彻底禁止Win10自动更新工具Windows Update Blocker v1.5 汉化版
    阿里云轻量应用服务器回滚快照后造成卡顿问题的解决方法
    IaaS、PaaS和SaaS的区别
    Everything文件搜索工具
    Windows10自带软件一款性能监控工具
  • 原文地址:https://www.cnblogs.com/cake-lover-77/p/10294099.html
Copyright © 2011-2022 走看看