【题目链接】

点击打开链接

【算法】

树形DP

令f[i][0]表示 : 以i为根的子树中,若i不参加宴会,所能获得的最大愉悦值

f[i][1]表示 : 以i为根的子树中,若i参加宴会,所能获得的最大愉悦值

那么,如果i不参加宴会,它的下属就可以参加宴会,也可以不参加宴会,因此 :

f[i][0] = sigma( max(f[j][0],f[j][1]) ) (j为i的子节点)

如果i参加宴会,它的下属必然不能参加宴会,因此 :

f[i][1] = Ri + sigma( f[j][0] ) (j为i的子节点)

最后,答案为max(f[root][0],f[root][1])(root为根节点)

【代码】

  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cctype>
  4. #include <cerrno>
  5. #include <clocale>
  6. #include <cmath>
  7. #include <complex>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <ctime>
  12. #include <deque>
  13. #include <exception>
  14. #include <fstream>
  15. #include <functional>
  16. #include <limits>
  17. #include <list>
  18. #include <map>
  19. #include <iomanip>
  20. #include <ios>
  21. #include <iosfwd>
  22. #include <iostream>
  23. #include <istream>
  24. #include <ostream>
  25. #include <queue>
  26. #include <set>
  27. #include <sstream>
  28. #include <stdexcept>
  29. #include <streambuf>
  30. #include <string>
  31. #include <utility>
  32. #include <vector>
  33. #include <cwchar>
  34. #include <cwctype>
  35. #include <stack>
  36. #include <limits.h>
  37. using namespace std;
  38. #define MAXN 6010
  39.  
  40. int i,n,u,v,root;
  41. vector<int> e[MAXN];
  42. int val[MAXN],fa[MAXN],f[MAXN][];
  43.  
  44. inline void dfs(int x)
  45. {
  46. int i,y;
  47. f[x][] = val[x];
  48. for (i = ; i < e[x].size(); i++)
  49. {
  50. y = e[x][i];
  51. dfs(y);
  52. f[x][] += max(f[y][],f[y][]);
  53. f[x][] += f[y][];
  54. }
  55. }
  56.  
  57. int main()
  58. {
  59.  
  60. while (scanf("%d",&n) != EOF)
  61. {
  62. for (i = ; i <= n; i++)
  63. {
  64. e[i].clear();
  65. f[i][] = f[i][] = ;
  66. }
  67. for (i = ; i <= n; i++) scanf("%d",&val[i]);
  68. for (i = ; i < n; i++)
  69. {
  70. scanf("%d%d",&u,&v);
  71. e[v].push_back(u);
  72. fa[u] = v;
  73. }
  74. scanf("%d%d",&u,&v);
  75. for (i = ; i <= n; i++)
  76. {
  77. if (!fa[i])
  78. root = i;
  79. }
  80. dfs(root);
  81. printf("%d\n",max(f[root][],f[root][]));
  82. }
  83.  
  84. return ;
  85.  
  86. }

【HDU 1520】 Anniversary Party的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  5. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  6. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  7. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  8. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

  9. 【HDU 4699】 Editor

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...

随机推荐

  1. BZOJ4551 - [TJOI2016]树

    Portal Description 给出一棵\(n(n\leq10^5)\)个点的以\(1\)为根的有根树,进行\(Q(Q\leq10^5)\)次操作: 标记一个点\(x\). 询问\(x\)的祖先 ...

  2. Hankson 的趣味题(codevs 1172)

    题目描述 Description Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Hankson.现在,刚刚放学回家的Hankson 正在思考一个有趣的问题.今天在 ...

  3. 八数码难题 双向搜索(codevs 1225)

    题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们.问题描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字 ...

  4. google play上获取apk文件

    先说一种测试不通过的方法(chrome浏览器添加Direct APK downloader拓展程序),浪费了我很多的时间,结果发现根本用不了,记录一下过程给大家参考. 使用chrome浏览器,点击左上 ...

  5. Tomcat绑定具体IP

    https://blog.csdn.net/paomadeng/article/details/1826880

  6. 使用图像扫描控件ScanOnWeb实现在线图像扫描

    今天上网查资料,看到一篇文章,描述的是一个开发OA软件的公司解决浏览器嵌入式扫描仪编程的文章,文章描述了改OA厂商的工程师如何辛苦的克服了各种技术难题,最终实现了在线图像扫描处理,然后又在无数个不眠的 ...

  7. 202. Segment Tree Query

    最后更新 二刷 09-Jan-17 正儿八经线段树的应用了. 查找区间内的值. 对于某一个Node,有这样的可能: 1)需要查找区间和当前NODE没有覆盖部分,那么直接回去就行了. 2)有覆盖的部分, ...

  8. [RxJS] Implement the `map` Operator from Scratch in RxJS

    While it's great to use the RxJS built-in operators, it's also important to realize you now have the ...

  9. VB6 如何自定义代码字体和支持鼠标滚轮

    1 点击工具-选项-编辑器格式,把代码改成需要的字体和大小.(一般微软雅黑,16号字体比较好)   2 从以下网站下载VB6增强工具,可以支持鼠标滚轮代替右侧滚动条查看代码,按F3还可以切换代码窗口和 ...

  10. Python中的shelve模块

    shelve中有用的函数就是open(),但是下面编写的数据库函数中调用路径是经常出错,如果直接调用一个从来没有用过的文件却能正常运行,暂时没有找出原因. 调用shelve.open()会返回一个sh ...