Anniversary party
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 6062 |
|
Accepted: 3490 |
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 ' th Students session pressure is too difficult, first do the tree-like DP test instructions: A university to open a party, there are n people, everyone has their own pleasure value, If someone's boss is present, the person will be very disappointed, so the boss and subordinates can only go to one, ask how to arrange the most pleasant value.
Analysis: Dp[i][0] represents the maximum pleasure value that the first person does not go to, and the maximum pleasure value that can be obtained by a subtree of root I
DP[I][1] On behalf of the first person I went to the maximum pleasure value that can be obtained, the maximum pleasure value that can be obtained by a subtree of root I
DP[I][1] = dp[i][1] + dp[j][0] J for Children of I
Dp[i][0] = dp[i][0] + max (dp[j][0],dp[j][1]) Since this is a one-way edge, DFS is found after locating the root node, then Max (Dp[root][0],dp[root][1]) is selected
///Analysis: dp[i][0] represents the maximum pleasure value that the first person does not go to, and the maximum pleasure value that can be obtained by a subtree of root I///Dp[i][1] On behalf of the first person I went to the maximum pleasure value that can be obtained, the maximum pleasure value that can be obtained by a subtree of root I///dp[i][1] = dp[i][1] + dp[j][0] J for children of I///dp[i][0] = dp[i][0] + max (dp[j][0],dp[j][1])#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#defineN 6005using namespacestd;intdp[n][2];intIndgree[n];intIsroot[n];structedge{intU,v,next;} Edge[n];intHead[n];voidAddedge (intUintVint&k) {edge[k].u= U,EDGE[K].V =v; Edge[k].next= head[u],head[u]=k++;}voidDfsintu) { for(intK = head[u];k!=-1; k=Edge[k].next) { intv =edge[k].v; DFS (v); dp[u][0]+=max (dp[v][0],dp[v][1]); dp[u][1]+=dp[v][0]; }}intMain () {intN; while(SCANF ("%d", &n)! =EOF) {memset (head,-1,sizeof(head)); memset (Indgree,0,sizeof(Indgree)); memset (IsRoot,0,sizeof(IsRoot)); for(intI=1; i<=n;i++) {scanf ("%d", &dp[i][1]); } inttot=0; intu,v; while(SCANF ("%d%d", &v,&u), u+v) {Addedge (U,v,tot); Isroot[u]=1; INDGREE[V]++; } intRoot; for(intI=1; i<=n;i++){ if(indgree[i]==0) root =i; } dfs (root); //printf ("%d\n", root);printf"%d\n", Max (dp[root][0],dp[root][1])); } return 0;}
POJ 2342 (tree-shaped DP)