Long Live the Queen_树形dp

原创 2018年04月15日 10:38:37

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
//#include<bits/stdc++.h>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;

const int N=16000+10;
int p[N];
int d[N];
bool vis[N];
vector<int> v[N];


void dfs(int u){
    d[u]=p[u];
    vis[u]=true;
    for(int i=0;i<v[u].size();i++)
    if(!vis[v[u][i]]){
        dfs(v[u][i]);
        d[u]+=max(0,d[v[u][i]]);
    }
}


int main(){
    freopen("in.txt","r",stdin);
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>p[i];
    for(int j=1;j<n;j++){
        int x,y;
        cin>>x>>y;
        x--,y--;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0);
    int mm=-1000000;
    for(int k=0;k<n;k++)
    if(d[k]>mm)
    mm=d[k];
    cout<<mm<<endl;
    fclose(stdin);
}



 
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ujn20161222/article/details/79947239

iOS8开发视频教程Swift语言版-Part 6:iOS视图控制器与导航模式

视图控制器与导航是iOS开发中比较重要的知识,主要介绍了平铺导航,标签导航,树形导航,组合使用导航几种模式,分别介绍了每种导航适合于什么样的应用中使用,以及如何使用导航具体方法。
  • 2016年05月18日 09:33

【树形dp】Long Live the Queen

题目链接:Long Live the Queen  题意是给一棵树,每个结点都有一个值,从这棵树中选出一颗子树来,使得该子树的所有结点的值得和最大。 容易想到解决思路:定义dp[i]表示以...
  • bit_Line
  • bit_Line
  • 2015-01-29 20:17:43
  • 589

树形DP整理小结

树形DP: 在树上进行dp,树是递归的定义的,所以树形dp也是递归的求解,一般而言,dp[node]表示的是以node为根的子树能得到的最优解 一般而言,dp[node]需要从node的子结点进行状态...
  • tomorrowtodie
  • tomorrowtodie
  • 2016-08-24 15:38:21
  • 1132

树形动态规划(树形DP)入门问题—初探 & 训练

树形DP入门 poj 2342 Anniversary party   先来个题入门一下~ 题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的...
  • txl16211
  • txl16211
  • 2015-04-29 22:40:03
  • 11038

树形DP例题整理

与普通dp类似,但是状态遍历不能简单的++i,而应该按由儿子到父亲的顺序完成。可以通过DFS递归完成,也可以按DFS(BFS)序从大到小完成,以保证先完成儿子再完成父亲。例题一 皇宫看守 tyvj_...
  • Mr_Poisonous
  • Mr_Poisonous
  • 2017-08-17 17:40:56
  • 142

树形 DP 总结

一、介绍 1、什么是树型动态规划  顾名思义,树型动态规划就是在“树”的数据结构上的动态规划,平时作的动态规划都是线性的或者是建立在图上的,线性的动态规划有二种方向既向前和向后,相应的线性的动态规划有...
  • AngOn823
  • AngOn823
  • 2016-08-27 14:12:16
  • 2107

bsoj 1660 选课(树形DP)

【模拟试题】选课   Time Limit:10000MS  Memory Limit:65536K Total Submit:365 Accepted:166  Case T...
  • fp_hzq
  • fp_hzq
  • 2012-09-26 14:02:35
  • 8628

树形dp 小结

只能勉强称之为树形dp的傻逼问题:[POJ1655]Balancing Act 树的重心,经典问题,但是非常简单啊。。。 用size维护一下就好辣! [BZOJ2435][Noi2011]道路修...
  • Clove_unique
  • Clove_unique
  • 2016-11-17 22:32:40
  • 744

HDU 2196 Computer 树形DP经典题

这是一道树形DP的经典题目,把树的性质发挥的很清晰。
  • u011385365
  • u011385365
  • 2014-09-01 16:14:25
  • 1068

树形DP总结,持续更新

树形DP总结,持续更新
  • Dacc123
  • Dacc123
  • 2016-01-03 18:09:58
  • 439
收藏助手
不良信息举报
您举报文章:Long Live the Queen_树形dp
举报原因:
原因补充:

(最多只允许输入30个字)