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. spring boot由浅入深(一)jdk安装

    1jdk说明 jdk是java提供了运行环境,简单的说就是给java语言制定了规则,并且提供了工具 jdk官网网站:http://www.oracle.com/technetwork/java/jav ...

  2. Python小功能汇总

    1.没有文件夹就新建 适用以下3种情况. (1)文件夹适用 (2)相对路径适用 (3)绝对路径适用 # 判断输出文件夹是否存在.不存在就创建 # 1.output_dir为绝对路径 if os.pat ...

  3. mysql 表的增删改查 修改表结构

    四.修改表结构 语法: . 修改表名 ALTER TABLE 表名 RENAME 新表名; . 增加字段 ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…], ADD 字段名 ...

  4. centos 系统软件包管理 yum 本地yum配置 扩展源epel rpm 清除yum缓存 yum provides "*/vim" 第十节课

    centos  系统软件包管理  yum 本地yum配置  扩展源epel  rpm  清除yum缓存  yum provides "*/vim"   第十节课 你不能保证可逆化操 ...

  5. 跟我学Makefile(二)

    命令出错: 每当命令运行完后, make 会检测每个命令的返回码,如果命令返回成功,那么 make 会执行下一条命令. 如果一个规则中的某个命令出错了(命令退出码非零),那么 make 就会终止执行当 ...

  6. 散列表Java实现

    package 散列表; import java.util.Scanner; public class HashSearch { public static int data[] = {69,65,9 ...

  7. Python:笔记(7)——yield关键字

    Python:笔记(7)——yield关键字 yield与生成器 所谓生成器是一个函数,它可以生成一个值的序列,以便在迭代中使用.函数使用yield关键字可以定义生成器对象. 一个例子 我们调用该函数 ...

  8. SQL Server2008创建数据库语法

    use mastergo if exists(select * from sys.databases where name='MySchool') drop database MySchool  -- ...

  9. # 20145106 《Java程序设计》第3周学习总结

    教材学习内容总结 在本周的学习中,我看到了这样一句话:"使用java撰写程序几乎都是在使用对象(object),要产生对象必须先定义类(class),类是对象的设计图,对象是类的实例(ins ...

  10. 《Effective Java 2nd》第7章 方法

    目录 第38条 检查参数的有效性 第39条 必要时进行保护性拷贝 第40条 谨慎设计方法签名 第41条 慎用重载 第42条 慎用可变参数 第43条 返回零长度的数组或集合,而不是null 第44条 为 ...