首页 > 其他 > 详细

Anniversary party解题报告

时间:2014-03-18 12:01:44      阅读:402      评论:0      收藏:0      [点我收藏+]

 

Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)



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 3
2 3
6 4
7 4
4 5
3 5
0 0
 

 

Sample Output
5
问题描述:
每个人有自己的欢乐值,不能和自己的上司同时出现在party上。第一行输入N,代表N个人。下面N行代表1...N各自的欢乐值。然后输入l,k,k是l的上司。0 0结束输入。输出是能达到的最大欢乐值和。
解法:
简单树形dp。
 dp[i][1]表示 i 参加时候以i为根的数的欢乐值。dp[i][0]表示 i 不参加时候以i为根的数的欢乐值。
状态转移方程:
dp[i][1]+=dp[j][0];//j是i的子树的根。i参加的时候,j肯定不能参加。
dp[i][0]+=max(dp[j][0],dp[j][1]);// i不参加的时候,j可以参加,也可不参加,选大的那个欢乐值
bubuko.com,布布扣
 1 #include<stdio.h>
 2 #include<string.h>
 3 int N;
 4 int dp[6005][2];
 5 typedef struct node/*孩子兄弟法*/
 6 {
 7     int brother; /*兄弟*/
 8     int next; /*孩子*/
 9     int havefather;/*是否有父亲,后面找根节点用*/
10 }node;
11 node a[6005];
12 int max(int a,int b)
13 {
14     return a>b?a:b;
15 }
16 void dfs(int x)
17 {
18     int i;
19     for(i=a[x].next;i!=0;i=a[i].brother)
20     {
21         dfs(i);
22         dp[x][1]+=dp[i][0];   /*x参加的时候,i肯定不能参加。*/
23         dp[x][0]+=max(dp[i][0],dp[i][1]);/*x不参加的时候,i可以能参加,也不可不参加,选大的欢乐值*/
24     }
25 }
26 int main()
27 {
28     int L,K;
29     int i;
30     int root;
31     while(scanf("%d",&N)!=EOF)
32     {
33         memset(dp,0,sizeof(dp));
34         for(i=1;i<=N;i++)
35         {
36             scanf("%d",&dp[i][1]);
37             a[i].brother=0;
38             a[i].havefather=0;
39             a[i].next=0;
40         }
41         while(scanf("%d%d",&L,&K),L+K>0)
42         {
43             a[L].havefather=1;
44             if(a[K].next)/*要是有孩子了,后面插入孩子用兄弟形式表示*/
45             {
46                 a[L].brother=a[K].next;
47                 a[K].next=L;
48             }
49             else
50                 a[K].next=L;
51         }
52         for(i=1;i<=N;i++)
53         {
54             if(a[i].havefather==0)
55             {
56                 root=i;
57                 break;
58             }
59 
60         }
61         dfs(root);
62         printf("%d\n",max(dp[root][1],dp[root][0]));
63 
64     }
65     return 0;
66 }
bubuko.com,布布扣

 

 
 

Anniversary party解题报告,布布扣,bubuko.com

Anniversary party解题报告

原文:http://www.cnblogs.com/love-willow/p/3606431.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!