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=;
  8. int n,l,k,rt,dp[maxn][],InDeg[maxn];bool vis[maxn];vector<int>vec[maxn];
  9. void dfs(int root){
  10. for(size_t i=;i<vec[root].size();++i){
  11. int v=vec[root][i];
  12. dfs(v);//这里只会对整棵树中每个节点遍历一次,并不会重复访问,所以可以不使用vis数组
  13. dp[root][]+=dp[v][];//root去,则v不去(1表示去,0表示不去),累加不去的最大值
  14. dp[root][]+=max(dp[v][],dp[v][]);//root不去,取v去或不去的最大值
  15. }
  16. }
  17. int main(){
  18. while(~scanf("%d",&n)){
  19. memset(dp,,sizeof(dp));
  20. memset(InDeg,,sizeof(InDeg));
  21. for(int i=;i<=n;++i)vec[i].clear();
  22. for(int i=;i<=n;++i)scanf("%d",&dp[i][]);//刚开始都要去的都有这个权值
  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=;i<=n;++i)
  28. if(!InDeg[i]){rt=i;break;}//找到树的根节点,其入度为0
  29. dfs(rt);
  30. printf("%d\n",max(dp[rt][],dp[rt][]));
  31. }
  32. return ;
  33. }

题解报告: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. Mac中Maven的安装步骤

    1.下载Maven,并解压到某个目录. 2.打开terminal,输入一下命令. open .bash_profile; 3.在bash_profile中,编辑文件  内容如下. 4.保存bash_p ...

  2. jetty java文件无法删除 java文件占用 delete无效 运行时锁定静态资源的解决方法

    前几天jetty下发现java无法删除文件,文件操作后一直被jvm占用,无奈换了tomcat问题消失. 今天又想起来,尝试网上的解决方法,经本人试验,直接修改配置文件有时不能生效,具体原因不清楚,建议 ...

  3. XFire WebService demo

    XFire创建WebService实例应用 XFire使得在JavaEE应用中发布Web服务变得轻而易举.和其他Web服务引擎相比,  XFire的配置非常简单,可以非常容易地和Spring集成.   ...

  4. QT实现FTP服务器(二)

    QClientThread类的实现: #include "QClientThread.h" #include <QDebug> /******************* ...

  5. js中获取时间new date()的用法 获取时间:

    获取时间: 1 var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.getF ...

  6. vue中显示和隐藏导航

    const router = new VueRouter({ mode: 'history', routes: [ { path: '/first', component: firstView, me ...

  7. springboot在eclipse实现热部署

    eclipse使用spring-tool-suite插件创建springboot项目,项目创建完成后. 选中项目,右键 Spring Tools  --> Add Boot Devtools 点 ...

  8. iOS NSString拼接字符串

    NSString* str_C; // 结果字符串NSString* str_A, str_B; //已存在的字符串,需要将str_A和str_B连接起来 //方法1 str_C = [NSStrin ...

  9. AM335x Android eMMC mkmmc-android.sh hacking

    # AM335x Android eMMC mkmmc-android.sh hacking # # . 有空解读一下android的分区文件. # . 代码来源:https://github.com ...

  10. 【CQ18阶梯赛第二场】题解

    [A-H国的身份证号码I] 用N个for语句可以搞定,但是写起来不方便,所以搜索. dfs(w,num,p)表示搜索完前w位,前面x组成的数位num,最后以为为p. 如果搜索到第N位,则表示num满足 ...