143. Long Live the Queen

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

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

题意:求一棵收益最大的树/子树,不能为空
思路:分别对每个节点维护以该节点为根所能得到的最大收益,更新答案即可
转移方程dp[i]=sum(dp[son[i]]>0?dp[son[i]]:0)
#include <cstdio>
#include <cstring>
#include<algorithm>
using namespace std;
const int maxn=16001;
const int maxm=32001;
int first[maxn],next[maxm],to[maxm],profit[maxn],len,n;
int sum[maxn];
void addedge(int f,int t){
next[len]=first[f];
first[f]=len;
to[len]=t;
swap(f,t);len++;
next[len]=first[f];
first[f]=len;
to[len]=t;
len++;
}
int dfs(int s,int f){
sum[s]=profit[s];
for(int p=first[s];p!=-1;p=next[p]){
int t=to[p];
if(t==f)continue;
int son=dfs(t,s);
if(son>0)sum[s]+=son;
}
return sum[s];
}
int main(){
scanf("%d",&n);
int tf,tt;
memset(first,-1,sizeof(first));
for(int i=1;i<=n;i++)scanf("%d",profit+i);
for(int i=1;i<n;i++){scanf("%d%d",&tf,&tt);addedge(tf,tt);}
dfs(1,-1);
int maxn=-0x7ffffff;
for(int i=1;i<=n;i++){maxn=max(maxn,sum[i]);}
printf("%d\n",maxn);
}

  

143. Long Live the Queen 树形dp 难度:0的更多相关文章

  1. Uva LA 3902 - Network 树形DP 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. UVa 10859 - Placing Lampposts 树形DP 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  3. POJ 1947 Rebuilding Roads 树形dp 难度:2

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9105   Accepted: 4122 ...

  4. HDU 4035 Maze 概率dp,树形dp 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=4035 求步数期望,设E[i]为在编号为i的节点时还需要走的步数,father为dfs树中该节点的父节点,son为 ...

  5. POJ 2057 The Lost Home 树形dp 难度:2

    The Lost House Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 2203   Accepted: 906 Des ...

  6. ZOJ 3822 Domination 概率dp 难度:0

    Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headm ...

  7. 快速切题 sgu104. Little shop of flowers DP 难度:0

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB PROBLEM Yo ...

  8. CF 148D Bag of mice 概率dp 难度:0

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  9. URAL 1203 Scientific Conference 简单dp 难度:0

    http://acm.timus.ru/problem.aspx?space=1&num=1203 按照结束时间为主,开始时间为辅排序,那么对于任意结束时间t,在此之前结束的任务都已经被处理, ...

随机推荐

  1. Django - 模型层 - 上

    一.ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...

  2. 云备份厂商Rubrik再获2.61亿美元融资,估值高达33亿美元 转自中国存储网

    数据管理初创公司Rubrik在Bain Capital Ventures领导的最新一轮融资中筹集了2.61亿美元,估值为33亿美元. 现有的利益相关者 - Lightspeed Venture Par ...

  3. PAT 1110 Complete Binary Tree[判断完全二叉树]

    1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...

  4. #C++初学记录(初识汉诺塔)

    汉诺塔 题目 用1,2,...,n表示n个盘子,称为1号盘,2号盘,....号数大盘子就大.经典的汉诺塔问 题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于 印度传说的 ...

  5. fedora修改主目录文件名为英文

           在gnome-terminal里面能够显示中文的,但是在文本终端里面中文都是不能显示的,是一个小小的正方形.因此可以只把主目录文件名换成英文的,方便文本终端命令输入,而其他的一些地方还是 ...

  6. Bootstrap抽样(自展法)

    Bootstrap又称自展法,是用小样本估计总体值的一种非参数方法,在进化和生态学研究中应用十分广泛.例如进化树分化节点的自展支持率等. Bootstrap的思想,是生成一系列bootstrap伪样本 ...

  7. windows 系统相关配置

    1. 外接显示器分辨率调节:连接上外接下显示器,在本机空白处,右键,分辨率.然后选择显示器,设置显示相关配置. 详见:http://zhidao.baidu.com/question/13494806 ...

  8. Gulp和Webpack对比

    在现在的前端开发中,前后端分离.模块化开发.版本控制.文件合并与压缩.mock数据等等一些原本后端的思想开始逐渐渗透到“大前端”的开发中.前端开发过程越来越繁琐,当今越来越多的网站已经从网页模式进化到 ...

  9. PHP设计模式_单例模式

    了解 单例设计模式用于限制特定对象只能被实例化创建一次,有且只有一个此类型的资源.例如,通过数据库句柄到数据库的连接是独占的.您希望在应用程序中共享数据库句柄,因为在保持连接打开或关闭时,它是一种开销 ...

  10. bzoj1627 / P2873 [USACO07DEC]泥水坑Mud Puddles

    P2873 [USACO07DEC]泥水坑Mud Puddles bfs入门. 对于坐标为负的情况,我们可以给数组下标加上$abs(min(minx,miny))$转正(根据题意判断) #includ ...