Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

Description

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 2integer 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

Author : Mugurel Ionut Andreica
Resource : SSU::Online Contester Fall Contest #2
Date : Fall 2002
 
 
 
 
/*/
题意:
有N个村庄,每个村庄有一个权值,有n-1条路,将村庄连起来,然后选取这些路中的一个联通图,权值最大。 整个图都被联通,找其中权值最大的子联通块。 树状DP。 代码风格学了某个学长的写了个结构体,真刺激。。
AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#include"string"
#include"vector"
#include"queue"
#include"cmath"
using namespace std;
typedef long long LL ;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define FK(x) cout<<"["<<x<<"]\n"
#define bigfor(x) for(LL qq=1;qq<= T ;qq++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int MX = 16666; struct Treedp {
struct Edge {
int v,nxt;
} E[MX<<1]; int Head[MX],erear;
bool vis[MX];
int dp[MX];
int INF=-1e9-1e5; void init() {
erear=0;
memset(E,0);
memset(vis,0);
memset(Head,-1);
} void add(int u,int v) {
E[erear].v=v;
E[erear].nxt=Head[u];
Head[u]=erear++;
} int run(int u) {
vis[u]=1;
for(int i=Head[u]; ~i; i=E[i].nxt) {
int v=E[i].v;
if(!vis[v]) {
dp[u]+=max(0,run(v));
}
}
return dp[u];
} void print(int n) {
for(int i=1; i<=n; i++)
cout<<dp[i]<<" ";
puts("");
}
}; Treedp tdp; int main() {
int n,l,r;
scanf("%d",&n);
tdp.init();
for(int i=1; i<=n; i++) {
scanf("%d",&tdp.dp[i]);
}
for(int i=1; i<n; i++) {
scanf("%d%d",&l,&r);
tdp.add(l,r);
tdp.add(r,l);
}
int maxx=-1e9-1000000;
tdp.run(1);
for(int i=1; i<=n; i++) {
maxx=max(maxx,tdp.dp[i]);
}
// tdp.print(n);
printf("%d\n",maxx);
return 0;
}

  

 

ACM: Long Live the Queen - 树上的DP的更多相关文章

  1. 树上的DP

    CF#196B http://codeforces.com/contest/338/problem/B 题意:在一颗树上,给m个点,求到所有m个点距离不超过d的点的个数,所有路径长度为1. 分析:问题 ...

  2. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 D.寻找-树上LCA(树上a到b的路径上离c最近的点)

    链接:https://ac.nowcoder.com/acm/contest/558/D来源:牛客网 寻找 小猫在研究树. 小猫在研究树上的距离. 给定一棵N个点的树,每条边边权为1. Q次询问,每次 ...

  3. LOJ #2542. 「PKUWC 2018」随机游走(最值反演 + 树上期望dp + FMT)

    写在这道题前面 : 网上的一些题解都不讲那个系数是怎么推得真的不良心 TAT (不是每个人都有那么厉害啊 , 我好菜啊) 而且 LOJ 过的代码千篇一律 ... 那个系数根本看不出来是什么啊 TAT ...

  4. HDU 5956 The Elder (树上斜率DP)

    题意:给定上一棵树,然后每条边有一个权值,然后每个点到 1 的距离有两种,第一种是直接回到1,花费是 dist(1, i)^2,还有另一种是先到另一个点 j,然后两从 j 向1走,当然 j 也可以再向 ...

  5. loj 2542 随机游走 —— 最值反演+树上期望DP+fmt

    题目:https://loj.ac/problem/2542 因为走到所有点的期望就是所有点期望的最大值,所以先最值反演一下,问题变成从根走到一个点集任意一点就停止的期望值: 设 \( f[x] \) ...

  6. 『保卫王国 树上倍增dp』

    保卫王国 Description Z 国有n座城市,n - 1条双向道路,每条双向道路连接两座城市,且任意两座城市 都能通过若干条道路相互到达. Z 国的国防部长小 Z 要在城市中驻扎军队.驻扎军队需 ...

  7. [CSP-S模拟测试]:点亮(状压DP+树上背包DP)

    题目传送门(内部题121) 输入格式 第一行,一个正整数$n$. 第二行,$n-1$个正整数$p_2,p_3,...,p_n$.保证$p_u$是在$1$到$u-1$中等概率随机选取的. 接下来$n$行 ...

  8. 软件安装:树上分组DP/tarjan缩点/(也许基环树?)

    提炼:tarjan环缩成点,建0虚根,跑树形DP,最难的是看出可能有n个点n条边然后缩点,n个点n条边可能不只有一个环 n个点n条边->基环树: 基环树,也是环套树,简单地讲就是树上在加一条边. ...

  9. 树形DP——动态规划与数据结构的结合,在树上做DP

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法与数据结构的第15篇,也是动态规划系列的第4篇. 之前的几篇文章当中一直在聊背包问题,不知道大家有没有觉得有些腻味了.虽然经典的文 ...

随机推荐

  1. Android画面显示原理

    转自: http://blog.csdn.net/luoshengyang/article/details/7691321/ http://blog.chinaunix.net/uid-1675954 ...

  2. Delphi之DLL知识学习5---在Delphi应用程序中使用DLL

    首先说明一下:同一个动态库(DLL)被多个的程序加载的话,那么将会在每次加载的时候都会重新分配新的独立的内存空间,绝对不是共用一个,所以当一个DLL被多次加载的时候,其会在内存中“复制”多份,不会互相 ...

  3. PostgreSQL简单介绍

    自从MySQL被Oracle收购以后,PostgreSQL逐渐成为开源关系型数据库的首选. 本文介绍PostgreSQL的安装和基本用法,供初次使用者上手.以下内容基于Debian操作系统,其他操作系 ...

  4. 轻松学习RSA加密算法原理

    转自:http://blog.csdn.net/sunmenggmail/article/details/11994013 http://blog.csdn.net/q376420785/articl ...

  5. set[c++]

    #include <iostream> using namespace std; #include <set> int main(int argc, const char * ...

  6. [荐]Js apply()和call()方法详解 - http://www.w3cfuns.com/article-5596443-1-1.html

    本帖最后由 默默DE人生 于 2013-3-19 13:22 编辑 Js apply方法详解我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文 ...

  7. JSON数据解析(转)

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 本文将主要介绍在Android ...

  8. merge布局

    当LayoutInflater遇到这个标签时,它会跳过它,并将<merge />内的元素添加到<merge />的父元素里.迷惑了吗?让我们用<merge />来替 ...

  9. AJAX案例二:简单表单验证

    案例:如果用户名输入为张三,那么在失去焦点时后面会显示该用户名已被注册,否则显示可以注册! <%@ page language="java" import="jav ...

  10. 【maven + hibernate(注解) +spring +springMVC】 使用maven搭建项目

    研究,百度,查资料+好友帮助,使用MyEcplise2015工具,通过maven搭建hibernate+springMVC+spring的项目,数据库采用MySql5.5 不过使用的版本会在项目搭建过 ...