首页 > 代码库 > BNUOJ 2461 Anniversary party
BNUOJ 2461 Anniversary party
Anniversary party
Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 234264-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
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
711111111 32 36 47 44 53 50 0
Sample Output
5
Source
Ural State University Internal Contest October‘2000 Students Session
解题:。。。。
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 long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。