[ACM] poj 2342 anniversary party (getting started with tree DP)

Source: Internet
Author: User

Anniversary Party
Time limit:1000 ms   Memory limit:65536 K
Total submissions:4410   Accepted:2496

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 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 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

Source

Ural State University internal Contest October '2000 students session


Solution:

There are n people who want to open a party, numbered 1 to n. Each person has a happy value, and each person has a direct boss. In order to make the atmosphere better, it is required to select some of the N people to participate in the Party, and none of the two people selected to have direct superiors or direct subordinates.

The n members constitute a tree through direct superiors or direct subordinate relationships. If the parent node is regarded as a direct supervisor, the child node is a subordinate node, and the superiors and subordinates cannot choose at the same time.

Define DP [I] [0] as the maximum value of joy that I do not select, and DP [I] [1] as the maximum value of joy that I have selected.

Assume J is a subordinate of I, then there is a transfer equation:

DP [I] [0] + = max (DP [J] [0], DP [J] [1]); note that it is + =, because a parent node has multiple child nodes

DP [I] [1] + = DP [J] [0];

Use DFS to traverse the tree. Each vertex is accessed only once.

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <vector>using namespace std;const int maxn=6005;vector<int>son[maxn];bool vis[maxn];int dp[maxn][2];int n;void dfs(int root){    vis[root]=1;    for(int i=0;i<son[root].size();i++)    {        int v=son[root][i];        if(!vis[v])        {            dfs(v);            dp[root][1]+=dp[v][0];            dp[root][0]+=max(dp[v][0],dp[v][1]);        }    }}int main(){    while(scanf("%d",&n)!=EOF)    {        for(int i=0;i<=n;i++)            son[i].clear();        memset(vis,0,sizeof(vis));        for(int i=1;i<=n;i++)        {            scanf("%d",&dp[i][1]);            dp[i][0]=0;        }        int fa,so;        while(scanf("%d%d",&so,&fa)!=EOF)        {            if(so==0&&fa==0)                break;            son[fa].push_back(so);            son[so].push_back(fa);        }        dfs(1);        cout<<max(dp[1][0],dp[1][1])<<endl;    }    return 0;}


[ACM] poj 2342 anniversary party (getting started with 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.