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

ural 1039 树dp

时间:2017-09-03 19:32:12      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:pac   tar   ios   dia   eof   inf   each   void   ber   

http://acm.timus.ru/problem.aspx?space=1&num=1039

1039. Anniversary Party

Time limit: 0.5 second
Memory limit: 8 MB

Background

The president of the Ural State University is going to make an 80‘th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor relation forms a tree rooted at the president. Employees are numbered by integer numbers in a range from 1 to N, The personnel office has ranked each employee with a conviviality rating. In order to make the party fun for all attendees, the president does not want both an employee and his or her immediate supervisor to attend.

Problem

Your task is to make up a guest list with the maximal conviviality rating of the guests.

Input

The first line of the input contains a number N. 1 ≤ N ≤ 6000. 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 the supervisor relation tree goes. Each line of the tree specification has the form
<L> <K>
which means that the K-th employee is an immediate supervisor of L-th employee. Input is ended with the line
0 0

Output

The output should contain the maximal total rating of the guests.

Sample

inputoutput
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
5

    树上的树dp例题,f[i][0]表示以i为根不包含i可获得的最大价值,f[i][1]表示以i为根包含i在内可获得的最大价值。有f[i][0]+=MAX{f[son[i]][0],f[son[i]][1] } ,f[i][1]+=f[son[i]][0];

因为一旦包含i了显然不能包含i的儿子,所以不能加上f[son[i]][1],反之取二者中较大的就好了。

   因为如何保存他们之间的关系想了半天,想用vector来着,看书上写的很简便,利用数组做邻接表处理。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 using namespace std;
 6 #define LL long long
 7 #define inf 0x3f3f3f3f
 8 int c[6005],fa[6005],son[6005],bro[6005];
 9 int dp[6005][2];
10 void solve(int id)
11 {
12     dp[id][0]=0;
13     dp[id][1]=c[id];
14     for(int i=son[id];i!=-1;i=bro[i])
15     {
16         solve(i);
17         dp[id][0]+=max(dp[i][0],dp[i][1]);
18         dp[id][1]+=dp[i][0];
19     }
20 }
21 int main()
22 {
23     int N,i,j,k;
24     while(cin>>N){
25         int a,b,root;
26         memset(fa,-1,sizeof(fa));
27         memset(son,-1,sizeof(son));
28         memset(bro,-1,sizeof(bro));
29         for(i=1;i<=N;++i) scanf("%d",c+i);
30         while(scanf("%d%d",&a,&b)&&(a||b)){
31             fa[a]=b;
32             bro[a]=son[b];
33             son[b]=a;
34         }
35         for(i=1;i<=N;++i){
36             dp[i][0]=0,dp[i][1]=c[i];
37             if(fa[i]==-1) root=i;
38         }
39         solve(root);
40         printf("%d\n",max(dp[root][0],dp[root][1]));
41     }
42     return 0;
43 }

 

  

ural 1039 树dp

标签:pac   tar   ios   dia   eof   inf   each   void   ber   

原文地址:http://www.cnblogs.com/zzqc/p/7470406.html

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