Anniversary partyTime
limit:MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64u SubmitStatus
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 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
Output
Output should contain the maximal sum of guests ' ratings.
Sample Input
Sample Output
A company to go to the party, asked to go to the person can not have direct leadership relationship, give each person's happiness value, and L K for K is the direct leader of L, ask the maximum joy value is how much.
Build the company's relationship as a tree, from the biggest boss down Dfs
Dp[i][0] represents the maximum joy value of a subtrees tree (without i) that is the root of the employee numbered I.
Dp[i][1] represents the maximum joy value of a subtrees tree (containing i) with the employee numbered I as the root.
Then the state transfer equation is assumed to be subordinate to J I
Dp[i][0] = max (dp[j][0],dp[j][1]);
DP[I][1] = max (dp[j][0]) + c[i];
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; struct node{int v; int next;} Edge[10000];int head[10000], cnt; int c[10000]; int dp[10000][2]; void Add (int l,int k) {edge[cnt].v = l; Edge[cnt].next = Head[k]; Head[k] = cnt++;} void Dfs (int u) {if (head[u] = =-1) {dp[u][0] = 0; DP[U][1] = C[u]; return; } int I, V; for (i = Head[u], dp[u][1] = C[u]; I! = 1; i = edge[i].next) {v = edge[i].v; DFS (v); Dp[u][0] + = max (dp[v][0],dp[v][1]); DP[U][1] + = dp[v][0]; } return; int main () {int n, I, J, L, K, Num; while (scanf ("%d", &n)! = EOF) {memset (dp,0,sizeof (DP)); memset (head,-1,sizeof (head)); CNT = num = 0; for (i = 1; I <= n; i++) {scanf ("%d", &c[i]); num + = i; } while (scanf ("%d%d", &l, &k) && (l+k! = 0)) { num-= l; Add (l,k); } dfs (num); printf ("%d\n", Max (dp[num][0],dp[num][1])); } return 0;}
poj2342--hdu1520--Anniversary Party (tree DP Exercise 1)