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. maven下载与配置环境变量

    1.打开maven官网下载 http://maven.apache.org/download.cgi 下载 解压(这里博主放在D盘下) 2.配置环境变量 验证是否配置成功 3.本地仓库配置

  2. 决策树的剪枝,分类回归树CART

    决策树的剪枝 决策树为什么要剪枝?原因就是避免决策树“过拟合”样本.前面的算法生成的决策树非常的详细而庞大,每个属性都被详细地加以考虑,决策树的树叶节点所覆盖的训练样本都是“纯”的.因此用这个决策树来 ...

  3. 机器学习笔记—K-均值聚类

    在聚类问题中,给定训练集 {x(1),...,x(m)},要把数据分成内聚的“簇”.这里 x(i)∈R,没有 y(i).所以,这是一个无监督学习问题. k-均值聚类算法如下: 1.随机初始化簇中心 μ ...

  4. 简单易用的分页类实例代码PHP

    <?php /*********************************************** * @类名: page * @参数: $myde_total - 总记录数 * $m ...

  5. SPA(单页面web应用程序)

    单页web应用(single page web application,SPA),就是只有一张web页面的应用,是加载单个HTML页面并在用户与应用程序交互时动态更新该页面的web应用程序. 浏览器一 ...

  6. vue.js引用出错-script代码块放在head和body中的区别

    这篇随笔是为了记录vue.js引用出错的原因,看到最后原来是vue.js代码放在head中不能正常使用,要最后发现要将其放在body中才行... 原来是js代码放在head和body中的区别问题,占个 ...

  7. webpack入门配置

    webpack入门配置 根据该篇文章进行配置: 入门 Webpack,看这篇就够了 其中由于版本更新的问题会出现几个问题: 1.Would you like to install webpack-cl ...

  8. 缓存技术内部交流_02_Ehcache3 XML 配置

    参考资料: http://www.ehcache.org/documentation/3.2/getting-started.html#configuring-with-xml http://www. ...

  9. 关于 Token,你应该知道的十件事

    转自:http://ju.outofmemory.cn/entry/134189 原文是一篇很好的讲述 Token 在 Web 应用中使用的文章,而这是我和 Special 合作翻译的译文. 1. T ...

  10. Art-Template模板引擎(原生写法与简洁写法)

    模板引擎:把js数据转换成html需要的页面,这就是模板引擎需要做的事     • native原生语法     1. 准备数据     2. 把数据转化成html格式的字符串 使用模板引擎 artT ...