zoukankan      html  css  js  c++  java
  • 树状dp--B

    trategic game
    Time Limit:2000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
    Submit

    Status
    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

    <span style="color:#3366ff;">/*
    ***********************************************************************
               author    :     Grant Yuan
               time      :     2014.7.22
               algorithm :     树状dp
               explain   :    状态转移方程:
                                 f[x][1] =1 + Σ min(f[i][0],f[i][1]);// x上放置的士兵,于是它的儿子们可放可不放!
                                 f[x][0] = Σ f[i][1];//x上不放置的士兵,它的儿子们都必须放!
                                 (i是x的儿子!!)
                                 结果为min(f[root][0], f[root][1])
     ************************************************************************
     */
       
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<functional>
    #include<vector>
    #define MAX 1501
    
    using namespace std;
    
    vector<int> G[MAX];
    int dp[MAX][2],n;
    int a,b;
    int root;
    
    void dfs(int rt)
    {
       int len;
       len=G[rt].size();
       for(int i=0;i<len;i++)
       {
           dfs(G[rt][i]);
       }
    
       for(int i=0;i<len;i++)
       {
          dp[rt][1]+=min(dp[G[rt][i]][0],dp[G[rt][i]][1]);
          dp[rt][0]+=dp[G[rt][i]][1];
       }
       dp[rt][1]+=1;
    }
    
    int main()
    {
        while(~scanf("%d",&n)){
            memset(dp,0,sizeof(dp));
            memset(G,0,sizeof(G));
            for(int i=0;i<n;i++)
            { 
                scanf("%d:(%d)",&a,&b);
                if(i==0) root=a;
                for(int j=0;j<b;j++)
                {  int t;
                   scanf("%d",&t);
                    G[a].push_back(t);
                }
            }
            dfs(root);
            int ans;
            ans=min(dp[root][0],dp[root][1]);
            printf("%d
    ",ans);}
            return 0;
    }
    </span>


  • 相关阅读:
    安装SQL sever2008时显示重新启动计算机规则失败,应该怎么解决?
    C#获取当前日期时间(转)
    使用JQUERY实现页面局部刷新
    Metal渲染:实现画面比例功能
    Metal渲染:实现旋转/翻转功能
    依赖注入浅析
    iOS 消息推送实现 APNS
    使用#pragma阻止一些warnings
    github 多帐户使用
    Swift 实现Bitmask Option(Enum)
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254488.html
Copyright © 2011-2022 走看看