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. importlib应用 - django

    背景 仿django的中间件的编程思想 用户可通过配置,选择是否启用某个组件/某个功能,只需要配置 eg:报警系统,发邮件,发微信 ... ( 根据字符串导入模块, 利用反射找到模块下的类,实例化.执 ...

  2. inotify+rsync安装配置

    环境 系统 IP地址 主服务器 CentOS7.4 192.168.1.1 备份服务器 CentOS7.4 192.168.1.2 一.备份服务器 安装rsync(备) wget https://rs ...

  3. Python开发【模块】:Concurrent

    concurrent 模块 回顾: 对于python来说,作为解释型语言,Python的解释器必须做到既安全又高效.我们都知道多线程编程会遇到的问题,解释器要留意的是避免在不同的线程操作内部共享的数据 ...

  4. 制造抽象基类--《C++必知必会》 条款33

    抽象类,含有纯虚函数的类,不可以创建对象. 然而,有时我们并不需要某个函数定义为纯虚函数,但是任然希望此类像抽象类一样,不可以创建对象. 方法1:通过确保类中不存在共有构造函数来模拟抽象基类的性质.意 ...

  5. windows 系统相关配置

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

  6. update t_conference set c_address = replace(c_address,'中华人民共和国','');

    update t_conference set c_address = replace(c_address,'中华人民共和国','中国');      //把c_address字段中的‘中华人民共和国 ...

  7. pyDay9

    内容来自廖雪峰的官方网站. generator 1.引入generator的原因. 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素 ...

  8. bzoj1607 / P2926 [USACO08DEC]拍头Patting Heads

    P2926 [USACO08DEC]拍头Patting Heads 把求约数转化为求倍数. 累计每个数出现的个数,然后枚举倍数累加答案. #include<iostream> #inclu ...

  9. ansible之template模块

    趁着最近在搞ansible,现在学习了一波template模块的用法: 1.使用template模块在jinja2中引用变量,先来目录结构树 [root@master ansible]# tree . ...

  10. Git简介【转】

    本文转载自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 Git简介 Git是什 ...