Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12770    Accepted Submission(s): 5142

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
 
Source
 
Recommend
linle   |   We have carefully selected several similar problems for you:  1561 1011 2196 1494 2242 
 
题意:有n个人,他们之间有上下级关系并且关系形成了一棵树,现在要求直接上下级的人不能同时参加聚会,每个人参加都有一个快乐值,问最大的快乐值。
思路:基础的树形dp题,dp[i][0]表示i不参加聚会时最大的快乐值;dp[i][1]表示i参加聚会时最大的快乐值。那么就有如下关系:
dp[u][1]+=dp[v][0];当u参加市,u的儿子v均不参加
dp[u][0]+=max(dp[v][1],dp[v][0]);当u不参加时,u的儿子可以参加,也可以不参加,取最大。
代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<queue>
  7. #include<stack>
  8. #include<map>
  9. #include<vector>
  10. #include<set>
  11. #include<bitset>
  12. using namespace std;
  13. #define PI acos(-1.0)
  14. #define eps 1e-8
  15. typedef long long ll;
  16. typedef pair<int,int> P;
  17. const int N=1e5+,M=1e5+;
  18. const int inf=0x3f3f3f3f;
  19. const ll INF=1e18+,mod=1e9+;
  20. struct edge
  21. {
  22. int from,to;
  23. int next;
  24. };
  25. edge es[M];
  26. int cnt,head[N];
  27. int in[N];
  28. int dp[N][];
  29. void init()
  30. {
  31. cnt=;
  32. memset(head,-,sizeof(head));
  33. memset(in,,sizeof(in));
  34. memset(dp,,sizeof(dp));
  35. }
  36. void addedge(int u,int v)
  37. {
  38. cnt++;
  39. es[cnt].from=u,es[cnt].to=v;
  40. es[cnt].next=head[u];
  41. head[u]=cnt;
  42. }
  43. void dfs(int u)
  44. {
  45. for(int i=head[u]; i!=-; i=es[i].next)
  46. {
  47. int v=es[i].to;
  48. dfs(v);
  49. dp[u][]+=dp[v][];
  50. dp[u][]+=max(dp[v][],dp[v][]);
  51. }
  52. ///cout<<u<<" * "<<dp[u][0]<<" * "<<dp[u][1]<<endl;
  53. }
  54. int main()
  55. {
  56. int n;
  57. while(~scanf("%d",&n))
  58. {
  59. init();
  60. for(int i=; i<=n; i++) scanf("%d",&dp[i][]);
  61. int u,v;
  62. while(scanf("%d%d",&u,&v)&&!(u==&&v==))
  63. {
  64. addedge(v,u);
  65. in[u]++;
  66. }
  67. int root;
  68. for(int i=; i<=n; i++)
  69. if(!in[i]) root=i;
  70. dfs(root);
  71. printf("%d\n",max(dp[root][],dp[root][]));
  72. }
  73. return ;
  74. }

基础树形dp

HDU 1520.Anniversary party 基础的树形dp的更多相关文章

  1. TTTTTTTTTTT hdu 1520 Anniversary party 生日party 树形dp第一题

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

  2. hdu 1520 Anniversary party || codevs 1380 树形dp

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

  3. hdu 1520 Anniversary party 基础树dp

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

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

  5. hdu 1520 Anniversary party(第一道树形dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  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. HDU 1520 Anniversary party(DFS或树形DP)

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

  9. hdu 1561 The more, The Better(树形dp,基础)

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

随机推荐

  1. Appium自动化学习1

    1.Appium-desktop配置Desired Capabilities { "platformName": "Android", "platfo ...

  2. FPGA笔试必会知识点1--数字电路基本知识

    组合逻辑与时序逻辑 组合逻辑电路:任意时刻电路输出的逻辑状态仅仅取决于当时输入的逻辑状态,而与电路过去的工作状态无关. 时序逻辑电路:任意时刻电路输出的逻辑状态不仅取决于当时输入的逻辑状态,而与电路过 ...

  3. python3 写excel文件 xlsxwriter模块

    之前一直用这个传说中可以让python飞起来的xlwings模块来写入excel文件,今天发现xlsxwriter模块,发现这才是飞起来的feel!! 使用体验对比: xlwings:写入7000+单 ...

  4. python2操作MySQL

    #coding=utf-8   import MySQLdb   conn = MySQLdb.connect(host='localhost',user='root',passwd='123456' ...

  5. jtable时间编辑器

    最近在做一个项目,很烦,用的swing,但是不管怎样也还是啃下来了,但是碰到一个问题,要在jtable里编辑时用一个时间选择器,因为走了许多弯路,找到挺多jar包,耗时较久,所以记录一下,便于以后查阅 ...

  6. react-native-table-component, react-native 表格

    使用 react-native-table-component, 加上 FlatList 组件,实现可以下拉刷新,上拉加载的demo import React, { Component } from ...

  7. Redis广播

    参见:http://blog.csdn.net/u011734144/article/details/51782085

  8. Oracle :多表更新多个字段

    https://blog.csdn.net/funnyfu0101/article/details/52765235 总体原则:1)更新的时候一定要加where条件,否则必然引起该字段的所有记录更新 ...

  9. [Docker] 容器持久化数据的首选机制 Volume

    Volume 是 docker 容器生成持久化数据的首选机制.bind mounts 依赖主机机器的目录机构,volume 完全由 docker 管理.volume 较 bind mounts 有几个 ...

  10. jquery中checkbox的选中,反选,全不选 注意1.6版本以上将attr改成prop

    <script type="text/javascript"> $(function () { // 全选 $("#btnCheckAll").bi ...