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. 【leetcode】Substring with Concatenation of All Words (hard) ★

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  2. 【文件】读取一个文件夹下所有的jpg图片

    今天做视频处理的时候,发现给的视频是用jpg图片的形式给出的,名字的命名规律性不是很强.就想找一种通用的遍历文件夹下图片的方法. 开始在网上找到了下面这份代码,发现只能读取所有的文件夹,文件都被跳过了 ...

  3. LeetCode 441 Arranging Coins

    Problem: You have a total of n coins that you want to form in a staircase shape, where every k-th ro ...

  4. linux日常易忘指令

    1.编辑全局指令 进入~/.bash_profile 增加"exprot +(启动文件的地址)" source ~/.bash_profilr(刷新) 2.修改mysql密码 my ...

  5. 获得同级iframe页面的指定ID元素的几种实现方法

    1.JS实现: var object= window.parent.frames("要获得的iframe的name").contentDocument.getElementById ...

  6. oracle 监控

    sqlplus "/as sysdba" .监控当前数据库谁在运行什么SQL语句 SELECT osuser, username, sql_text from v$session ...

  7. SQL Server 数据类型

    数据类型的选择帮助优化查询,比如针对int类型列和针对文本类型列可能会生成完全不同的查询计划 三种数据类型: 系统数据类型 别名数据类型:用户可以为系统数据类型提供一个别名,并且可以对数据类型做进一步 ...

  8. 20145206《Java程序设计》实验二Java面向对象程序设计实验报告

    20145206<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O. ...

  9. mysql中select五种子句和统计函数

    select 五种子句顺序 where 条件 group by 分组 having 把结果进行再次筛选 order by  排序 limit  取出条目 统计函数  max(列名)  求最大 min( ...

  10. .net学习笔记---xml基础知识

    一.XML简介 XML是一种标记语言,用于描述数据,它提供一种标准化的方式来来表示文本数据.XML文档以.xml为后缀.需要彻底注意的是XML是区分大小写的. 先从一个简单的XML例子来了解下xml基 ...