poj2342 Anniversary party (树形dp)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9128   Accepted: 5250

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

Source

大致题意:

某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚会的总活跃指数最大。

思路:

思路:

任何一个点的取舍可以看作一种决策,那么状态就是在某个点取的时候或者不取的时候,以他为根的子树能有的最大活跃总值。分别可以用f[i,1]和f[i,0]表示第i个人来和不来。

上司来,下属不来

当i来的时候,dp[i][1] += dp[j][0];//j为i的下属

上司不来,下属来或不来

当i不来的时候,dp[i][0] +=max(dp[j][1],dp[j][0]);//j为i的下属

  1. #include<iostream>
  2. #include<cmath>
  3. #include<algorithm>
  4. #include<vector>
  5. #include<cstdio>
  6. #include<cstdlib>
  7. #include<cstring>
  8. #include<string>
  9.  
  10. using namespace std;
  11.  
  12. #define maxn 6005
  13.  
  14. int n;
  15. int dp[maxn][],father[maxn];//dp[i][0]0表示不去,dp[i][1]1表示去了
  16. bool visited[maxn];
  17.  
  18. void tree_dp(int node)
  19. {
  20. int i;
  21. visited[node] = ;
  22. for(i=; i<=n; i++) //找1到n中找node节点的下属
  23. {
  24. if(!visited[i]&&father[i] == node)//i为下属
  25. {
  26. tree_dp(i);//递归调用孩子结点,从叶子结点开始dp
  27. //关键
  28. dp[node][] += dp[i][];//上司来,下属不来
  29. dp[node][] +=max(dp[i][],dp[i][]);//上司不来,下属来、不来
  30. }
  31. }
  32. }
  33.  
  34. int main()
  35. {
  36. int i;
  37. int f,c,root;
  38. while(scanf("%d",&n)!=EOF)
  39. {
  40. memset(dp,,sizeof(dp));
  41. memset(father,,sizeof(father));
  42. memset(visited,,sizeof(visited));
  43. for(i=; i<=n; i++)
  44. {
  45. scanf("%d",&dp[i][]);
  46. }
  47. root = ;//记录父结点
  48. bool beg = ;
  49. while (scanf("%d %d",&c,&f),c||f)
  50. {
  51. //c的老爸是f
  52. father[c] = f;
  53. //儿子是跟节点 或者这条边是第一条边
  54. //父亲做根节点
  55. if( root == c || beg )
  56. {
  57. root = f;
  58. }
  59. }
  60. //循环寻找根节点
  61. while(father[root])//查找父结点
  62. root=father[root];
  63. //从根节点开始树形dp
  64. tree_dp(root);
  65. //取最大的上司去或者不去中的大值
  66. int imax=max(dp[root][],dp[root][]);
  67. printf("%d\n",imax);
  68. }
  69. return ;
  70.  
  71. }

poj2342 Anniversary party (树形dp)的更多相关文章

  1. [poj2342]Anniversary party_树形dp

    Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...

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

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

  3. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

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

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

  5. hdu Anniversary party 树形DP,点带有值。求MAX

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

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

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

  7. HDU 1520 Anniversary party [树形DP]

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

  8. POJ Anniversary party 树形DP

    /* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...

  9. Anniversary party_树形DP

    Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State Univer ...

  10. HDU1520 Anniversary party 树形DP基础

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...

随机推荐

  1. Ajax应用查询员工信息

    首先要用上一篇的步骤启动服务器,建立站点.然后在该站点下创建php文件和html文件. php代码如下,文件名为server.php <?php //设置页面内容是html编码格式是utf-8 ...

  2. 删除过期备份报错RMAN-06207 RMAN-06208解决方案

    RMAN备份日志中出现了警告 日志文件目录如下: [root@erpdbs rmanback]# ll total 88 -rw-r--r-- 1 oraprod dba 81011 Sep 7 22 ...

  3. git 如何创建一个分支

    参考: https://jingyan.baidu.com/article/adc81513b95a20f723bf73bf.html 首先进入本地git仓库目录下,打开git bash环境 使用gi ...

  4. .net 程序集加载,版本不匹配的解决方法

    经常有些时候,A.dll引用的是Microsoft.EntityFrameworkCore.dll version=1.0.0.0 publicKeyToken="adb9793829dda ...

  5. Vue和JQuery相比,除了节省了开发成本,还有什么优点?

    1.模块化,变量都是私有作用域,JQuery只能用全局变量.闭包,影响性能 2.组件化 3.因为1,所以方便维护 vuex 要注意刷新清空的问题 vue-router是局部刷新,window.loca ...

  6. windows上关闭Nagle算法

    下面的设置可以调整或禁用 nagel 算法.禁用 nagel 算法以后, 允许很小的包没有延迟立即发送.建议对某些游戏关闭 nagel 算法, 这样做对文件传输/吞吐量有负面影响.默认状态( 开启na ...

  7. inet_XX族函数

    在网络编程中, 经常会将网络字节转为本地字节或者将本地字节转为网络字节, 但是如果每次我们都是都通过htonl, ntohl函数需要将10进制转为整数, 甚至还用将字符串转为整数, 再转为网络字节, ...

  8. 比n大的最小不重复数

    void Calculate(int a) { int pa = a; ; ] = {}; //将pa按位存储到数组b ) { b[count++] = pa%; pa = pa/; } bool f ...

  9. Shell 脚本编程 基本语法:

    Shell 脚本编程语法: 注: 文章来源 http://www.cnblogs.com/yunquan/p/6821850.html 视频来源:https://www.bilibili.com/vi ...

  10. Whl自助搜索下载器

    本文转载自以下链接:https://github.com/Light-City/AutoDownloadWhl 源码地址: https://github.com/Light-City/AutoDown ...