前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >年会Party(树形动态规划)- HDU 1520

年会Party(树形动态规划)- HDU 1520

作者头像
ACM算法日常
发布2018-08-07 17:01:51
5610
发布2018-08-07 17:01:51
举报
文章被收录于专栏:ACM算法日常ACM算法日常

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

题意:

在一棵有根树上每个节点有一个权值,相邻的父亲和孩子只能选择一个, 求总权值之和最大.

思路:

dp[i][0]表示第i个人参加,dp[i][1]表示第i个人不参加.

状态转移方程:

dp[root][0]+=max(dp[son][1],dp[son][0]); 当前这个点不选,他的孩子可选、可不选,取最大值. dp[root][1]+=dp[son][0];

当前这个点选择,它的孩子就不能选择 .

源代码:G++

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;

const int maxn = 6005;
int father[maxn];//标记根节点
int n, a[maxn]; //每个节点的权值
vector<int>G[maxn];//存放子节点
int dp[maxn][2];

void dfs(int root)
{
    dp[root][1] = a[root];
    for (int i = 0; i < G[root].size(); i++) {
        int son = G[root][i];
        dfs(son);
        dp[root][0] += max(dp[son][1], dp[son][0]);
        //当前这个人不选,取选孩子和不选孩子的较大者
        dp[root][1] += dp[son][0];
        //当前这个人选,不选他的孩子
    }
}

int main()
{
    ios::sync_with_stdio(0);
    int n;
    while (cin >> n) {
        //memset(father,-1,sizeof(father));
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            G[i].clear();//清空
            dp[i][0] = dp[i][1] = 0;
            father[i] = -1;
        }

        int l, k;
        while (cin >> l >> k, l + k) { //l是k的孩子
            father[l] = k; //l的祖先为k
            G[k].push_back(l);
        }
        int root = 1;
        while (father[root] != -1) //寻找根节点
            root = father[root];
        dfs(root);
        cout << max(dp[root][1], dp[root][0]) << endl;
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-05-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ACM算法日常 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档