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>


  • 相关阅读:
    Scrapy框架基础使用
    python爬虫练习--爬取今日头条街拍图片
    python爬虫练习--爬取所有微博
    python爬虫练习--爬取猫眼热映口碑榜
    python练习---校园管理系统
    python爬虫练习--爬取猫眼top100电影信息
    基础篇-Python的urllib库
    1--爬虫环境安装篇
    Confluence 如何查看页面 ID
    导入 Gradle 项目到 IntelliJ IDEA
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254488.html
Copyright © 2011-2022 走看看