標籤:
Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6990 Accepted Submission(s): 3104
Problem DescriptionThere 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.
InputEmployees 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
OutputOutput should contain the maximal sum of guests‘ ratings.
Sample Input711111111 32 36 47 44 53 50 0
Sample Output5
SourceUral State University Internal Contest October‘2000 Students Session
題意:n個人(編號1-N),然後n行分表代表第n個人的活躍度,之後若干行 L 和 K(0 0結束),代表K是L的上司,
問一個聚會中邀請這n個人中的若干,其中不能含直接的上下級關係,可以使聚會中的活躍度最大為多少。
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #define ll long long 8 using namespace std; 9 const int N = 10000;10 int dp[N][3],root[N];11 vector<int>G[N];12 void DFS(int cur)13 {14 for(int i = 0; i < (int)G[cur].size(); i++)15 {16 int to = G[cur][i];17 DFS(to);18 dp[cur][1] += dp[to][0];//該級去,下一級不去19 dp[cur][0] += max(dp[to][0],dp[to][1]);//該級不去,下級去不去選最優20 }21 }22 23 int main(void)24 {25 int n;26 while(scanf("%d",&n) != EOF)27 {28 memset(dp,0,sizeof(dp));29 for(int i = 1; i <= n; i++)30 {31 scanf("%d",&dp[i][1]);//代表第幾個人去能增加的活躍度32 root[i] = i;//並查集的初始化33 G[i].clear();34 }35 int a,b;36 while(scanf("%d %d",&a,&b),a != 0 ||b != 0)37 {38 G[b].push_back(a);//vector建圖39 root[a] = b;40 }41 int beg;42 for(int i = 1; i <= n; i++)43 {44 if(root[i] == i)//尋找根節點45 {46 beg = i;47 break;48 }49 }50 DFS(beg);51 printf("%d\n",max(dp[beg][0],dp[beg][1]));52 }53 return 0;54 }
hdu oj 1520 Anniversary party(樹形dp入門)