HDU1520 Anniversary party 解题报告【树形DP】
来源:互联网 发布:手绘板画图软件 编辑:程序博客网 时间:2024/05/22 17:36
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
解题报告
我们定义dp[u][0/1]表示在u节点不染色/染色的情况下,其子树的最优答案。
我们考虑转移,由于需要间隔染色,则有:
dp[u][1]+=dp[v][0];
dp[u][0]+=max(dp[v][1],dp[v][0])。
最后答案就是max(dp[root][1],dp[root][0])。
代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=6000;struct edge{ int v,next;}ed[N+5];int n,w[N+5];int head[N+5],num;int dp[N+5][2],father[N+5];void build(int u,int v){ ed[++num].v=v; ed[num].next=head[u]; head[u]=num;}void dfs(int u){ dp[u][1]=w[u]; for(int i=head[u];i!=-1;i=ed[i].next) { int v=ed[i].v; dfs(v); dp[u][1]+=dp[v][0]; dp[u][0]+=max(dp[v][1],dp[v][0]); }}int main(){ while(~scanf("%d",&n)) { memset(head,-1,sizeof(head));num=0; memset(father,-1,sizeof(father)); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++)scanf("%d",&w[i]); int u,v; while(~scanf("%d%d",&u,&v)) { if(!u&&!v)break; father[u]=v; build(v,u); } int root=1; while(father[root]!=-1)root++; dfs(root); printf("%d\n",max(dp[root][0],dp[root][1])); } return 0;}
- HDU1520 Anniversary party 解题报告【树形DP】
- hdu1520 Anniversary party 树形dp
- HDU1520:Anniversary party(树形DP)
- hdu1520 Anniversary party(树形DP)
- hdu1520 Anniversary party (树形dp)
- hdu1520 Anniversary party 【树形dp】
- HDU1520 Anniversary party(树形DP)
- 【树形DP】HDU1520-Anniversary party
- HDU1520 Anniversary party 树形DP
- HDU1520 Anniversary party[树形DP]
- HDU1520 Anniversary party 树形DP
- hdu1520-树形dp-Anniversary party
- hdu1520 Anniversary party 树形dp
- HDU1520-Anniversary party(树形dp)
- 树形dp Anniversary party(HDU1520)
- hdu1520 Anniversary party(poj2342,树形dp)
- HDU1520 Anniversary party(树形DP)
- HDU1520 Anniversary party(树形dp)
- QAbstractItemView,QTableView
- [ 数据结构 ] DS线性表综合练习--数制转换
- java 面试问的问题和个人理解(三年)(一)
- stlink gdb server
- iCheck表单美化插件使用方法详解(含参数、事件等)
- HDU1520 Anniversary party 解题报告【树形DP】
- wps白色背景设置为淡绿色保护眼睛预防眼疲劳
- findContours函数报错:“将一个无效参数传递给了将无效参数视为严重错误的函数”解决方案之一
- 微信摇一摇周边设置
- TensorFlow 分布式集群 Local Server
- 使用git回滚之后,如何再滚回去
- wps怎么保存网页上的内容通过在线素材一键实现
- 基于活度系数模型的气液平衡计算
- c中的typedef