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
Connect Table Store Kids
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace STD; #define N 6005int dp[n][2],father[n];int n;int head[n]; int num;struct stud{int to;int Next;} e[n*2];void Add (int s,int to) {e[num].to=to;e[num].next=head[s];head[s]=num++;} void Dfs (int x) { int i,j; for (i=head[x];i!=-1;i=e[i].next) {int To=e[i].to;dfs (to);DP [x][1]+=dp[to][0];dp [X][0]+=max (Dp[to][1],dp[to][0]);}} int main () {int i,j;while (~scanf ("%d", &n)) {memset (dp,0,sizeof (DP)); memset (father,0,sizeof (father)); Memset ( Head,-1,sizeof (head)), for (i=1;i<=n;i++) scanf ("%d", &dp[i][1]), int s,e;num=0;while (scanf ("%d%d", &s, &e), s+e) {father[s]=e;add (e,s);} I=1;while (Father[i]) I=father[i];d FS (i);p rintf ("%d\n", Max (Dp[i][1],dp[i][0]));} return 0;}
Hdu1520anniversary party (stronger than POJ 2342 data)