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. split+ Pattern切割字符串

    今天在对一个String对象进行拆分的时候,总是无法到达预计的结果.呈现数据的时候出现异常,后来debug之后才发现,错误出在String spilt上,于是开始好好研究下这东西,开始对api里的sp ...

  2. 树的深度优先遍历和广度优先遍历的原理和java实现代码

    import java.util.ArrayDeque; public class BinaryTree { static class TreeNode{ int value; TreeNode le ...

  3. linq to xml There are multiple root elements.

    添加xml结点的时候 var temp2 = temp1.Element("staticContent"); if (temp2 != null) { string str = & ...

  4. 一步一步学Silverlight 2系列(22):在Silverlight中如何用JavaScript调用.NET代码

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  5. php中自运算++ 或-- 的总结

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  6. 开启sqlplus中执行计划

    在sqlplus中我们一般用Autotrace来查看执行计划,从而对于一些语句执行过程分析,开展优化工作.这里就演示一下如何将autotrace权限授予给普通的用户,以scott用户为例(set au ...

  7. 使用slot分发内容

    为了让组件可以组合,我们需要一种方式来混合父组件的内容与子组件的模板.这个过程被称为 内容分发 使用特殊的<slot>元素作为原始内容的插槽 除非子组件模板包含至少一个<slot&g ...

  8. 《Eye In-Painting with Exemplar Generative Adversarial Networks》论文阅读笔记

    Abstract 基于conditional GAN使用隐藏在reference image中的exemplar information生成high-quality,personalized in-p ...

  9. Java递归应用:输出树形菜单

    转自:https://blog.csdn.net/zhangzeyuaaa/article/details/24574769

  10. 聊聊Web App、Hybrid App与Native App的设计差异(转)

    目前主流应用程序大体分为三类:Web App.Hybrid App. Native App. 一.Web App.Hybrid App.Native App 纵向对比 首先,我们来看看什么是 Web ...