HDU 1520 Anniversary party (树状dp)

来源:互联网 发布:mac照片导出无反应 编辑:程序博客网 时间:2024/04/27 18:14
Anniversary party
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]  

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

711111111 32 36 47 44 53 50 0
 

Sample Output

5
 

Source

Ural State University Internal Contest October'2000 Students Session 

 

题意:

大意是在一个聚会上,每个人都有自己的心情值,员工不能和自己的直属上司同时参加

问所能达到的最大心情总值是多少

 

代码:

#include<cstdio>#include<vector>#include<algorithm>#define MAX 6002using namespace std;vector<int> vec[MAX];int dp[MAX][2];//dp[i][0]表示不选择i点时,i点及其子树能选出的最大值//dp[i][1]表示选择i点时,i点及其子树能选出的最大值int tree[MAX];int fun[MAX];int n;void dfs(int root){int i;int len=vec[root].size();dp[root][1]=fun[root];for(i=0;i<len;i++)dfs(vec[root][i]);for(i=0;i<len;i++){dp[root][0]+=max(dp[vec[root][i]][0],dp[vec[root][i]][1]);  //若不选择root,那么root的直接下属可以选择也可以不选择dp[root][1]+=dp[vec[root][i]][0];  //若选择root,那么root的下属必定不能选择}}int main(){while(scanf("%d",&n)!=EOF){for(int i=1;i<=n;i++){scanf("%d",&fun[i]);vec[i].clear();dp[i][0]=dp[i][1]=0;tree[i]=i;}int a,b;while(scanf("%d%d",&a,&b) && (a || b)){tree[a]=b;           //b为a的直接上属vec[b].push_back(a); //a为b的直接下属(之一)}a=1;while(a!=tree[a])a=tree[a];dfs(a);printf("%d\n",max(dp[a][0],dp[a][1]));}return 0;}


思路:

树状dp

原创粉丝点击