Anniversary party
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 4606 |
|
Accepted: 2615 |
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
Source
Ural State University Internal Contest October ' Students Session
POJ 2342
Topic Meaning:
The minimum point weight of a tree is covered ;
Problem Solving Ideas:
The tree-like dp, the state transition equation is:
Dp[i][0]=max (Dp[son1][0],dp[son1][1]) +max (dp[son2][0],dp[son2][1]) +...max (dp[sonm][0],dp[sonm][1]);
Dp[i][1]=sum (Dp[son][0]);
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cstdio>6 using namespacestd;7 Const intmaxn=6007;8 BOOLVISIT[MAXN];9 intVALUE[MAXN];Ten intHEAD[MAXN]; One intdp[maxn][2]; A intsum=0; - intN; - structnode the { - intTo,next; - }; - node TREE[MAXN]; + voidInit () - { +memset (head,-1,sizeof(head)); Amemset (Visit,0,sizeof(visit)); atMemset (DP,0,sizeof(DP)); -memset (Value,0,sizeof(value)); -sum=0; - } - voidAddedge (intUintv) - { intree[sum].to=v; -tree[sum].next=head[u];head[u]=sum; tosum++; + } - voidDfs_tree (intPoint ) the { * intres0=0, res1=0; $ if(Visit[point])return;Panax Notoginsengvisit[point]=1; - for(inti=head[point];i!=-1; i=tree[i].next) the { + if(!visit[tree[i].to]) dfs_tree (tree[i].to); Ares1+=dp[tree[i].to][0]; theRes0+=max (dp[tree[i].to][0],dp[tree[i].to][1]); + } -dp[point][0]=Res0; $dp[point][1]=res1+Value[point]; $ } - intMain () - { the //freopen ("In.txt", "R", stdin); - while(~SCANF ("%d",&N)) {Wuyi init (); the for(intI=1; i<=n;i++) - { Wuscanf"%d",&value[i]); - } About intb; $ while(1) - { -scanf"%d%d",&a,&b); - if(a==0&&b==0) Break; A Addedge (b,a); +visit[a]=1; the } - intRoot; $ for(intI=1; i<=n;i++) the { the if(visit[i]==0) root=i; the } thememset (Visit,0,sizeof(visit)); - Dfs_tree (root); in intAns=max (dp[root][0],dp[root][1]); theprintf"%d\n", ans); the } About return 0; the}
POJ 2342 tree-like DP