143. Long Live the Queen 树形dp 难度:0
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的更多相关文章
- Uva LA 3902 - Network 树形DP 难度: 0
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- UVa 10859 - Placing Lampposts 树形DP 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- POJ 1947 Rebuilding Roads 树形dp 难度:2
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9105 Accepted: 4122 ...
- HDU 4035 Maze 概率dp,树形dp 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=4035 求步数期望,设E[i]为在编号为i的节点时还需要走的步数,father为dfs树中该节点的父节点,son为 ...
- POJ 2057 The Lost Home 树形dp 难度:2
The Lost House Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 2203 Accepted: 906 Des ...
- ZOJ 3822 Domination 概率dp 难度:0
Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge Edward is the headm ...
- 快速切题 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 ...
- 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 ...
- URAL 1203 Scientific Conference 简单dp 难度:0
http://acm.timus.ru/problem.aspx?space=1&num=1203 按照结束时间为主,开始时间为辅排序,那么对于任意结束时间t,在此之前结束的任务都已经被处理, ...
随机推荐
- SpringCloud 进阶之分布式配置中心(SpringCloud Config)
1. SpringCloud Config SpringCLoud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用 的所有环境提供了一个中心化的外部配置; ...
- DOM2和DOM3读书笔记
二刷<高程>做的笔记,没什么技术含量就不发到首页啦!~DOM1级主要定义HTML和XML文档底层结构,DOM2和DOM3在这个结构基础上引入更多交互能力,也支持更高级的XML特性.DOM2 ...
- 5.3 Components — Passing Properties to A Component
1. 默认情况下,一个组件在它使用的模板范围中没有访问属性. 例如,假想你有一个blog-post组件被用来展示一个blog post: app/templates/components/blog-p ...
- Java StringBuffer 和 StringBuilder 类
当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类. 和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够 ...
- python 中读取yaml
YAML 是专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便. YAML在python语言中有PyYAML安装包. YAML 语言(发音 /ˈjæməl/ )的设计目标,就是方便人类 ...
- unhandledException详细介绍
http://www.cnblogs.com/eaglet/archive/2009/02/17/1392191.html 1. GC 产生的异常,这种异常通常因为Finalize函数中引发未捕获异常 ...
- 20145339顿珠达杰 《网络对抗技术》 逆向与Bof基础
目的 通过一些方法,使能够运行本不该被运行的代码部分,或得到shell的使用: 将正常运行代码部分某处call后的目标地址,修改为另一部分我们希望执行.却本不应该执行的代码部分首地址(这需要我们有一定 ...
- 2017-2018-1 JaWorld 第八周作业
2017-2018-1 JaWorld 第八周作业 团队分工 成员 分工 陈是奇 统计成员工具选择 马平川 类图 王译潇 编码规范 李昱兴 用例图 林臻 状态图 张师瑜 推进工作进展.写博客 UML ...
- TCP状态迁移
TCP的状态变迁图 CLOSED:表示初始状态.对服务端和C客户端双方都一样. LISTEN:表示监听状态.服务端调用了listen函数,可以开始accept连接了. SYN_SENT:表示客户端已经 ...
- c++ 将容量设置为容器的长度(shrink_to_fit)
#include <iostream> #include <vector> using namespace std; int main () { vector<); co ...