Description
There is going-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.
Input
Employees 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 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 was 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
"Test Instructions" has n individuals, each giving the value of each person, they are invited to the party, but everyone can not and their direct leadership at the same time invited, ask the invitation to achieve the maximum value of how much
"thought" dp[k][1]+=dp[i][0] means that K is the leader of I, 1 means go, 0 means not to go, the leader K went, I did not go;
Dp[k][0]+=max (Dp[i][0],dp[i][1]); leader K not go, I can go to not go, take large value
#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;Const intn=6005;intN;intfa[n],vis[n],dp[n][2];voidDfsintk) {Vis[k]=1; for(intI=1; i<=n; i++) { if(!vis[i]&&fa[i]==k) {DFS (i); dp[k][1]+=dp[i][0]; dp[k][0]+=max (dp[i][0],dp[i][1]); } }}intMain () { while(~SCANF ("%d",&N) {memset (FA,0,sizeof(FA)); for(intI=1; i<=n; i++) {scanf ("%d", &dp[i][1]); } intk=0; intb; while(SCANF ("%d%d",&a,&b)) {if(a==0&&b==0) Break; Fa[a]=b; K=b; } memset (Vis,0,sizeof(VIS)); DFS (k); printf ("%d\n", Max (dp[k][0],dp[k][1])); } return 0;}
Anniversary Party_ Tree DP