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. 

InputEmployees 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 0OutputOutput 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<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<memory>
using namespace std;
const int maxn=; int vis[maxn],V[maxn],ans,cnt,dp[maxn][];
int Laxt[maxn],Next[maxn],To[maxn],ru[maxn]; void add(int u,int v){
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
} int dfs(int u)
{
vis[u]=;
dp[u][]=V[u];
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(!vis[v]){
dfs(v);
dp[u][]+=dp[v][];
dp[u][]+=max(dp[v][],dp[v][]);
}
}
} int main()
{
int i,j,k,n,u,v;
while(~scanf("%d",&n)){
cnt=;ans=;
memset(Laxt,,sizeof(Laxt));
memset(dp,,sizeof(dp));
memset(ru,,sizeof(ru));
memset(vis,,sizeof(vis));
for(i=;i<=n;i++) scanf("%d",&V[i]);
for(;;){
scanf("%d%d",&u,&v);
if(u==&&v==) break;
add(v,u);
ru[u]++;
}
for(i=;i<=n;i++) {
if(ru[i]==) {
dfs(i);
ans+=max(dp[i][],dp[i][]);//防止多棵树
}
}
printf("%d\n",ans);
}
return ;
}

HDU1520 Anniversary party 树形DP基础的更多相关文章

  1. HDU1520 Anniversary party —— 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  2. hdu1520 Anniversary party (树形dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1520题意:上司和直系下属不能同时参加party,求party的最大活跃值.输入: 输入n个 ...

  3. POJ 2342 Anniversary party 树形DP基础题

    题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人 ...

  4. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

  5. hdu1520 第一道树形DP,激动哇咔咔!

    A - 树形dp Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  6. [poj2342]Anniversary party_树形dp

    Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...

  7. POJ 2342 - Anniversary party - [树形DP]

    题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...

  8. hdu Anniversary party 树形DP,点带有值。求MAX

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

  9. 树形dp基础

    今天来给大家讲一下数形dp基础 树形dp常与树上问题(lca.直径.重心)结合起来 而这里只讲最最基础的树上dp 1.选课 题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程 ...

随机推荐

  1. Spring cloud + boot 问题记录

    1 配置中心更新值的时候,要在有需要更新的属性 类上 加入  @RefreshScope 注解 2 关于Spring Cloud 调用服务     服务名称的问题 spring: applicatio ...

  2. html和JavaScript,用户点击浏览器后退按钮,或者返回上一步自动刷新方式

    浏览器用户返回上一步,自动刷新 方式一. <input type="hidden" id="refreshed" value="no" ...

  3. Quartz(自动任务)中的触发器Trigger

    1.Quartz中的触发器TriggerJob 包含了要执行任务的逻辑,但是 Job 对何时该执行却一无所知.这个事情留给了 Trigger.Quartz Trigger 继承了抽象的 org.qua ...

  4. JS正则表达式入门,看这篇就够了

    前言 在正文开始前,先说说正则表达式是什么,为什么要用正则表达式?正则表达式在我个人看来就是一个浏览器可以识别的规则,有了这个规则,浏览器就可以帮我们判断某些字符是否符合我们的要求.但是,我们为什么要 ...

  5. nyoj520——素数序列

    最大素因子 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 GreyAnts最近正在学习数论中的素数,但是现在他遇到了一个难题:给定一个整数n,要求我们求出n的最大素 ...

  6. Django开发BUG "Model class WH_auth.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS."

    当进行数据库迁移的时候发生问题,报错如下:RuntimeError: Model class WH_auth.models.User doesn't declare an explicit app_l ...

  7. Java复习6异常处理

    Java复习6.异常处理 20131005 前言: Java中的异常处理机制是非常强大的,相比C++ 来说,更加系统.但是我们开发人员没有很好的使用这一点.一些小的程序是没有什么问题的,但是对于大型项 ...

  8. confluence 部署与配置

    什么是confluence 部署步骤 安装 下载目标平台目标版本安装包.windows为例 若第一次默认安装 一直下一步. 若再次安装 下载并将mysql 连接控件放到 /xxx/xxx/conflu ...

  9. qml 知识积累

    本文仅仅只是记录,怕日后遗忘. ListView相关应用 https://www.cnblogs.com/SaveDictator/p/8192391.html ListView 清空操作 https ...

  10. eclipse ndk 配置和简单开发demo

    记录下以备忘: android开发的各种资源国内镜像 http://www.androiddevtools.cn/ 前端时间尝鲜用android stuido1.5开发了个android的小项目,发现 ...