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

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[i][0], dp[i][1]分别表示不取节点 i 上的值和取节点 i 上的值后,以 i 为根的树在满足题目要求的下能得到的最大值

叶子节点:dp[i][0] = 0, dp[i][1] = v[i] ;

非叶子节点:    dp[i][0] = sum( max( dp[j][0], dp[j][1] ) ) , dp[i][1] = sum( dp[j][0] ) + v[i] ;

 #include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std ;
const int N = ;
int v[N], dp[N][] ;
vector<int> G[N] ;
int n, root, vis[N] ;
void _in()
{
memset(vis, , sizeof vis) ;
for(int i = ; i <= n; ++i) G[i].clear() ;
for(int i = ; i <= n; ++i) scanf("%d",&v[i]) ;
int u, v ;
while(){
scanf("%d%d",&v,&u) ;
if(!v && !u) break ;
vis[v] = ;
G[u].push_back(v) ;
}
for(int i = ; i <= n; ++i)
if(!vis[i]) { root = i ; break; }
//printf("%d--\n",root) ;
}
int get(int root, int c)
{
int& res = dp[root][c] ;
if(res != -) return res ;
int sx = G[root].size() ;
if(c) res = v[root] ;
else res = ;
for(int i = ; i < sx; ++i)
if(c) res += get(G[root][i],) ;
else res += max(get(G[root][i],), get(G[root][i],)) ;
return res ;
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin) ;
#endif
while(~scanf("%d",&n)){ //多case,囧
_in() ;
memset(dp, -, sizeof dp) ;
printf("%d\n",max(get(root,), get(root,))) ;
}
return ; }

hdu 1520 Anniversary party 基础树dp的更多相关文章

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

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

  2. 题解报告:hdu 1520 Anniversary party(树形dp入门)

    Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural Stat ...

  3. HDU - 1520 Anniversary party (树的最大独立集)

    Time limit :1000 ms :Memory limit :32768 kB: OS :Windows There is going to be a party to celebrate t ...

  4. hdu 1520 Anniversary party(入门树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6926   Accepted: 3985 ...

  5. HDU 1520 Anniversary party (树形DP,入门)

    题意:给一棵树,每个节点都有权值,要求选择部分节点出来,使得权值之和最大,但是每对(父亲,儿子)中最多只能挑一个. 思路: 比较入门的题,每个节点可以选也可以不选.若当前节点选的话,孩子必须全部不选: ...

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

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

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

  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. supersr--addSubview和 insertSubView 区别

    A addSubview B  是将B直接覆盖在A的最上层  例子: [self.view addSubview:scrollView]; A insertSubView B AtIndex:2 是将 ...

  2. [Linux] vim的高亮查找操作

    reference :  http://blog.chinaunix.net/uid-20732478-id-763411.html 使用了VIM这么久,却一直无法牢记一些基本的操作指令.今天查找一个 ...

  3. October 6th 2016 Week 41st Thursday

    The outer world you see is a reflection of your inner self. 你拥有什么样的内心,你就会看到什么样的世界. And we eventually ...

  4. 数独挑战(codevs 2924)

    2924 数独挑战  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description “芬兰数学家因卡拉,花费3 ...

  5. Android中shell命令语句

    最近学习了Android中碰到了shell命令,故收集终结了一下 Ccat zdd 浏览文件zdd的内容cat zdd1 zdd2 浏览多个文件的内容cat -n zdd浏览文件zdd的内容并显示行号 ...

  6. Lattice 的 Framebuffer IP核使用调试笔记之datasheet笔记

    本文由远航路上ing 原创,转载请标明出处. 学习使用以及调试Framebuffer IP 核已经有一段时间了,调试的时候总想记录些东西,可是忙的时候就没有时间来写,只有先找个地方记录下,以后再总结. ...

  7. PAL/NTSC 制电视广播技术有关知识--FPGA

    1.PAL和NTSC的区别 常见的电视信号制式是PAL和NTSC,另外还有SECAM等. NTSC即正交平衡调幅制,PAL为逐行倒像正交平衡调幅制. (1)PAL电视标准  PAL电视标准,每秒25帧 ...

  8. CLR via C#(16)--泛型

    泛型就像是一个模板,常常定义一些通用的算法,具体调用时再替换成实际的数据类型,提高了代码的可重用性. 一.初识泛型 1. 简单实例 以最常用的FCL中的泛型List<T >为例: stat ...

  9. ASP.NET MVC中的Global.asax文件

    1.global.asax文件概述 global.asax这个文件包含全局应用程序事件的事件处理程序.它响应应用程序级别和会话级别事件的代码. 运行时, Global.asax 将被编译成一个动态生成 ...

  10. WPF控件

    1:内容控件(Content Controls)2:条目控件(Items Controls)3:文本控件(Text Controls)4:范围控件(Range Controls) 一:内容控件 内容控 ...