SGU 143. Long Live the Queen(树形DP)

The Queen of Byteland is very loved by her people. In order to show her their love, the Bytelanders have decided to conquer a new country which will be named according to the queen's name. This new country contains N towns. The towns are connected by bidirectional roads and there is exactly ONE path between any two towns, walking on the country's roads. For each town, the profit it brings to the owner is known. Although the Bytelanders love their queen very much, they don't want to conquer all the N towns for her. They will be satisfied with a non-empty subset of these towns, with the following 2 properties: there exists a path from every town in the subset to every other town in the subset walking only through towns in the subset and the profit of the subset is maximum. The profit of a subset of the N towns is equal to the sum of the profits of the towns which belong to the subset. Your task is to find the maximum profit the Bytelanders may get.

Input

The first line of input will contain the number of towns N (1<=N<=16 000). The second line will contain N integers: the profits for each town, from 1 to N. Each profit is an integer number between -1000 and 1000. The next N-1 lines describe the roads: each line contains 2 integer numbers a and b, separated by blanks, denoting two different towns between which there exists a road.

Output

The output should contain one integer number: the maximum profit the Bytelanders may get.

Sample Input

5
-1 1 3 1 -1
4 1
1 3
1 2
4 5

Sample Output

4

题意:给你一棵树,树上每一个点都有一个权值,有正有负,让你从n个点中选出若干个联通的点(选出的这些点必须在原图中都联通),使得总权值最大。


思路:树形DP,dp[i][0]表示选到节点i,且节点i这个节点不选,dp[i][1]表示节点i这个点选。

转移:如果节点i选的话,那么和他相连的点可选可不选,那么我们就把正的都加上,如果这个节点不选的话,那么我们只能从和他相连的节点中选一个最大的。


有一个坑点,就是至少要选一个点,如果所有点的权值都是负数,那么我们就选一个最大的。


#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;

const int inf = 1e8;

int a[16010];
int dp[16010][2];
vector<int> e[16010];

void dfs(int x,int pre)
{
    dp[x][1] = a[x];
    dp[x][0] = 0;
    for(int i=0;i<e[x].size();i++)
    {
        int xx = e[x][i];
        if(xx == pre)
            continue;
        dfs(xx,x);
        dp[x][1] = max(dp[x][1],dp[x][1] + dp[xx][1]);
        dp[x][0] = max(dp[x][0],max(dp[xx][1],dp[xx][0]));
    }
}
int main(void)
{
    int n,i,j;
    scanf("%d",&n);
    int flag = 0;
    int maxn = -inf;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        maxn = max(maxn,a[i]);
        if(a[i] >= 0)
            flag = 1;
    }
    for(i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        e[x].push_back(y);
        e[y].push_back(x);
    }
    if(flag == 0)
    {
        printf("%d\n",maxn);
    }
    else
    {
        dfs(1,-1);
        printf("%d\n",max(dp[1][0],dp[1][1]));
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值