POJ-2342 Anniversary party (树形DP)

Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10417   Accepted: 5957

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

题意:给你一颗N个节点的树,每个节点都有权值,当选择一个节点时,其直接的父亲节点不能在选择,现在问你能选择的节点最大和是多少。

思路:树形dp入门题,我们定义dp[i][0]为第i个节点不选时的最优解,dp[i][1]为选择时的最优解。

根据题目限制,可以很容易得到状态方程:设i为父节点,j为子节点

dp[i][0]+=max(dp[j][0],dp[j][1])

dp[i][1]+=dp[j][0];

我们可以用vector保存树,同时开一个数组保存父亲节点,便于我们找到这棵树的根root,然后从根开始dfs这颗树即可更新出最优解。最后dp[root][0]与dp[root][1]取最大输出即可。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<sstream>
#define ll long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
using namespace std;
vector<ll>v[6060];
ll n,dp[6060][10],f[6060],root;
void dfs(ll root)
{
    for(ll i=0;i<v[root].size();i++)
    {
        ll nex=v[root][i];
        dfs(nex);         //走到叶子节点为止
    }
    for(ll i=0;i<v[root].size();i++)
    {
        ll nex=v[root][i];         
        dp[root][0]+=max(dp[nex][0],dp[nex][1]);    //回溯时开始更新当前父亲节点的最优解
        dp[root][1]+=dp[nex][0];
    }
    return ;
}
int main()
{
    ll x,y;
    while(~scanf("%lld",&n))
    {
        if(n==0)
        memset(dp,0,sizeof(dp));
        for(ll i=1;i<=n;i++)scanf("%lld",&dp[i][1]);
        for(ll i=0;i<=n;i++)v[i].clear();
        for(ll i=1;i<=n;i++)f[i]=i;
        while(1)
        {
            scanf("%lld%lld",&x,&y);
            v[y].push_back(x);     //用vector存树
            f[x]=y;      //保存i的父亲节点
            if(x==0&&y==0)break;
        }
        root=1;
        while(f[root]!=root)root=f[root];   //找到这颗树的根节点
        dfs(root);
        ll ans=max(dp[root][0],dp[root][1]);
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/s540239976/article/details/80067137