【HDU 1520】 Anniversary Party
【题目链接】
【算法】
树形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为根节点)
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 6010 int i,n,u,v,root;
vector<int> e[MAXN];
int val[MAXN],fa[MAXN],f[MAXN][]; inline void dfs(int x)
{
int i,y;
f[x][] = val[x];
for (i = ; i < e[x].size(); i++)
{
y = e[x][i];
dfs(y);
f[x][] += max(f[y][],f[y][]);
f[x][] += f[y][];
}
} int main()
{ while (scanf("%d",&n) != EOF)
{
for (i = ; i <= n; i++)
{
e[i].clear();
f[i][] = f[i][] = ;
}
for (i = ; i <= n; i++) scanf("%d",&val[i]);
for (i = ; i < n; i++)
{
scanf("%d%d",&u,&v);
e[v].push_back(u);
fa[u] = v;
}
scanf("%d%d",&u,&v);
for (i = ; i <= n; i++)
{
if (!fa[i])
root = i;
}
dfs(root);
printf("%d\n",max(f[root][],f[root][]));
} return ; }
【HDU 1520】 Anniversary Party的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
- 【HDU 4699】 Editor
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...
随机推荐
- [codeforces538F]A Heap of Heaps
[codeforces538F]A Heap of Heaps 试题描述 Andrew skipped lessons on the subject 'Algorithms and Data Stru ...
- POJ3528移石头
题目大意: 河道两旁直线上有两块石头不能移动,距离为L,但中间放置了N块石头并列出这N块石头到起点的距离,可以移走M块,那么移走石头后每次牛跨石头的最小距离如何达到最大值,输出这个最大值 让最小距离的 ...
- 洛谷P1432 倒水问题
题目背景 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with th ...
- bzoj1708 [Usaco2007 Oct]Money奶牛的硬币 背包dp
[Usaco2007 Oct]Money奶牛的硬币 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 852 Solved: 575[Submit][Sta ...
- HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)
Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...
- 新版VS-code如何自动换行?
文件 -> 首选项 -> 设置 -> 编辑器 找到 // 控制折行方式.可以选择: - "off" (禁用折行), - "on" (视区折行 ...
- BZOJ1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害
n<=3000个点m<=20000条无向边的图,有p<=n个出发点,每个出发点都不可拆,现拆一些点使每个出发点都不能到达点1,求最小点数. 简单的最小割.每个点拆成两个x和y,无向边 ...
- iOS FMDB 无法更新二进制数据的问题
使用FMDB很方便的实现了(通过数据库字段名而不是字段索引)数据的读取,插入,更新,删除.但是我在更新图片时发现通过格式化字符(@“%@”,data/NSData/)传入的二进制数据更新到数据库后不能 ...
- 前端学习之--CSS
CSS 常用帮助文档 CSS:被用来同时控制多重网页的样式和布局.HTML页面中CSS样式的写法有3种: 1:标签中写入 <body style='margin o auto;'> 2:h ...
- 【搜索引擎】SOLR VS Elasticsearch(2019技术选型参考)
SOLR是什么 (官方的解释) Solr是基于Apache Lucene构建的流行的.快速的.开源的企业搜索平台. Solr也是高度可靠.可伸缩和容错的,提供分布式索引.复制和负载平衡查询.自动故障转 ...