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[i][0]表示i没来参加,dp[i][1]表示i有来参加,状态转移方程:j是i的直系下属,①当i来参加时累加i的直系下属j没来参加的欢乐值,dp[i][1]+=dp[j][0];②当i没来参加时,dp[i][0]+=max(dp[j][1],dp[j][0]);累加去或不去这两者中的最大欢乐值。
AC代码(78ms):
 #include<iostream>
#include<vector>
#include<algorithm>
#include<string.h>
#include<cstdio>
using namespace std;
const int maxn=;
int n,l,k,rt,dp[maxn][],InDeg[maxn];bool vis[maxn];vector<int>vec[maxn];
void dfs(int root){
for(size_t i=;i<vec[root].size();++i){
int v=vec[root][i];
dfs(v);//这里只会对整棵树中每个节点遍历一次,并不会重复访问,所以可以不使用vis数组
dp[root][]+=dp[v][];//root去,则v不去(1表示去,0表示不去),累加不去的最大值
dp[root][]+=max(dp[v][],dp[v][]);//root不去,取v去或不去的最大值
}
}
int main(){
while(~scanf("%d",&n)){
memset(dp,,sizeof(dp));
memset(InDeg,,sizeof(InDeg));
for(int i=;i<=n;++i)vec[i].clear();
for(int i=;i<=n;++i)scanf("%d",&dp[i][]);//刚开始都要去的都有这个权值
while(~scanf("%d%d",&l,&k)&&(l+k)){
vec[k].push_back(l);
++InDeg[l];//将l的入度加1
}
for(int i=;i<=n;++i)
if(!InDeg[i]){rt=i;break;}//找到树的根节点,其入度为0
dfs(rt);
printf("%d\n",max(dp[rt][],dp[rt][]));
}
return ;
}

题解报告:hdu 1520 Anniversary party(树形dp入门)的更多相关文章

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

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

  2. HDU 1520 Anniversary party [树形DP]

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

  3. hdu oj 1520 Anniversary party(树形dp入门)

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

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

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

  5. HDU 1520-Anniversary party(树形dp入门)

    题意: n个人参加party,已知每人的欢乐值,给出n个人的工作关系树,一个人和他的顶头上司不能同时参加,party达到的最大欢乐值. 分析:dp[i][f],以i为根的子树,f=0,i不参加,f=1 ...

  6. poj 2342 Anniversary party 树形DP入门

    题目链接:http://poj.org/problem?id=2342 题意:一家公司有1 <= N <= 6 000个职工,现要组织一些职工参加晚会,要求每个职工和其顶头上司不能同时参加 ...

  7. hdu 1011 Starship Troopers(树形DP入门)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

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

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

  9. [HDU 1520] Anniversary party

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

  10. 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 ...

随机推荐

  1. 简说 call() 、apply() 、bind()

    对于这三个方法,我想一部分人还是比较陌生的. 所以今天来个简单的介绍~ 我们可以将call()和apply()看作是某个对象的方法,通过调用方法的形式来间接调用函数.call()和apply()的第一 ...

  2. OOalv 实现带出栏位描述

    .类定义 CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. METHODS: handle_data_changed_finished FOR E ...

  3. #import @import #include

    1.在xcode5以后 ,Replace #import <Cocoa/Cocoa.h> with @import Cocoa; 在这之前 必须手动设置一下才能用. 2.#import 与 ...

  4. css3中animation的应用

    1.css3 的相关属性: 相关代码: div { animation-name: myfirst; //动画的名称 animation-duration: 5s; //动画一个周期需要5秒 anim ...

  5. POJ2955 Brackets —— 区间DP

    题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  6. HTML&CSS——background: url() no-repeat 0 -64px;CSS中背景图片定位方法

    CSS中背景图片的定位,困扰我很久了.今天总算搞懂了,一定要记下来. 在CSS中,背景图片的定位方法有3种: 1)关键字:background-position: top left; 2)像素:bac ...

  7. 【转载】asp.net 后台弹出提示框

    感觉这种最好用: public void showMessage(string str_Message) { ClientScript.RegisterStartupScript(this.GetTy ...

  8. hadoop-3.0.0-alpha4启动

    全部启动或者全部停止(注意:第一次启动需要先格式,以后就不需要格式了,不能多次格式化) 1.启动 [root@master sbin]# pwd /usr/hadoop/hadoop-3.0.0-al ...

  9. 【NOIP2014】 联合权值

    [题目链接] 点击打开链接 [算法] 如果(u,v)的距离为2,那么有两种可能 : 1.u和v为祖孙关系 2.u和v为兄弟关系 树形DP即可,详见代码 [代码] #include<bits/st ...

  10. 【转】maven的安装、配置以及下载jar包

    原文地址:https://blog.csdn.net/qq_40673345/article/details/79015456 1.下载maven的压缩包,并解压到除了C盘里的maven文件夹中 2. ...