zoukankan      html  css  js  c++  java
  • HDU-1520 Anniversary party(树形DP)

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

    题目大意就是一个Party,有n个人,每个人参加的满意度分别是A1,A2,A3...An,不能同时和直属上司参加。
    问你哪些人参加,满意度最大,求最大满意度值。

    uhmmm一看就是DP,而且题目还说明了最大BOSS只有一个,也就是有根节点且只有一个。
    所以状态转移方程就是:
    dp[root][0] += max(dp[son][1],dp[son][0]); //当前点不选,儿子可选可不选,最大值就是儿子的最大值
    dp[root][1] += dp[son][0]; //当前点选,最大值就是不选儿子的值

    然后用vector存每个结点的儿子,从根节点开始dfs就行了。

    AC代码

     1 #include <iostream>
     2 #include <string>
     3 #include <cstdio>
     4 #include <cstdlib>
     5 #include <sstream>
     6 #include <iomanip>
     7 #include <map>
     8 #include <stack>
     9 #include <queue>
    10 #include <vector>
    11 #include <set>
    12 #include <list>
    13 #include <cstring>
    14 #include <cctype>
    15 #include <algorithm>
    16 #include <iterator>
    17 #include <cmath>
    18 using namespace std;
    19 
    20 #define MAXN 1000000+5
    21 #define MOD 1e9+7
    22 #define PI acos(-1)
    23 #define EPS 1e-8
    24 #define MMT(s) memset(s, 0, sizeof s)
    25 typedef long long ll;
    26 typedef double db;
    27 typedef long double ldb;
    28 typedef stringstream sstm;
    29 
    30 const int INF = 0x3f3f3f3f;
    31 
    32 int n,a,b,dp[6010][2],fa[6010],h[6010];
    33 vector<int>Edge[6010];
    34 
    35 void dfs(int root){
    36     dp[root][1] = h[root];
    37     for(int i = 0; i < Edge[root].size(); i++){
    38         int son = Edge[root][i];
    39         dfs(son);
    40         dp[root][1] += dp[son][0];
    41         dp[root][0] += max(dp[son][1],dp[son][0]);
    42     }
    43 }
    44 int main(){
    45     ios_base::sync_with_stdio(false);
    46     cin.tie(0);
    47     cout.tie(0);
    48 
    49     while(cin>>n){
    50         for(int i = 1; i <= n; i++){
    51             cin>>h[i];
    52             Edge[i].clear();
    53             dp[i][0] = dp[i][1] = 0;
    54             fa[i] = -1;
    55         }
    56         while(cin>>a>>b && a && b){
    57             Edge[b].push_back(a);
    58             fa[a] = b;
    59         }
    60         int node = 1;
    61         while(fa[node] != -1)
    62             node = fa[node];
    63         dfs(node);
    64         cout << max(dp[node][1],dp[node][0]) << endl;
    65     }
    66     return 0;
    67 }
    View Code
    
    

     开始做的时候没有去存结点儿子,而是直接遍历找儿子,TLE了一发 (¬︿̫̿¬☆) 气死

    
    
  • 相关阅读:
    汇编入门学习笔记 (十二)—— int指令、port
    c#List泛型数据扩展,把List&lt;&gt;型数据格式化成List&lt;SelectListItem&gt;,用来作dropdownlist的数据
    使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务
    Centos6.5 VNC 配置
    HP服务器更换主板前后的注意事项
    【转】BCSphere入门教程01:Immediate Alert--不错
    【转】命令行使用7zip
    【转】 Android BCM4330 蓝牙BT驱动调试记录
    【转】android-support-v7-appcompat.jar 的安装及相关问题解决 --- 汇总整理
    【转】如何在eclipse里关联查看android源码
  • 原文地址:https://www.cnblogs.com/xenny/p/9362117.html
Copyright © 2011-2022 走看看