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
Test instructions
n Individuals (numbered 1-n), then the N row table represents the activity of the nth person, followed by several lines L and K (0 0 end), representing K being the boss of L,
Ask a party to invite a few of these n individuals, which cannot contain direct subordinate relationships, which can make the maximum amount of activity in a party.
State transitions:
The boss goes, subordinates do not go:
DP[C][1]+=DP[T][0]; C go, then T will not go
The boss does not go, subordinates go or not go:
Dp[c][0]+=max (dp[t][0],dp[t][1]); c Do not go, take t to go or not to go to the maximum value
And deep Search is very similar, that is, with the deep search.
#include <stdio.h> #include <string.h> #include <vector> #include <algorithm>using namespace Std;vector<int>g[10001];int dp[10001][3],root[10001];void dfs (int c) {int i,t; for (i=0;i< (int) g[c].size (); i++) {t=g[c][i];//node for child DFS (t);//child who searches for its child as a node dp[c][1]+=dp[t][0]; If the parents go, the child can not go to Dp[c][0]+=max (dp[t][0],dp[t][1]);//If the parents do not go, choose the children of joy value to}}int main () {int n,i,a,b; while (scanf ("%d", &n)!=eof) {memset (dp,0,sizeof (DP)); for (i=1;i<=n;i++) {scanf ("%d", &dp[i][1]),//input I personal activity root[i]=i;//Initialize each node is the root node, is also a check set Initialize G[i].clear ();//Empty Container} while (scanf ("%d%d", &a,&b) &&a+b) {G[b ].push_back (a);//Deposit data in container root[a]=b; The root node of A is b} int beg; for (i=1;i<=n;i++) if (root[i]==i)//If the root node is itself, the description is the root node of the tree {beg=i; Break } dfs (beg);//Search This treeprintf ("%d\n", Max (dp[beg][0],dp[beg][1]));//Output The final joy value}return 0;}
AYITACM2016 Province third week l-anniversary party (tree-shaped DP)