HDU 1520 Anniversary party ----树形dp

Anniversary party

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


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
 1 // 题意:一棵树每个节点有一个权值,保证父亲节点与孩子节点不能同时出现,求出现的节点最大权值。
 2 // 基础树形dp。
 3 #include <iostream>
 4 using namespace std ;
 5 
 6 struct Tree                 //用孩子兄弟表示法存储
 7 {
 8     int father ;
 9     int child ;
10     int brother ;
11     int with_max;    //表示选择该节点的子树所得的最大值
12     int without_max;  //表示不选该节点的子树所得的最大值
13     int MAX()       //现在再看这些写法,感觉很熟悉了。
14     {
15         return with_max>=without_max?with_max:without_max;
16     }
17     void init()
18     {
19         father=child=brother=without_max=0 ;
20     }
21 }tree[6001] ;
22 
23 void dfs(int id)
24 {
25     int child;
26     child=tree[id].child;
27     while(child)
28     {
29         dfs(child);
30         tree[id].with_max+=tree[child].without_max;  //dp---有父亲一定不能有儿子
31         tree[id].without_max+=tree[child].MAX();  //这里的tree[child].MAX()不能写成tree[child].with_max,因为没父亲儿子可有可无;
32         child=tree[child].brother;
33     }
34 }
35 
36 int main()
37 {
38     int n,i;
39     while(~scanf("%d",&n))
40     {
41         for(i=1;i<=n;i++)
42         {
43             scanf("%d",&tree[i].with_max);
44             tree[i].init();
45         }
46         int a,b;
47         while(scanf("%d%d",&a,&b),a||b)  //构造孩子兄弟树----很巧妙,注意方法,
48         {                                //与书上讲的孩子兄弟树的存储方法不同不同
49             tree[a].father=b;
50             tree[a].brother=tree[b].child;
51             tree[b].child=a; 
52         }
53         for(i=1;i<=n;i++)
54             if(!tree[i].father)  //题目中说的是一个学校的组织,应该不会出现单独的部门吧,
55             {                        //所以只会出现一棵树,即只有一根
56                 dfs(i) ;
57                 printf("%d\n",tree[i].MAX());
58                 break ;
59             }
60     }
61     return 0 ;
62 }

 

你可能感兴趣的:(part)