POJ Anniversary Party (tree-shaped DP)

Source: Internet
Author: User

Anniversary party
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4825 Accepted: 2732

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

Idea: Find the root node, start searching down, recursively from the leaf node, and record the child node information.

For the current node there are two options, select or not selected;

Dp[u][0]+=max (Dp[v][1],dp[v][0]); If the current node is not selected, the child node is optional
DP[U][1]+=DP[V][0]; Select the current node, the child node must not be selected


#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include < Queue> #include <vector>using namespace std; #define N 6005int Dp[n][2];int pre[n],vis[n];vector<int>g[n    ];void dfs (int u) {int i,v;    Vis[u]=1;        for (i=0;i< (int) g[u].size (); i++) {v=g[u][i];        if (!vis[v]) Dfs (v);        Dp[u][0]+=max (Dp[v][1],dp[v][0]);        DP[U][1]+=DP[V][0];        printf ("%d:%d%d%d\n", u,dp[u][0],dp[v][1],dp[v][0]);    printf ("%d%d\n", dp[u][1],dp[v][0]);    }}int Main () {int i,n,u,v,root;    scanf ("%d", &n);        for (i=1;i<=n;i++) {scanf ("%d", &dp[i][1]);        dp[i][0]=0;        Pre[i]=i;    G[i].clear (); } while (scanf ("%d%d", &u,&v), u| |        V) {pre[u]=v;        Root=v;    G[v].push_back (U);          } root=1;     Be careful with only one person's case while (pre[root]!=root)//Find root node root=pre[root];   memset (vis,0,sizeof (VIS));    Log the node that has been accessed Dfs (root); printf ("%d\ n ", Max (dp[root][1],dp[root][0)); return 0;}


POJ Anniversary Party (tree-shaped 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.