HDU1520 & POJ 2342 Anniversary Party (tree DP)

Source: Internet
Author: User


Anniversary partyTime limit : MS Memory Limit:65536KB 64bit IO Format: C7>%I64D &%i64u



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


Main topic:

The topic is probably said that a company to hold a party, let everyone participate in the words will increase a certain degree of happiness, in order to rule out embarrassing scenes, everyone and his direct leadership can not participate at the same time, give you a n, stating that there are N people, and then N line to let everyone go to the increased level of happiness, Then each line entered two said L and K, indicating that K is the direct boss of L, until the input is 0 0 o'clock the input end of the group, the output can reach the maximum joy value.


Thinking Analysis:

Everyone can participate with his immediate supervisor to participate in the non-participation, if his boss to participate, then he can not participate, if his boss did not participate, then he may participate or not to participate, we now use dp[i][0] show I do not participate in the joy Value, dp[i][1] show I participate in the joy value, So we can introduce a state transition equation.

DP[k][1] += DP[L][0];DP[k][0] += Max(DP[L][0],DP[L][1]);


Therefore, we can use this state to transfer the equation solution.


This problem appeared a very silent situation, that is, in HDU OJ with C + + compiler can be over, and with the g++ compiler timeout, so when you commit the point of attention

Attach some of the differences between C + + and g++ I found link: http://blog.csdn.net/xia842655187/article/details/51329012 Click to open link


Attached code:

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <                           String> #define INF 0x3f3f3f3fusing namespace Std;int dp[6500][2];int visit[6500],n,father[6500];void dfs (int root)  Use deep Search from the root to search down, search to the last layer thick, slowly on-line recursive transfer {Visit[root] = 1;for (int i = 1;i <= n;i++) {if (father[i] = = Root                 &&!visit[i])//root is the boss of I, then search for I as a new root {dfs (i);DP [root][1] + = dp[i][0];  When Root goes, I definitely can't go, so add dp[i][0]dp[root][0] + + MAX (dp[i][0],dp[i][1]); When Root does not go from I go and not go to choose a maximum value}}}int main () {while (CIN >> N) {memset (dp,0,sizeof (DP)), for (int i = 1;i <= n;i++) {SCA NF ("%d", &dp[i][1]);}          Fill (father,father + 6500,0); Initialize each person's immediate boss to 0 int L, k,root = 0;while (cin >> l >> k && L + k)//mark the immediate boss of each person {father [l] = K;root = k;} while (1)//judgment query to the root of the tree {if (father[root] = = 0) Break;root = Father[root];} memset (visit,0,sizeof (visit));d FS (root);        Query from Root cout << max (dp[root][0],dp[root][1]) << Endl; From the big boss to go with not to choose a happy value the maximum output}return 0;}


HDU1520 & POJ 2342 Anniversary Party (tree DP)

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.