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
    解题思路:这道题跟树的最大独立集的思想很类似,只不过每个节点被赋予了一个权值,即有一群人参加聚会,要求上司和直系下属不能同时到场,求到场的人欢乐程度之和的最大值。dp[i][0]表示i没来参加,dp[i][1]表示i有来参加,状态转移方程:j是i的直系下属,①当i来参加时累加i的直系下属j没来参加的欢乐值,dp[i][1]+=dp[j][0];②当i没来参加时,dp[i][0]+=max(dp[j][1],dp[j][0]);累加去或不去这两者中的最大欢乐值。
    AC代码(78ms):
     1 #include<iostream>
     2 #include<vector>
     3 #include<algorithm>
     4 #include<string.h>
     5 #include<cstdio>
     6 using namespace std;
     7 const int maxn=6005;
     8 int n,l,k,rt,dp[maxn][2],InDeg[maxn];bool vis[maxn];vector<int>vec[maxn];
     9 void dfs(int root){
    10     for(size_t i=0;i<vec[root].size();++i){
    11         int v=vec[root][i];
    12         dfs(v);//这里只会对整棵树中每个节点遍历一次,并不会重复访问,所以可以不使用vis数组
    13         dp[root][1]+=dp[v][0];//root去,则v不去(1表示去,0表示不去),累加不去的最大值
    14         dp[root][0]+=max(dp[v][0],dp[v][1]);//root不去,取v去或不去的最大值
    15     }
    16 }
    17 int main(){
    18     while(~scanf("%d",&n)){
    19         memset(dp,0,sizeof(dp));
    20         memset(InDeg,0,sizeof(InDeg));
    21         for(int i=1;i<=n;++i)vec[i].clear();
    22         for(int i=1;i<=n;++i)scanf("%d",&dp[i][1]);//刚开始都要去的都有这个权值
    23         while(~scanf("%d%d",&l,&k)&&(l+k)){
    24             vec[k].push_back(l);
    25             ++InDeg[l];//将l的入度加1
    26         }
    27         for(int i=1;i<=n;++i)
    28             if(!InDeg[i]){rt=i;break;}//找到树的根节点,其入度为0
    29         dfs(rt);
    30         printf("%d
    ",max(dp[rt][0],dp[rt][1]));
    31     }
    32     return 0;
    33 }
  • 相关阅读:
    【转】java的string中,关于split空串总会返回单个元素的数组
    【转】Java实现将文件或者文件夹压缩成zip
    单例模式
    数据库隔离级别
    ckeditor+ckfinder
    extremecomponents
    在linux环境下重启oracle数据库,解决密码过期的问题
    20180918 begin
    hadoop免登录
    CentOS环境下通过YUM安装软件,搭建lnmp环境
  • 原文地址:https://www.cnblogs.com/acgoto/p/9590704.html
Copyright © 2011-2022 走看看