码迷,mamicode.com
首页 > 其他好文 > 详细

树形dp系列

时间:2017-09-21 23:30:07      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:closed   col   clu   poj   imm   mes   employees   from   head   

hdu 1520,poj2342

Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12664    Accepted Submission(s): 5106


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
 
有搞并查集的,麻烦了。树形dp就行了。
当前点有两个状态,一个是去dp[u][1]一个不去dp[u][0]。
所以dp[u][1]=dp[uson1][0]+dp[uson2][0]+...dp[usonn][0] uson是u的孩子。
dp[u][0]=max(dp[uson1][0],dp[uson1][1])+max(dp[uson2][0],dp[uson2][1])+...max(dp[usonn][0],dp[usonn][1]),同样的概念。
难的是杭电的读入会坑人。
技术分享
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define clr(x) memset(x,0,sizeof(x))
 6 #define clr_1(x) memset(x,-1,sizeof(x))
 7 using namespace std;
 8 const int N=1e4+10;
 9 struct edg
10 {
11     int next,to;
12 }edge[N*2];
13 int head[N],ecnt;
14 int dp[N][2],value[N],fa[N];
15 void addedge(int u,int v)
16 {
17     edge[++ecnt]=(edg){head[u],v};
18     head[u]=ecnt;
19     return ;
20 }
21 void dfs(int u,int pre)
22 {
23     dp[u][0]=dp[u][1]=0;
24     for(int i=head[u];i!=-1;i=edge[i].next)
25     {
26         if(edge[i].to!=pre)
27         {
28             dfs(edge[i].to,u);
29             dp[u][0]+=max(dp[edge[i].to][1],dp[edge[i].to][0]);
30             dp[u][1]+=dp[edge[i].to][0];
31         }
32     }
33     dp[u][1]+=value[u];
34     return ;
35 }
36 int main()
37 {
38     int n,u,v,root,m;
39     while(scanf("%d",&n)!=EOF && n)
40     {
41         for(int i=1;i<=n;i++)
42             scanf("%d",&value[i]);
43         clr_1(head);
44         ecnt=0;
45         clr(fa);
46         while(scanf("%d%d",&u,&v) && u|v)
47         {
48             addedge(v,u);
49             addedge(u,v);
50             fa[u]=v;
51         }
52         for(int i=1;i<=n;i++)
53             if(fa[i]==0)
54             {
55                 root=i;
56                 break;
57             }
58         dfs(root,0);
59         printf("%d\n",max(dp[root][0],dp[root][1]));
60     }
61     return 0;
62 }
View Code

 

 
 

树形dp系列

标签:closed   col   clu   poj   imm   mes   employees   from   head   

原文地址:http://www.cnblogs.com/wujiechao/p/7571737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!