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. Centos 7 下安装mysql

    // 卸载原有maridb-lib库 [root@localhost ~]# rpm -qa | grep mariadb mariadb-libs-5.5.41-2.el7_0.x86_64 [ro ...

  2. linux安装openldap步骤

    目录 虚拟机环境:centos 7 一.环境准备 1.关闭 selinux firewalld 临时: setenforce 0  永久: vi /etc/sysconfig/selinux SELI ...

  3. Centos7 使用LVM进行新加磁盘管理

    centos7使用LVM管理一块新的磁盘   注意!文中凡是带#的都是命令标志.   一些重要概念: LV(Logical Volume)- 逻辑卷, VG(Volumne Group)- 卷组, P ...

  4. yarn 无法下载node-sass

    指定node-sass的下载源 yarn config set sass-binary-site http://npm.taobao.org/mirrors/node-sass

  5. 申请qq第三方登录 http://www.php20.com/forum.php?mod=viewthread&tid=29 (出处: 码农之家)

    百度  qq互联  进入网站 按图中的步骤申请第三方登录即可 先申请成为开发者 审核通过后再继续操作 提交 后列表中会出现提交的申请. 状态为审核中,审核通过会得到下图. 点查看 红线后面就是appi ...

  6. MOS管学习笔记

    最近在做一个小的电路设计项目,其中遇到了MOS管,经过查询资料,多年遗忘的数电.模电渐渐又浮现在我的脑海,在百度文库找到一篇比较不错的文章,把它截图使用出来,如原稿作者看到感觉侵权,请及时联系我,以便 ...

  7. POJ 3484 二分

    Showstopper Description Data-mining huge data sets can be a painful and long lasting process if we a ...

  8. POJ:2139-Six Degrees of Cowvin Bacon

    传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...

  9. 笔记-scrapy-请求-下载-结果处理流程

    笔记-scrapy-请求-下载-结果处理流程 在使用时发现对scrpy的下载过程中的处理逻辑还是不太明晰,-写个文档温习一下. 1.      请求-下载-结果处理流程 从哪开始呢? engine.p ...

  10. MVN settings.xml

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...