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. 【编程题目】一串首尾相连的珠子(m 个),有 N 种颜色(N<=10),取出其中一段,要求包含所有 N 中颜色,并使长度最短。

    40.百度研发笔试题 2)一串首尾相连的珠子(m 个),有 N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有 N 中颜色,并使长度最短.并分析时间复杂度与空间复杂度. 思路: ...

  2. Oracle、SQL Server、MySQL分页方法

    测试用例:查询TEST_TABLE表中TEST_COLUMN列的第10-20条数据 1,Oracle分页方法 SELECT A.* FROM ( SELECT ROWNUM ROWNO, B.* FR ...

  3. win8访问win7中的共享文件夹 映射网络驱动器

    同一个局域网内,配置好了一台win7(假设计算机名为A)的共享文件夹,设置方法可以参考http://www.doudouxitong.com/guzhang/xitongjiqiao/2014/082 ...

  4. java获取短uuid

    public static String[] chars = new String[] { "a", "b", "c", "d&q ...

  5. linux crontab 学习

    安装crontab:[root@CentOS ~]# yum install vixie-cron[root@CentOS ~]# yum install crontabs/sbin/service ...

  6. viewpager中彻底性动态添加、删除Fragment

    为了解决彻底删除fragment,我们要做的是:1.将FragmentPagerAdapter 替换成FragmentStatePagerAdapter,因为前者只要加载过,fragment中的视图就 ...

  7. .net转的时间戳用java去解析的代码

    /// <summary> /// 转换成java解析一致的时间戳 /// </summary> /// <param name="time"> ...

  8. JavaScript - 基本概念

    相等操作符 == 和 != 是先转换再比较 === 和 !== 是直接比较,不经过转换 中文翻译上来说叫做相等和全等.类型转换简单的概括就是高级转低级,比如说布尔字符串转数字,对象取值再转.相等比较有 ...

  9. PHPCMS 实现上一篇下一篇的几种方法

    1第一种 <p>上一篇:{get sql = "select contentid,catid,url,titlee from phpcms_content where conte ...

  10. mysql编译时报的一个警告warning: type-punning to incomplete type might break strict-aliasing rules,可能是bug

    cmake的时候报了一个警告: /softdb/mysql-5.5.37/storage/innobase/handler/ha_innodb.cc:11870: warning: type-punn ...