Anniversary party HDU - 1520(树形dp)

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 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个节点,每个节点对应一个值,对于一个节点u,如果选了他,就不能选他的儿子节点(直系子节点),但可以选他的孙子节点。给出n个节点的关系,求选若干个节点所得和的最大值。、
输入:一个n,接下来n行的值代表每个节点的值,接下来若干行,每行两个值u,v;代表v是u的父节点。(当输入0 0时,输入结束)。

状态:dp[u][0]代表不选节点u时的最大值;dp[u][1]代表选节点u时的最大值。
转移方程:dp[u][1]+=dp[v][0]; dp[u][0]+=max(dp[v][0],dp[v][1]);
(v指节点u的儿子节点,这里不会打求和的符号,只好写“+=”了。)
实现:找到树根然后dfs从后先前推即可。
(在此感谢某“狗”星人的推荐,阿里嘎多。推一下它的博客:https://www.cnblogs.com/1013star/p/9941532.html)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#define LL long long
#define PI acos(-1.0)
using namespace std;
const int maxn=6e3+10;
const int inf=0x3f3f3f3f;
vector <int> ve[maxn];
int in[maxn],arr[maxn];
int dp[maxn][2],book[maxn];
void dfs(int u)
{

    book[u]=1;
    for(int i=0;i<ve[u].size();i++)
    {
        int v=ve[u][i];
        if(!book[v]) //该节点如果搜过了就跳过,没有必要再跑一遍。
        {
            dfs(v);
            dp[u][0]+=max(dp[v][0],dp[v][1]); //如果不选u节点,那么可以选其v,也可以不选v。
            dp[u][1]+=dp[v][0]; //如果选了u,那么他的儿子节点就都不能选了。
        }

    }
    return ;

}
int main(void)
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(in,0,sizeof(in));
        memset(book,0,sizeof(book));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&arr[i]);
            dp[i][0]=0;//不选初值肯定是0.
            dp[i][1]=arr[i];//选了初值就是本节点的值。
            ve[i].clear();//别忘初始化
        }
        int u,v;
        while(~scanf("%d%d",&u,&v))
        {
            if(u==0&&v==0) break;
            ve[v].push_back(u);
            in[u]++;
        }
        int root;
        //找根节点
        for(int i=1;i<=n;i++)
        {
            if(in[i]==0)
            {
                root=i;
                break;
            }

        }
        dfs(root);
        printf("%d\n",max(dp[root][0],dp[root][1]));

    }

	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值