hdu Anniversary party 树形DP,点带有值。求MAX

Anniversary party

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

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
 
Source
 
 
典型的树形dp,很简单的。
题意:大致:校庆,邀请老师们,老师们之间有严格的层次关系,是一颗树。
      每个老师有自己的一个值rating。
      不能同时邀请 “下属” he 他/她 的直接上司。
      求满足这样的 最大的rating 和。
思路:  寻找根节点通过标记 L , 如果没有被标记过,就代表没有父亲,那就是root了。
          设dp[ k ] [ 0 ] 为不包含 k 点时,最大的和。
      设dp[ k ] [ 1 ] 为   包含 k 点时,最大的和。
    
          对于叶子节点:   dp[ k ] [ 0 ]= 0;    dp[ k ] [ 1 ]= rating[ k ];
          对于非叶子节点:
                                dp[ k ] [ 0 ]= sum{      Max(dp[ j ] [ 0 ], dp[ j ] [ 1 ])       }; 
                                                          //dp[ j ] [ 0 ]  , dp[ j ] [ 1 ] 是K的 儿子节点。
                                dp[ k ] [ 1 ]= sum{  dp [ j ] [ 0 ] } + rating[ k ];
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using namespace std;
 6 
 7 struct node
 8 {
 9     int next[6002];
10     int num;
11 }f[6002];
12 int a[6002];
13 bool use[6002];
14 int dp[6002][2];
15 
16 int Max(int x,int y)
17 {
18     return x>y? x:y;
19 }
20 
21 void dfs(int k)
22 {
23     int i,cur,j;
24     if(f[k].num==0)
25     {
26         dp[k][0]=0;
27         dp[k][1]=a[k];
28         return;
29     }
30     for(i=1;i<=f[k].num;i++)
31     {
32         j=f[k].next[i];
33         dfs(j);
34         cur=Max(dp[j][0],dp[j][1]);
35         dp[k][0]+=cur;
36 
37         dp[k][1]+=dp[j][0];
38     }
39     dp[k][1]+=a[k];
40 }
41 
42 int main()
43 {
44     int n,i,root,x,y;
45     while(scanf("%d",&n)>0)
46     {
47         for(i=1;i<=n;i++)
48             scanf("%d",&a[i]);
49 
50         for(i=1;i<=6000;i++)
51         f[i].num=0;
52         memset(use,false,sizeof(use));
53         memset(dp,0,sizeof(dp));
54         scanf("%d%d",&x,&y);
55 
56         while(1)
57         {
58             if(x==0&&y==0)break;
59             f[y].num++;
60             f[y].next[f[y].num]=x;
61             use[x]=true;
62             scanf("%d%d",&x,&y);
63         }
64 
65         for(i=1;i<=n;i++)
66             if(use[i]==false)
67             {
68                 root=i;
69                 break;
70             }
71         dfs(root);
72         printf("%d
",Max(dp[root][0],dp[root][1]));
73     }
74     return 0;
75 }
                   
 
原文地址:https://www.cnblogs.com/tom987690183/p/3393886.html