poj2342 Anniversary party__ Tree DP

Source: Internet
Author: User

Language:default Anniversary Party
Time Limit: 1000MS Memory Limit: 65536K
Total submissions: 6480 accepted: 3734

Description There is going to being a party to celebrate the 80-th anniversary of the Ural state University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a, rooted at the Rector v. Tretyakov. In order to make the party funny for every one, the rector does not want both a employee and his or her immediate Sor to be present. The personnel office has evaluated conviviality of each employee, so everyone has 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 are numbered from 1 to N. A a number n. 1 <= n <= 6 000 contains. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is the integer number in a range from-128 to 127. After this go n–1 lines the describe a supervisor relation tree. Of the tree specification has the form:
L K
It means that the k-th employee are an immediate supervisor of the L-th employee. The Input is ended with the line
0 0

Output output should contain the maximal sum of guests ' ratings.

Sample Input

7
1
1
1
1
1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0-0

Sample Output

5
In order to make the scene as active as possible, the headmaster considers that an employee and his or her direct boss cannot participate at the same time, and each employee's activity and relationship are known (input). Find the maximum degree of activity. (This relationship is obviously a tree, so called a tree dp.) It should be. )

Analysis:

Each employee has two kinds of decisions, to come or not (with 1 and 0 respectively), the dp[i][1] (or dp[i][0]) indicates that a subtree with I root can have the largest active value, of which dp[i][1] represents the case of I, dp[i][0 the case that I do not come. is the following recursive relationship:

Dp[i][1]+=dp[j][0];//i is J's boss, I come, J does not come.

Dp[i][0]+=max (Dp[j][1],dp[j][0])//i is J's boss, I do not come, J come or not.

Remember that the root node of the whole tree is root, and that is Max (Dp[root][1],dp[root][0]).

The AC code is as follows:

#include <iostream> #include <cstring> #include <cstdio> using namespace std;
#define MAXN 6005 #define MAX (a,b) (a>b) a:b int n;
int dp[maxn][2];
int FATHER[MAXN];
int VISITED[MAXN];
    void solve (int node) {visited[node]=1;
            for (int i=1;i<=n;i++) {if (!visited[i]&&father[i]==node) {//i is subordinate to node solve (i);
            dp[node][1]+=dp[i][0];//boss to go, subordinates do not go.
    Dp[node][0]+=max (dp[i][1],dp[i][0]);/The boss does not go, subordinates can go or not go}} int main () {scanf ("%d", &n);
    Memset (Dp,0,sizeof (DP));
    memset (visited,0,sizeof (visited));
    memset (father,0,sizeof (father));
    for (int i=1;i<=n;i++) scanf ("%d", &dp[i][1]);
    int c,f,root; while (scanf ("%d%d", &c,&f) && (c| |
        f)) {father[c]=f; if (root==c| |
        true) {root=f;
    } while (Father[root]) {//Find root node root=father[root];
    } solve (root);
    printf ("%d\n", Max (dp[root][1],dp[root][0));
return 0; }

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.