首页 > 其他 > 详细

HDU 1520 - Anniversary party

时间:2016-08-20 10:05:42      阅读:208      评论:0      收藏:0      [点我收藏+]

Anniversary party

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


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
 
题意:
  邀请公司的聚餐,但是每个人都不能和他的上司见面,每个人都有相应的价值,求邀请的最大的价值。
思路:
  树形DP,用邻接表建立无向图,枚举每个点
  一开始数组开小了,TLE了半天。
AC代码:
技术分享
 1 # include <iostream>
 2 # include <cstring>
 3 # include <cstdio>
 4 using namespace std;
 5 const int MAX = 6010;
 6 struct node
 7 {
 8     int is;
 9     int next;
10 };
11 node tree[MAX * 2];
12 int head[MAX];
13 int tol = 0;
14 
15 int dp[MAX][2];
16 int vis[MAX];
17 int val[MAX];
18 
19 void add(int a, int b)
20 {
21     tree[tol].is = b;
22     tree[tol].next = head[a];
23     head[a] = tol++;
24     
25     tree[tol].is = a;
26     tree[tol].next = head[b];
27     head[b] = tol++;
28 }
29 void dfs(int root)
30 {
31     if(vis[root])
32         return;
33     vis[root] = 1;
34     int tep = head[root];
35     dp[root][1] = val[root];
36     
37     while(tep != -1)
38     {
39         if(!vis[tree[tep].is])
40         {
41             dfs(tree[tep].is);
42             dp[root][0] += max(dp[tree[tep].is][0], dp[tree[tep].is][1]);
43             dp[root][1] += dp[tree[tep].is][0];
44         }
45         tep = tree[tep].next;
46     }
47 }
48 int main()
49 {
50     int n;
51     while(~scanf("%d", &n))
52     {
53         memset(head, -1, sizeof(head));
54         memset(dp, 0, sizeof(dp));
55         memset(vis, 0, sizeof(vis));
56         tol = 0;
57         for(int i = 0; i < MAX; i++)
58         {
59             tree[i].is = 0;
60             tree[i].next = -1;
61         }
62         for(int i = 1; i <= n; i++)
63             scanf("%d", &val[i]);
64         
65         int a, b;
66         
67         while(scanf("%d%d", &a, &b))
68         {
69             if(a == 0 && b == 0)
70                 break;
71             add(a, b);
72         }
73         dfs(1);
74         printf("%d\n", max(dp[1][1], dp[1][0]));
75     }
76     return 0;
77 }
View Code

 

 

HDU 1520 - Anniversary party

原文:http://www.cnblogs.com/lyf-acm/p/5789707.html

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