HDU1520 Anniversary party 解题报告【树形DP】
2017-10-18 09:00
495 查看
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])。
代码如下:
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; }
相关文章推荐
- HDU 1520 Anniversary party (树形dp) 解题报告
- hdu 1520 Anniversary party(树形DP)
- HDU 1520 Anniversary party / 树形DP水题!!!
- HDU 1520 Anniversary party 树形DP
- hdu 1520 Anniversary party(我的第一道树形DP)
- hdu1520——Anniversary party(树形dp)
- 【HDU 1520】Anniversary party(树形DP)
- HDU 1520 Anniversary party (树形dp,dfs+dp)
- HDU 1520 Anniversary party(树形DP)
- HDU 1520 Anniversary party【树形DP】
- HDU 1520 Anniversary party【树形DP】
- HDU 1520 Anniversary party(DFS或树形DP)
- 【树形DP】hdu 1520 Anniversary party
- HDU 1520 Anniversary party (简单树形DP)
- HDU 1520 & POJ 2342 Anniversary party(树形dp)
- HDU 1520 Anniversary party【树形DP】
- 【hdu】1520 Anniversary party【树形dp】
- hdu 1520 Anniversary party 树形dp入门
- HDU 1520 Anniversary party 树形DP入门题
- HDU 1520 Anniversary party 树形DP入门