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

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
 #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 6e3 + ;
const int INF = 0x7fffffff;
int n, dp[maxn][], fa[maxn], vis[maxn];
void solve(int x) {
vis[x] = ;
for (int i = ; i <= n ; i++) {
if (!vis[i] && fa[i] == x ) {
solve(i);
dp[x][] += max(dp[i][], dp[i][]);
dp[x][] += dp[i][];
}
}
}
int main() {
while(scanf("%d", &n) != EOF) {
memset(vis,,sizeof(vis));
memset(dp,,sizeof(dp));
memset(fa,,sizeof(fa));
for (int i = ; i <= n ; i++) scanf("%d", &dp[i][]);
int x, y, root = ;
while(scanf("%d%d", &x, &y)) {
if (x == && y == ) break;
fa[x] = y;
root = y;
}
while(fa[root]) root = fa[root];
solve(root);
printf("%d\n", max(dp[root][], dp[root][]));
}
return ;
}

hdu 1520Anniversary party 树形dp入门的更多相关文章

  1. (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520

    题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...

  2. 树形dp 入门

    今天学了树形dp,发现树形dp就是入门难一些,于是好心的我便立志要发一篇树形dp入门的博客了. 树形dp的概念什么的,相信大家都已经明白,这里就不再多说.直接上例题. 一.常规树形DP P1352 没 ...

  3. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

  4. 树形DP入门详解+题目推荐

    树形DP.这是个什么东西?为什么叫这个名字?跟其他DP有什么区别? 相信很多初学者在刚刚接触一种新思想的时候都会有这种问题. 没错,树形DP准确的说是一种DP的思想,将DP建立在树状结构的基础上. 既 ...

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

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

  6. LuoGu-P1122 最大子树和+树形dp入门

    传送门 题意:在一个树上,每个加点都有一个值,求最大的子树和. 思路:据说是树形dp入门. 用dfs,跑一边,回溯的时候求和,若和为负数,则减掉,下次不记录这个节点. #include <ios ...

  7. HDU 1561 树形DP入门

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

  8. HDU 1520 树形DP入门

    HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...

  9. HDU - 1520 树形DP入门题

    写了两种DP,第一种是按照自己习惯来xjb敲的,第二种参考别人 熟悉一下树形DP的套路 dp[i][]是维护i及以下的关系最优值的,所以我觉得两次DP记忆搜索之间不清-1应该是正确的(也就做了一次加法 ...

随机推荐

  1. Horner规则求多项式

    /* Horner */ /*多项式:A(x)=a[n]X^n+a[n-1]x^n-1+...+a[1]X^1+a[0]X^0*/ #include <stdio.h> long int ...

  2. 41-Individual authentication 模板

    1-创建项目,进入vscode控制台,输出如下命令, uld表示指定mssqllocaldb E:\coding\netcore>dotnet new mvc -au Individual -u ...

  3. 1 http协议

    1.四层模型 + 2.socket 3.http协议 4. HTTP请求 跟踪了新浪的首页,我们来总结一下HTTP请求的流程: 3.1.1 步骤1:浏览器首先向服务器发送HTTP请求,请求包括: 方法 ...

  4. 大数据de 2文章

    点击可免费试用网易有数 文章来源:网易有数的搭积木原则阐述 ,经作者文雯授权发布 wo ceceshi 相关文章:[推荐] SpringBoot入门(五)--自定义配置

  5. Unity3d脚本生命周期

    如图: 测试脚本: using UnityEngine; public class Test2 : MonoBehaviour { void Awake() { Debug.Log("Awa ...

  6. FJOI 2019 游记

    (FJOI 2019 滚粗记) Day 0 早上刷了一些水题,然后就上路了. (画图3D好好玩) 来得晚只有十几分钟时间看考场,于是连试机题都没有Ak. Day 1 果然我还是太菜了 走过来的时候再讨 ...

  7. ABP框架设置默认语言

    在Global.asax文件中设置 private readonly IAbpWebLocalizationConfiguration _webLocalizationConfiguration; p ...

  8. C 计算金额

    #include <stdio.h> int main(int argc, char **argv) { \\定义两个变量 a金额 z跟票面 int a=0; int z=0;\\ 输入金 ...

  9. cpp语言程序设计教程第七章的一道编程题

    题目如下 按下列要求实现一个有关学生成绩的操作. 该类名为Student. (1)每个学生的信息包含有姓名(字符数组)和成绩(int型). (2)共有5个学生,用对象数组表示. (3)计算出5个学生中 ...

  10. git 创建分支并提交到服务器对应的新分支

    1.切换到源分支 git checkout test 2.在源分支的基础上创建新分支 git branch test1 3.提交到远程分支 git pull 会自动提示下面的命令 git pull - ...