Anniversary partyTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5333 Accepted Submission (s): 2459
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 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 a employee and his or her immediate supervi Sor to is present. The personnel office has evaluated conviviality of all employee, so everyone had 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 is 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 the 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 was 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 Input
711111111 32 36 47 44 53 50 0
Sample Output
5
Test instructions: The company party,n an individual's active value, and then tells the rank-to-position relationship, and the staff and direct boss cannot participate at the same time, asking for the maximum number of people to attend
#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <cmath > #include <algorithm> #include <vector> #define N 6009using namespace Std;int n;vector<int>v[n]; int dp[n][2];//boolean array int fa[n];int f[n];void DFS (int node) {int len=v[node].size (); Dp[node][1]=f[node]; for (int i=0;i<len;i++) DFS (V[node][i]); for (int i=0;i<len;i++) {dp[node][1]+=dp[v[node][i]][0];//The Boss is present, the clerk must not attend Dp[node][0]+=max (Dp[v[node][i]) [0],dp[v[node][i]][1]);//Supervisor not present the staff may attend or may not attend}}int main () {while (~SCANF ("%d", &n)} {for (int i=1;i<=n ; i++) {scanf ("%d", &f[i]); V[i].clear (); dp[i][0]=dp[i][1]=0; Fa[i]=-1; } int A, b; while (~SCANF ("%d%d", &a,&b)) {if (a==0&&b==0) break; Fa[a]=b; V[b].push_back (a); } a=1; while (Fa[a]!=-1) a=fa[a]; DFS (a); printf ("%d\n", Max (dp[a][0],dp[a][1])); } return 0;}
HDU 1520Anniversary Party tree DP Getting Started