Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3862   Accepted: 2171

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 N – 1 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

  1. 7
  2. 1
  3. 1
  4. 1
  5. 1
  6. 1
  7. 1
  8. 1
  9. 1 3
  10. 2 3
  11. 6 4
  12. 7 4
  13. 4 5
  14. 3 5
  15. 0 0

Sample Output

  1. 5

【题意】

公司有n个人,每个人有价值vi,有一天举办年会,每个人都可以参加,但有严格的等级制度,参加活动时,不能同时出现a和a的上司,问如何才能使总和最大。

【分析】

每个人只有去和不去两种状态,设DP[i][0]和DP[i][1]分别表示第i个人不参加和参加年会,获得的总的最大价值。

则状态转移方程为:

    DP[i][1] += DP[j][0],

    DP[i][0] += max{DP[j][0],DP[j][1]};其中j为i的孩子节点。

    这样,从根节点r进行dfs,最后结果为max{DP[r][0],DP[r][1]}。

(分析来自yzmduncan

第2~n+1行为这n个人的价值

代码:

  1. #include "stdio.h" //简单的树形dp题
  2. #include "string.h"
  3. #include "queue"
  4. using namespace std;
  5.  
  6. #define N 60005
  7. #define INF 0x3fffffff
  8.  
  9. struct node
  10. {
  11. int x,y;
  12. int weight;
  13. int next;
  14. }edge[*N];
  15. int idx,head[N];
  16.  
  17. int root;
  18. int du[N];
  19. int value[N];
  20.  
  21. int dp[N][];
  22. int MAX(int a,int b) { return a>b?a:b; }
  23.  
  24. void Init()
  25. {
  26. idx = ;
  27. memset(head,-,sizeof(head));
  28. }
  29.  
  30. void Add(int x,int y,int weight)
  31. {
  32. edge[idx].x = x;
  33. edge[idx].y = y;
  34. edge[idx].weight = weight;
  35. edge[idx].next = head[x];
  36. head[x] = idx++;
  37. }
  38.  
  39. void DFS(int i) //
  40. {
  41. int k,j;
  42. dp[i][] = ;
  43. dp[i][] = value[i];
  44. for(k=head[i]; k!=-; k=edge[k].next)
  45. {
  46. j = edge[k].y;
  47. DFS(j);
  48. dp[i][] += MAX(dp[j][],dp[j][]);
  49. dp[i][] += dp[j][];
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. int n;
  56. int i;
  57. int x,y;
  58. while(scanf("%d",&n)!=EOF)
  59. {
  60. Init();
  61. memset(du,,sizeof(du)); //记录节点的入度
  62. memset(dp,,sizeof(dp));
  63. for(i=; i<=n; ++i)
  64. scanf("%d",&value[i]);
  65. while(scanf("%d %d",&x,&y) && x+y>)
  66. {
  67. Add(y,x,);
  68. du[x]++;
  69. }
  70. for(i=; i<=n; ++i)
  71. {
  72. if(du[i]==)
  73. root = i;
  74. }
  75. DFS(root);
  76. printf("%d\n",MAX(dp[root][],dp[root][]));
  77. }
  78. return ;
  79. }
  1.  

poj 2342 Anniversary party 简单树形dp的更多相关文章

  1. POJ 2342 Anniversary party(树形dp)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7230   Accepted: 4162 ...

  2. POJ 2342 Anniversary party (树形DP入门)

    题意: 给定一个上下属的关系树, 每个人有一个活跃值, 现在要参加一个派对, 每个人都不会和自己的上属参加派对(上属参加了,下属就不能参加了), 求参加派对的最大活跃值 分析: 枚举每个节点取与不取得 ...

  3. poj 2342 hdu 1520【树形dp】

    poj 2342 给出每个顶点的happy值,还有若干组两个顶点L,K关系,表示K是L的上司.求当K.L不同时出现时获得的happy值的最大和. 设dp[u][0]表示不选u结点时获得的最大值,dp[ ...

  4. hdu1520 Anniversary party 简单树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:树形DP的入门题 定义dp[root][1]表示以root为根节点的子树,且root本身参 ...

  5. [ACM] POJ 2342 Anniversary party (树DP获得冠军)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4410   Accepted: 2496 ...

  6. DP Intro - poj 2342 Anniversary party

    今天开始做老师给的专辑,打开DP专辑 A题 Rebuilding Roads 直接不会了,发现是树形DP,百度了下了该题,看了老半天看不懂,想死的冲动都有了~~~~ 最后百度了下,树形DP入门,找到了 ...

  7. 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 ...

  8. poj2342 Anniversary party (树形dp)

    poj2342 Anniversary party (树形dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9128   ...

  9. POJ 2342 - Anniversary party - [树形DP]

    题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...

随机推荐

  1. JavaScript 面向对象继承详解

    题记 由于js不像java那样是完全面向对象的语言,js是基于对象的,它没有类的概念.所以,要想实现继承,一般都是基于原型链的方式: 一.继承初探 大多数JavaScript的实现用 __proto_ ...

  2. MVC知识点01

    1:母版页都 放在View/Shared里面,而且全部的视图页面都可以去用母板页. **母板的应用要用到嵌套,@RenderBody();将别的网页的内容全部显示在此处,它就相当于一个占位符. 2:架 ...

  3. 【JavaScript回顾】继承

    组合继承 组合继承(combination inheritance),有时候也叫做伪经典继承,指的是将原型链和借用构造函数的 技术组合到一块,从而发挥二者之长的一种继承模式.其背后的思路是使用原型链实 ...

  4. ajax回调函数Status问题

    function readyDo() {//            alert(xhr.readyState + "分" + xhr.Status);            if ...

  5. 10个出色的NoSQL数据库

    http://www.infoq.com/research/nosql-databases?utm_source=infoqresearch&utm_campaign=lr-homepage ...

  6. Python轻量Web框架Flask使用

    http://blog.csdn.net/jacman/article/details/49098819 目录(?)[+] Flask安装 Python开发工具EclipsePyDev准备 Flask ...

  7. Win764位配置Github环境及将代码部署到Github pages-志银强势总结

    (软件及教程下载分享:链接:http://pan.baidu.com/s/1dFysay9 密码:pug0) 1-安装Git-2.9.2-64-bit.exe(解压安装文件,运行安装程序,除了记得修改 ...

  8. [moka同学笔记]yii2.0的下拉菜单与bootstrap下拉菜单

    1.yii2下拉菜单 <li class="dropdown"><a href="#" class="dropdown-toggle ...

  9. Access restriction : The constructor BASE64Decoder() is not accessible due to restriction on required library

    1.问题描述 找不到包  sun.misc.BASE64Encoder 2. 解决方案 只需要在project build path中先移除JRE System Library,再添加库JRE Sys ...

  10. xCode删除storyboard,新建window并启动

    application:didFinishLaunchingWithOptions该函数是应用程序启动之后首次加载页面的函数,删除storyboard之后,需要在这里new出新的window,初始化, ...