【题目链接】

点击打开链接

【算法】

树形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的更多相关文章

  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. MS SQLServer Update语法和实例

    最常用的update语法是: 1 2 UPDATE TABLE_NAME SET column_name1 = VALUE WHRER column_name2 = VALUE 如果我的更新值Valu ...

  2. SQL SERVER 2012 第四章 连接 JOIN の INNER JOIN

    所有JOIN语句的共同点是:将一个记录与另外一个或多个记录匹配,从而生成一个新记录,这个记录是由两个记录的合并列所产生的一个超集. 内部连接: 内部连接语法结构:SELECT <select l ...

  3. HDU 1402 大数乘法 FFT、NTT

    A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. uva 11691

    贪心 ~~ 使用优先队列 #include <cstdio> #include <cstdlib> #include <cmath> #include <se ...

  5. 某考试 T1 至危警告

    题目大意就是: 设f(x)为x各个位数字之和,求x属于[0,k]且b * f(x)^a + c = x的x个数并升序输出. (a<=5  .  b,c,<=10^4  .   k<= ...

  6. Spring的@Autowired注解

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-autowired-an ...

  7. Spring中使用byType实现Beans自动装配

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byType.html: 此 ...

  8. eclipse导入maven工程步骤

    转自:http://jingyan.baidu.com/article/cbf0e500a6e3252eaa2893c1.html 感谢作者 步骤一 : 选择 “Import”操作 有两个途径可以选择 ...

  9. asp.net core 集成JWT(二)token的强制失效,基于策略模式细化api权限

    [前言] 上一篇我们介绍了什么是JWT,以及如何在asp.net core api项目中集成JWT权限认证.传送门:https://www.cnblogs.com/7tiny/p/11012035.h ...

  10. .NET作品集:基于svn 的.net 持续集成工具

    作品背景 这个.net 持续集成作品还是在2014年的时候从事.net 软件项目开发的时候做的,当时部门还用着vs2008用vb.net做项目(现在也是),项目代码极混乱,版本工具用的vss,而且用的 ...