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
题解:树形DP。对于每个人,我们储存下比他职位低的下标,并对比他职位低的人打上标记代表这个人有上司。dp[i][1]代表选i这个人,dp[i][0]代表不选这个人,然后从底层开始,对上一层dp[x][1]+=dp[x-1][0];
dp[x][0]+=max(dp[x-1][1],dp[x-1][0]);
然后最大值为: max(dp[root][1],dp[root][0]);root为没有标记的那个人,即没有任何下属的人;
参考代码为:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cstdlib>
#include<set>
using namespace std;
const int maxn=1e4+10;
int N,u,v,w[maxn],vis[maxn],dp[maxn][2];
vector<int> vec[maxn]; void solve(int x)
{
for(int i=0;i<vec[x].size();i++)
{
solve(vec[x][i]);
dp[x][1]+=dp[vec[x][i]][0];
dp[x][0]+=max(dp[vec[x][i]][0],dp[vec[x][i]][1]);
}
} int main()
{
while(~scanf("%d",&N))
{
for(int i=0;i<=N;i++) vec[i].clear();
memset(vis,0,sizeof vis);
memset(dp,0,sizeof dp);
for(int i=1;i<=N;i++) scanf("%d",w+i);
for(int i=1;i<=N;i++) dp[i][1]=w[i];
while(scanf("%d%d",&u,&v), u+v)
{
vec[v].push_back(u);
vis[u]=1;
}
int flag;
for(int i=1;i<=N;i++)
{
if(!vis[i])
{
solve(i);
flag=i;
break;
}
}
int Max=max(dp[flag][1],dp[flag][0]);
printf("%d\n",Max);
}
return 0;
}

  

HDU 1520 Anniversity party的更多相关文章

  1. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

  2. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

  3. HDU 1520 树形DP入门

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

  4. HDU 1520:Anniversary party(树形DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Problem Description   There i ...

  5. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  6. HDU 1520 Anniversary party [树形DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...

  7. HDU 1520.Anniversary party 基础的树形dp

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

  8. hdu 1520(简单树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:dp[u][0]表示不取u的最大价值,dp[u][1]表示取u的最大价值,于是有dp[u] ...

  9. hdu 1520 Anniversary party(第一道树形dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

随机推荐

  1. HashMap深入分析及使用要点

    本文内容来自深入理解HashMap.从数据结构谈HashMap.HashMap深度分析 先说使用要点. 1.不要在并发场景中使用HashMap HashMap是线程不安全的,如果被多个线程共享的操作, ...

  2. Redis实战--使用Jedis实现百万数据秒级插入

    echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 当我们 ...

  3. win10 visual studio 2017环境中安装CUDA8

    从https://developer.nvidia.com/cuda-toolkit-archive下载CUDA 8 安装 从https://developer.nvidia.com/gamework ...

  4. nyoj 54-小明的存钱计划 (遍历 + 判断)

    54-小明的存钱计划 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:5 submit:11 题目描述: 小明的零花钱一直都是自己管理.每个月的月初妈 ...

  5. php5.6开启curl

    1.   打开php安装目录,打开ext目录,是否有php_curl.dll扩展文件,如果没有该扩展文件,请在网上下载此文件. 2.   打开php.ini,找到  ;extension=php_cu ...

  6. java中的transient关键字详解

    目录 1.何谓序列化? 2.为何要序列化? 3.序列化与transient的使用 4.java类中serialVersionUID作用 5.transient关键字小结 前言 说实话学了一段时间jav ...

  7. Java流程控制之(四)中断

    目录 break continue return 标签 在程序设计时,循环直接的跳转显得十分重要,虽然Java没有提供goto语句去控制程序的跳转,但为了控制循环,Java提供了continue,br ...

  8. C#笔记01——注释、进制、基本数据类型、量和输入输出函数

    一.注释 1.单行注释 使用方法:行首加 ” //“: VS2019中的快捷键(以后如果不特加说明都是VS2019): 注释(CTRL+E,C): 取消注释(CTRL+E,U): 2.多行注释 使用方 ...

  9. selenium(java)浏览器多窗口切换处理

    要在多个窗口直接切换,首先获取每个窗口的唯一标示符(句柄),通过窗口属性可以获取所有打开窗口的标示符,以集合的形式返回:以下示例:       Set<String> winHandels ...

  10. cesium定义线面

    面: var polygon = viewer.entities.add({ polygon : { hierarchy : { positions : null, holes : [{ positi ...