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):
 #include<iostream>
#include<vector>
#include<algorithm>
#include<string.h>
#include<cstdio>
using namespace std;
const int maxn=;
int n,l,k,rt,dp[maxn][],InDeg[maxn];bool vis[maxn];vector<int>vec[maxn];
void dfs(int root){
for(size_t i=;i<vec[root].size();++i){
int v=vec[root][i];
dfs(v);//这里只会对整棵树中每个节点遍历一次,并不会重复访问,所以可以不使用vis数组
dp[root][]+=dp[v][];//root去,则v不去(1表示去,0表示不去),累加不去的最大值
dp[root][]+=max(dp[v][],dp[v][]);//root不去,取v去或不去的最大值
}
}
int main(){
while(~scanf("%d",&n)){
memset(dp,,sizeof(dp));
memset(InDeg,,sizeof(InDeg));
for(int i=;i<=n;++i)vec[i].clear();
for(int i=;i<=n;++i)scanf("%d",&dp[i][]);//刚开始都要去的都有这个权值
while(~scanf("%d%d",&l,&k)&&(l+k)){
vec[k].push_back(l);
++InDeg[l];//将l的入度加1
}
for(int i=;i<=n;++i)
if(!InDeg[i]){rt=i;break;}//找到树的根节点,其入度为0
dfs(rt);
printf("%d\n",max(dp[rt][],dp[rt][]));
}
return ;
}

题解报告:hdu 1520 Anniversary party(树形dp入门)的更多相关文章

  1. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  2. HDU 1520 Anniversary party [树形DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...

  3. hdu oj 1520 Anniversary party(树形dp入门)

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. [poj2342]Anniversary party树形dp入门

    题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max ...

  5. HDU 1520-Anniversary party(树形dp入门)

    题意: n个人参加party,已知每人的欢乐值,给出n个人的工作关系树,一个人和他的顶头上司不能同时参加,party达到的最大欢乐值. 分析:dp[i][f],以i为根的子树,f=0,i不参加,f=1 ...

  6. poj 2342 Anniversary party 树形DP入门

    题目链接:http://poj.org/problem?id=2342 题意:一家公司有1 <= N <= 6 000个职工,现要组织一些职工参加晚会,要求每个职工和其顶头上司不能同时参加 ...

  7. hdu 1011 Starship Troopers(树形DP入门)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520

    题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...

  9. [HDU 1520] Anniversary party

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

随机推荐

  1. MapReduce简述、工作流程及新旧API对照

    什么是MapReduce? 你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查而且数出有多少张是黑桃. MapReduce方法则是: 1. 给在座的全部玩家中分配这摞牌. 2. 让每一个玩家数自己手 ...

  2. Swift开发教程--怎样播放图片动画

    废话少说,直接上代码: var barsAnim = UIImageView(frame: self.view.frame); barsAnim.animationImages = NSArray() ...

  3. SHOW PROCESSLIST Syntax

    https://dev.mysql.com/doc/refman/5.7/en/show-processlist.html SHOW PROCESSLIST shows you which threa ...

  4. LIS(最长上升子序列)的三种经典求法

    求最长上升子序列的三种经典方案: 给定一个长度为 \(N\) 的数列,求它数值单调递增的子序列长度最大为多少.即已知有数列 \(A\) , \(A=\{A_1,A_2....A_n\}\) ,求 \( ...

  5. python 2: 解决python中的plot函数的图例legend不能显示中文问题

     问题: 图像标题.横纵坐标轴的标签都能显示中文名字,但是图例就是不能显示中文,怎么解决呢?  解决: plt.figure() plt.title(u'训练性能', fontproperties=f ...

  6. 前端模块化开发的规范:AMD与CDM

    AMD, 异步模块定义. CMD,通用模块规范.

  7. 皮尔逊相关系数的java实现

    相关系数的值介于–1与+1之间,即–1≤r≤+1.其性质如下:当r>0时,表示两变量正相关,r<0时,两变量为负相关.当|r|=1时,表示两变量为完全线性相关,即为函数关系.当r=0时,表 ...

  8. DPI和PPI

    写在前面 各种手机测频机构或者相关资讯老是谈及一个概念:ppi和dpi,通常总会忽略,只是稍微明白,这参数越高,说明屏幕分辨率越高:很长时间都止步如此:但作为一个iOS开发者,岂能止步如此,万一别人问 ...

  9. 你真的会使用assert吗?

    写这篇博客源于在阅读lighttpd源代码是遇到的一个关于assert应用的疑问. 在阅读lighttpd源代码时,发现比比皆是的对malloc的调用结果进行assert检查,比如:Buffer.c: ...

  10. 初学者遇到的PostgreSQL字符集问题的解决

    当初学者在使用PostgreSQL数据库,输入中文时,会遇到“ERROR:  invalid byte sequence for encoding "UTF8": 0xd6d0”的 ...