Anniversary party
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 9066 Accepted Submission (s): 3896
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 Input711111111 32 36 47 44 53 50 0
Sample OUTPUT5compare the underlying tree DP
1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <queue>5#include <stack>6#include <vector>7#include <iostream>8#include"algorithm"9 using namespacestd;TentypedefLong LongLL; One Const intmax=6005; A intN,root; - intA[max]; - inttot; the intHead[max],adj[max],next[max]; - intf[max][2]; - voidAddedge (intUintv) { -tot++; +adj[tot]=v; -next[tot]=Head[u]; +head[u]=tot; A } at voidinit () { - inti,j; - intu,v; - intT[max]; -memset (T,0,sizeof(t)); -scanf"%d",&n); in for(i=1; i<=n;i++) -scanf"%d", A +i); to for(i=1; i<n;i++) +{scanf ("%d%d",&v,&u); - Addedge (u,v); thet[v]++; * } $ for(i=1; i<=n;i++)Panax Notoginseng if(t[i]==0) -{root=i; the Break; + } Amemset (f,-1,sizeof(f)); the } + intDfsintXintk) { - inti,j; $ if(f[x][k]!=-1) $ returnF[x][k]; - if(k==1) -{inty=0; the for(i=head[x];i;i=Next[i]) -Y+=dfs (Adj[i],0);Wuyif[x][k]=a[x]+y; the } - Else Wu{inty=0; - for(i=head[x];i;i=Next[i]) AboutY+=max (Dfs (adj[i),0), DFS (Adj[i],1)); $f[x][k]=y; - } - returnF[x][k]; - } A intMain () { +Freopen ("party.in","R", stdin); theFreopen ("Party.out","W", stdout); - init (); $printf"%d", Max (Dfs (Root,0), DFS (root,1))); the return 0; the}
HDU-1520 Anniversary Party (tree-shaped DP)