HDU-1520 Anniversary Party

Source: Internet
Author: User

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.

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 Input711111111 0 Sample Output5 Idea: The introduction of the tree-shaped DP, two months ago did once, but the feeling is not too clear, this time to do the basic to understand that the problem is gradually a feeling that the ability to use data structure has been enhanced, that is, every time you want to realize what things feel will not be too difficult, perhaps because of the STL = good, Seriously don't know how to feel the DP and the base of recursion more and more like, but the key of DP, especially the tree-shaped DP seems to be found in the recursive relationship between the parent-child node, that is, the original linear that set of play method to move to the tree, more wonderful for a node, The maximum value that it can return is to choose him and not to choose the biggest one between them, and then all we have to do is to find out the two values to take the largest increase over time, more and more found that the calculation of the word is very correct ———— the core of DP is overlapping sub-problem
 #include <iostream> #include <cstdio> #include <vector># Define Max 6007#define Max (a) > (b)? (a):(B) using namespace std;vector<int> son[max];int f[max];int dp[max][2];int d_max[max];int c[max];int df (int x) {if (d_max[x]! = -207) return d_max[x];else {int len = son[x].size (); for (int i = 0;i < len;i++) {int s = son[x][i];DP [x][ 0] + = DF (s);DP [x][1] + = dp[s][0];} DP[X][1] + = c[x]; return d_max[x] = max (dp[x][0],dp[x][1]);}} int main () {int N;while (cin>>n) {for (int i = 1;i <= n;i++) {cin>>c[i];//cyclic data initialization d_max[i] = -207;f[i] = -1;d P[i][0] = dp[i][1] = 0;son[i].clear ();} int T1,t2;while (scanf ("%d%d", &t1,&t2) && T1 + T2) {son[t2].push_back (T1); f[t1] = T2;} Look for the tree root int head;for (int i = 1;i <= n;i++) if (f[i] = = 1) {head = I;break;} Initialize leaf node for (int i = 1;i <= n;i++) if (Son[i].empty ()) d_max[i] = dp[i][1] = c[i];//output final answer cout<<df (head) << Endl;} return 0;} 

HDU-1520 Anniversary Party

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.