Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15968    Accepted Submission(s): 6125

Problem Description
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 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 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
 

题意:员工的上级和员工不能一起参加聚会,每个员工都有一个权值,问怎么样使来的人权值最大

题解:每个员工(除了大老板外)都有一个上级,这样结点有子节点,相互连接,就可以形成一个树形的结构,那么在树上想要得到最优解的时候就可以想到树形DP了

如果上级来了,那么他的员工就不能来,很容易想到定义的状态为dp[maxn][2],其中定义1为来,0定义为不来

转移方程:if(i来) dp[i][1]+=dp[j][0]//j是i的子节点

else dp[i][0]+=max(dp[j][1],dp[j][0]),这里的0代表了不来,1代表了来 

代码如下

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e5+;
int f[maxn];
int vis[maxn];
int dp[maxn][];
int t;
void dfs(int node){
vis[node]=;
for(int i=;i<=t;i++){
if(!vis[i]&&f[i]==node){
dfs(i);
dp[node][]+=dp[i][];
dp[node][]+=max(dp[i][],dp[i][]);
}
}
}
int main(){
while(cin>>t){
for(int i=;i<=t;i++){
cin>>dp[i][];
}
int root=;
int l,k;
while(cin>>l>>k){
if(l==&&k==) break;
f[l]=k;
root=k;
}
memset(vis,,sizeof(vis));
dfs(root);
cout<<max(dp[root][],dp[root][])<<endl;
}
}
 

HDU1520 树形DP入门的更多相关文章

  1. hdu1520树形dp入门

    题目链接 题意:要开派对,邀请了上司就不能邀请他的下属,邀请了下属就不能邀请他的上司,每个人有一个值,求邀请的人的总值最大 第一行给出一个数n,代表有n个人. 下面n行分别给出n个人的的值 再下面n行 ...

  2. hdu1520 树形dp

    树形dp入门,在树上进行dp. 状态转移方程: dp[i][0] = max(dp[j][0], dp[j][1]);//i为j的上司 并且i不来 dp[i][1] = dp[j][0];//i来了 ...

  3. POJ 2342 树形DP入门题

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

  4. 树形dp 入门

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

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

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

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

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

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

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

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

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

  9. hdu_Anniversary party_(树形DP入门题)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:有N个人,N-1个人有自己的上司,每个人有一个快乐值,如果这个人参加了聚会,那么这个人的直 ...

随机推荐

  1. python核心编程2 第八章 练习

    8–2. 循环. 编写一个程序, 让用户输入三个数字: (f)rom, (t)o, 和 (i)ncrement . 以 i为步长, 从 f 计数到 t , 包括 f 和 t . 例如, 如果输入的是 ...

  2. laravel-多条件查询并指定key输出

    $room = DB::table('room') ->where(function($query) use($contList){ foreach ($contList as $k=>$ ...

  3. POJ:2976-Dropping tests(二分平均值)

    Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15508 Accepted: 5418 Descr ...

  4. [Codeforces958F2]Lightsabers (medium)(思维)

    Description 题目链接 Solution 设一个l指针指向当前数列左边,从左往右扫描一遍,将当前颜色记录, 当所有颜色都得到后,进行判断,如果当前l指向的颜色大于需要的颜色,l后移一位,然后 ...

  5. WPF点击不同界面上的按钮实现界面切换

    原文:WPF点击不同界面上的按钮实现界面切换 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_29844879/article/details/ ...

  6. SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID

    如题: SDK location not found. Define location with sdk.dir in the local.properties file or with an AND ...

  7. css一些事儿

    1. margin和padding 如果边界画一条线,则margin的属于边界外,padding属于边界内 当我们给元素背景色时,margin区域不会被着色,而padding区域会被着色. 当上下两个 ...

  8. 当Hadoop 启动节点Datanode失败解决

    Hadoop 启动节点Datanode失败解决 [日期:2014-11-01] 来源:Linux社区  作者:shuideyidi [字体:大 中 小] 当我动态添加一个Hadoop从节点的之后,出现 ...

  9. X的N次方。N比较大。

    final static long DIV = 1000000009; //分治法, 注意java类型为long, C++为__int64或 long long public static long ...

  10. Webdriver--获得验证信息

    title:获得当前页面的标题 current_url:获得当前页面的URL text:前面提到过,获得标签对的文本信息 try: couseTitle = driver.find_element_b ...