zoukankan      html  css  js  c++  java
  • HDU1520(树形dp)

    H - Anniversary party

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
    Submit Status

    Description

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests' conviviality ratings.

    Input

    Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go N – 1 lines that describe a supervisor relation tree. Each line of the tree specification has the form: 
    L K 
    It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line 
    0 0 

    Output

    Output should contain the maximal sum of guests' ratings.

    Sample Input

    7
    1
    1
    1
    1
    1
    1
    1
    1 3
    2 3
    6 4
    7 4
    4 5
    3 5
    0 0
    

    Sample Output

    5

    树形dp
     1 //2016.8.7
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<vector>
     5 #include<algorithm>
     6 #include<cstring>
     7 
     8 using namespace std;
     9 
    10 const int maxn = 6005;
    11 int dp[maxn][2];//dp[i][j]表示i节点的子树的最大happy值,j==1时i不去,j==0时i去
    12 int happy[maxn], fa[maxn];//fa[i]表示i节点的父亲
    13 vector<int> v[maxn];//用来保存v[i]的儿子
    14 
    15 int dfs(int r)
    16 {
    17     int len = v[r].size();
    18     dp[r][1] = happy[r];
    19     for(int i = 0; i < len; i++)
    20       dfs(v[r][i]);//使得从叶子节点先计算
    21     for(int i = 0; i < len; i++)
    22     {
    23         dp[r][0] += max(dp[v[r][i]][0], dp[v[r][i]][1]);//r不去
    24         dp[r][1] += dp[v[r][i]][0];//r去
    25     }
    26 }
    27 
    28 int main()
    29 {
    30     int n, l, k;
    31     while(cin>>n)
    32     {
    33         memset(dp, 0, sizeof(dp));
    34         for(int i = 1; i <= n; i++)
    35         {
    36             scanf("%d", &happy[i]);
    37             fa[i] = -1;
    38             v[i].clear();//初始化
    39         }
    40         while(cin>>l>>k&&l&&k)
    41         {
    42             fa[l] = k;
    43             v[k].push_back(l);
    44         }
    45         int root = 1;
    46         while(fa[root] != -1)root = fa[root];//节点的父亲为-1时,此节点为根
    47         dfs(root);
    48         cout<<max(dp[root][0],dp[root][1])<<endl;
    49     }
    50 
    51     return 0;
    52 }
  • 相关阅读:
    Servlet 易错点和注意点
    Spring 完成自动注入(autowire)
    Java 定时调度Timer&Quartz
    常用Linux命令杂记
    Spring 使用AOP——基于注解配置
    Spring 使用AOP——xml配置
    Spring 使用纯注解方式完成IoC
    Spring 简单使用IoC与DI——XML配置
    让多个HTML页面 使用 同一段HTML代码
    Feture、ListenableFuture、ComplatableFuture
  • 原文地址:https://www.cnblogs.com/Penn000/p/5747295.html
Copyright © 2011-2022 走看看