POJ 2342 Anniversary Party (tree DP entry)

Source: Internet
Author: User


Anniversary Party
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4810 Accepted: 2724

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

Source

Ural State University Internal Contest October ' Students Session

Title Link: http://poj.org/problem?id=2342

The main idea: a tree, each node has a value, now to select some points, require these points and maximum and each pair of sons and fathers can not be selected at the same time

Title analysis: dp[i][0] and dp[i][1] respectively indicate that when I is not selected and the first point of the sub-tree value when I is subtree, then:
DP[FA[I]][1] + = Dp[i][0] Indicates the father of the selected I, whose value equals the value of the self value plus the not selected I
Dp[fa[i]][0] + = max (dp[i][1], dp[i][0]) indicates that when the father is not selected, its value is equal to the maximum value of the selected or not selected value of his son

#include <cstdio> #include <cstring> #include <vector> #include <algorithm>using namespace std; int const MAX = 6005;int dp[max][2], Val[max];bool Vis[max], flag[max];vector <int> vt[max];void DFS (int fa) {Vis    [FA] = true;    DP[FA][1] = Val[fa];    int sz = Vt[fa].size ();        for (int i = 0; i < sz; i++) {int son = vt[fa][i];            if (!vis[son]) {DFS (son);            DP[FA][1] + = dp[son][0];        Dp[fa][0] + = max (dp[son][1], dp[son][0]); }} return;    int main () {int n;        while (scanf ("%d", &n) && N) {for (int i = 1; I <= n; i++) vt[i].clear ();        Memset (flag, false, sizeof (flag));        Memset (Vis, false, sizeof (VIS));        memset (DP, 0, sizeof (DP));        for (int i = 1; I <= n; i++) scanf ("%d", &val[i]);            for (int i = 0; i < n-1; i++) {int A, B;            scanf ("%d%d", &a, &b); Vt[b].push_back (a);        Flag[a] = true;        } int root; for (int i = 1; I <= n; i++)//n Point n-1, there must be a point of "" of 0, which is the root {if (!flag[i]) {Roo                t = i;            Break        }} DFS (root);    printf ("%d\n", Max (Dp[root][1], dp[root][0])); }}


POJ 2342 Anniversary Party (tree DP entry)

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.