题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520

Anniversary party

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

Total Submission(s): 12366    Accepted Submission(s): 5009

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
 
Source

题解:

dp[u][state]:结点u其状态为state(0或1,即不放和放)的最大值。

状态转移:

1.如果当前结点不放,那么它的子结点放不放都行,所以两者取其大。

2.如果当前结点放,那么它的子节点只能不放。

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 1e4+10; int val[MAXN], hav_fa[MAXN], dp[MAXN][2];
vector<int>son[MAXN]; int dfs(int u)
{
dp[u][0] = 0; //不放,初始化为0
dp[u][1] = val[u]; //放,初始化为本身的值
for(int i = 0; i<son[u].size(); i++)
{
int v = son[u][i];
dfs(v); //递归其子树
dp[u][0] += max(dp[v][0], dp[v][1]); //如果不放,那么它的子节点放不放都行,所以取其大。
dp[u][1] += dp[v][0]; //如果放,那么它的子节点不能放
}
return max(dp[u][0], dp[u][1]); //返回最大值
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i = 1; i<=n; i++)
son[i].clear(), hav_fa[i] = 0; for(int i = 1; i<=n; i++)
scanf("%d",&val[i]); int u, v;
while(scanf("%d%d",&v,&u) && (v || u))
{
son[u].push_back(v);
hav_fa[v] = 1;
} for(int i = 1; i<=n; i++) //找到根节点,然后深搜
if(!hav_fa[i])
cout<< dfs(i) <<endl;
}
}

HDU1520 Anniversary party —— 树形DP的更多相关文章

  1. HDU1520 Anniversary party 树形DP基础

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...

  2. hdu1520 Anniversary party (树形dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1520题意:上司和直系下属不能同时参加party,求party的最大活跃值.输入: 输入n个 ...

  3. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

  4. hdu1520 第一道树形DP,激动哇咔咔!

    A - 树形dp Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  5. [poj2342]Anniversary party_树形dp

    Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...

  6. POJ 2342 - Anniversary party - [树形DP]

    题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...

  7. hdu Anniversary party 树形DP,点带有值。求MAX

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

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

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

  9. HDU 1520 Anniversary party [树形DP]

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

随机推荐

  1. cf396B On Sum of Fractions

    Let's assume that v(n) is the largest prime number, that does not exceed n; u(n) is the smallest pri ...

  2. 【Codeforces Round #502 (Div. 1 + Div. 2) 】

    A:https://www.cnblogs.com/myx12345/p/9843032.html B:https://www.cnblogs.com/myx12345/p/9843050.html ...

  3. 【Tomcat】linux下实时查看tomcat运行日志

    今天在部署一个项目到linux服务器的时候一直报错,可是在日志文件中也没有记录.但是在本地测试的时候都没有错误,在windoesServer服务器上也没错误,实在找不到原因,因此想的实时查看tomca ...

  4. 每日记录 2016-4-29 HTML5本地存储

    HTML5本地存储 一.HTML5 localStorage 在HTML5中,本地存储是一个window的属性,包括localStorage和 sessionStorage,从名字应该可以很清楚的辨认 ...

  5. BZOJ——2096: [Poi2010]Pilots

    http://www.lydsy.com/JudgeOnline/problem.php?id=2096 Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: ...

  6. codevs——2651 孔子教学——同桌

    2651 孔子教学——同桌  时间限制: 1 s  空间限制: 8000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 孔子是我国古代著名的教育家.他有先见 ...

  7. windows安装RabbitMQ注意事项

    1.首先下载好ERLANG.RabbitMQ安装包,先安装erlang,设置好环境变量,然后再去安装MQ; 2.别人有两个报错: 一:RabbitMQ安装目录中不允许有空格; 二:安装rabbitmq ...

  8. Ubuntu 16.04下在Shell终端下使用nautilus快速打开窗口文件夹

    Ubunut 16.04默认使用nautilus进行管理资源文件夹,nautilus默认是支持参数传递的. 使用: nautilus /dirurl 打开当前文件夹(可以使用$PWD代替): naut ...

  9. ZT:150条毒鸡汤

    1.照照镜子吧,还要什么段子? 2.多年过去,再回忆高考,其实本质上没有考到好与坏的说法,重要的是年轻人在一起,做份试题,然后决定去哪座城市做代购. 3.真正努力过的人,就会明白天赋的重要性. 4.转 ...

  10. 浅析怎样学好C语言

    今天,我能够自称是一个混IT的人,并能以此谋生,将来大家能一次谋生.都要感谢两个人:克劳德.香农和约翰.冯.诺依曼,是他们发现了全部的数字化信息,不论是一段程序,一封email.一部电影都是用一连串的 ...