HDU 1250 tree DP

Source: Internet
Author: User
Anniversary Party Time limit:1000 ms Memory limit:32768kb 64bit Io format:% I64d & % i64usubmit statusappoint description: System crawler)

Description

There 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 that 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 an employee and his or her immediate supervisor 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 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 that 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 is an immediate supervisor of the L-th employee. input is ended with the line
0 0

Output

Output shoshould contain the maximal sum of guests ratings.

Sample Input

711111111 32 36 47 44 53 50 0

Sample output

5

Input:

Enter n knots and the next n rows, indicating the active values of each node of 1-n. In the next n-1 line, enter a and B, indicating that B is the boss of.

Output:

Because two people with direct relationship between superiors and subordinates cannot participate in the party at the same time, find the solution that maximizes the activity value of the Party (find the maximum activity value ).

#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn=6005;vector<int> v[maxn];int value[maxn],in[maxn],dp[maxn][2];inline int max(int a,int b){return a>b?a:b;}void dfs(int id){    dp[id][0]=0;dp[id][1]=value[id];    for(int i=0;i<v[id].size();i++)    {        int u=v[id][i];        dfs(u);        dp[id][0]+=max(dp[u][0],dp[u][1]);        dp[id][1]+=dp[u][0];    }}int main(){    int n,a,b,i,ans;    while(~scanf("%d",&n))    {        for(i=1;i<=n;i++) v[i].clear();        for(i=1;i<=n;i++) scanf("%d",&value[i]);        memset(in,0,sizeof(in));        while(scanf("%d%d",&a,&b),a+b)        {            v[b].push_back(a);in[a]++;        }        memset(dp,0,sizeof(dp));        ans=0;        for(i=1;i<=n;i++)            if(!in[i])            {                dfs(i);                ans+=max(dp[i][0],dp[i][1]);            }        printf("%d\n",ans);    }    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.