hdu1520 树形DP
来源:互联网 发布:赤龙服淘宝 编辑:程序博客网 时间:2024/05/16 06:55
http://acm.hdu.edu.cn/showproblem.php?pid=1520
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
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
711111111 32 36 47 44 53 50 0
Sample Output
5
/**hdu 1520 树形dp题目大意:给定一个树形关系网,每一个员工不可以和他的直接上司同时出席聚会,并且每一个人有一个欢乐值, 问邀请那些人参加在不违反规矩的前提下总欢乐值最高。解题思路:树形dp。dp[u][0]:u不参加,以u为根节点的子树的欢乐值最高值, dp[u][0]:u参加,以u为根节点的子树的欢乐值最高值。 状态转移方程: dp[u][0]+=max(dp[v][1],dp[v][0]); dp[u][1]+=dp[v][0]; 上司参加则下属一定不参加,上司不参加下属参不参加都可以。*/#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;const int maxn=6005;struct note{ int v,next;}edge[maxn*4];int head[maxn],ip;int n,a[maxn],dp[maxn][2];void addedge(int u,int v){ edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;}void init(){ memset(head,-1,sizeof(head)); ip=0;}void dfs(int u,int pre){ dp[u][0]=0; dp[u][1]=a[u]; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==pre)continue; dfs(v,u); dp[u][0]+=max(dp[v][1],dp[v][0]); dp[u][1]+=dp[v][0]; }}int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } init(); for(int i=0;i<n;i++) { int u,v; scanf("%d%d",&u,&v); if(u==0&&v==0)continue; addedge(u,v); addedge(v,u); } memset(dp,0,sizeof(dp)); dfs(1,-1); printf("%d\n",max(dp[1][0],dp[1][1])); } return 0;}
0 0
- hdu1520 树形DP
- hdu1520(树形DP+DFS)
- hdu1520之树形dp
- hdu1520,树形dp
- hdu1520 树形DP
- hdu1520 (树形DP)
- hdu1520 树形DP
- hdu1520 树形dp
- poj2342&&hdu1520(树形dp)
- 树形dp-hdu1520
- HDU1520->树形DP
- HDU1520 树形DP裸题
- hdu1520 树形dp
- 树形DP-HDU1520
- hdu1520树形dp入门
- hdu1520 Anniversary party 树形dp
- HDU1520:Anniversary party(树形DP)
- hdu1520 Anniversary party(树形DP)
- Java异常机制Throwable
- 二进制文件 ascii码文件
- Android - Monkey测试
- Linux USB HID Device 测试代码
- 10763 - Foreign Exchange
- hdu1520 树形DP
- name 'patterns' is not defined 错误解决方法
- 游戏开发之橙子引擎尚韬:蓝海破冰 重新定义TV游戏
- 线程
- Nebula: level05
- OpenWRT开发链接收藏(不定时更新)
- 《鸟哥的Linux私房菜》读书笔记1
- JavaScript跨域总结与解决办法
- Unity编辑器中光照贴图背后的PowerVR光线追踪技术