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

BNUOJ 2461 Anniversary party

时间:2014-08-16 17:04:10      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

Anniversary party

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2342
64-bit integer IO format: %lld      Java class name: Main
 
 
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 N – 1 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

 
解题:。。。。
 
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 6010;
18 int dp[maxn][2],w[maxn],n,in[maxn];
19 vector<int>g[maxn];
20 void dfs(int u,int fa){
21     dp[u][1] = w[u];
22     for(int i = 0; i < g[u].size(); i++){
23         if(g[u][i] == fa) continue;
24         dfs(g[u][i],u);
25         dp[u][1] += dp[g[u][i]][0];//u来,g[u][i]不来
26         dp[u][0] += max(dp[g[u][i]][1],dp[g[u][i]][0]);
27         //u不来,g[u][i]来或者不来
28     }
29 }
30 int main() {
31     int i,j,u,v,tmp;
32     while(~scanf("%d",&n)){
33         for(i = 1; i <= n; i++){
34             scanf("%d",w+i);
35             g[i].clear();
36             in[i] = 0;
37             dp[i][0] = dp[i][1] = 0;
38         }
39         for(i = 1; i; i++){
40             scanf("%d %d",&u,&v);
41             if(u+v == 0) break;
42             g[u].push_back(v);
43             g[v].push_back(u);
44             in[v]++;
45         }
46         for(i = 1; i <= n; i++){
47             if(!in[i]){dfs(i,-1);break;}
48         }
49         printf("%d\n",max(dp[i][0],dp[i][1]));
50     }
51     return 0;
52 }
View Code

 

BNUOJ 2461 Anniversary party,布布扣,bubuko.com

BNUOJ 2461 Anniversary party

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/crackpotisback/p/3916527.html

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