ACM: Long Live the Queen - 树上的DP
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的更多相关文章
- 树上的DP
CF#196B http://codeforces.com/contest/338/problem/B 题意:在一颗树上,给m个点,求到所有m个点距离不超过d的点的个数,所有路径长度为1. 分析:问题 ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 D.寻找-树上LCA(树上a到b的路径上离c最近的点)
链接:https://ac.nowcoder.com/acm/contest/558/D来源:牛客网 寻找 小猫在研究树. 小猫在研究树上的距离. 给定一棵N个点的树,每条边边权为1. Q次询问,每次 ...
- LOJ #2542. 「PKUWC 2018」随机游走(最值反演 + 树上期望dp + FMT)
写在这道题前面 : 网上的一些题解都不讲那个系数是怎么推得真的不良心 TAT (不是每个人都有那么厉害啊 , 我好菜啊) 而且 LOJ 过的代码千篇一律 ... 那个系数根本看不出来是什么啊 TAT ...
- HDU 5956 The Elder (树上斜率DP)
题意:给定上一棵树,然后每条边有一个权值,然后每个点到 1 的距离有两种,第一种是直接回到1,花费是 dist(1, i)^2,还有另一种是先到另一个点 j,然后两从 j 向1走,当然 j 也可以再向 ...
- loj 2542 随机游走 —— 最值反演+树上期望DP+fmt
题目:https://loj.ac/problem/2542 因为走到所有点的期望就是所有点期望的最大值,所以先最值反演一下,问题变成从根走到一个点集任意一点就停止的期望值: 设 \( f[x] \) ...
- 『保卫王国 树上倍增dp』
保卫王国 Description Z 国有n座城市,n - 1条双向道路,每条双向道路连接两座城市,且任意两座城市 都能通过若干条道路相互到达. Z 国的国防部长小 Z 要在城市中驻扎军队.驻扎军队需 ...
- [CSP-S模拟测试]:点亮(状压DP+树上背包DP)
题目传送门(内部题121) 输入格式 第一行,一个正整数$n$. 第二行,$n-1$个正整数$p_2,p_3,...,p_n$.保证$p_u$是在$1$到$u-1$中等概率随机选取的. 接下来$n$行 ...
- 软件安装:树上分组DP/tarjan缩点/(也许基环树?)
提炼:tarjan环缩成点,建0虚根,跑树形DP,最难的是看出可能有n个点n条边然后缩点,n个点n条边可能不只有一个环 n个点n条边->基环树: 基环树,也是环套树,简单地讲就是树上在加一条边. ...
- 树形DP——动态规划与数据结构的结合,在树上做DP
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法与数据结构的第15篇,也是动态规划系列的第4篇. 之前的几篇文章当中一直在聊背包问题,不知道大家有没有觉得有些腻味了.虽然经典的文 ...
随机推荐
- Linux SNMP oid
http://www.debianadmin.com/linux-snmp-oids-for-cpumemory-and-disk-statistics.html
- Win7 DCOM 配置中我的电脑出现红色箭头并且无属性显示的解决方法
http://blog.csdn.net/embededvc/article/details/19234479
- DIV宽度自动缓慢变化
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Freemarker使用入门
一.概述: FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写 Template + data_model = output FreeMarker也是与Web容 ...
- Jquery获取iframe子/父窗口中的标签
获取子窗口中的标签: $("#id",document.frames('iframename').document); 获取父窗口中的标签: $('#id', parent.doc ...
- CG&CAD resource
Computational Geometry The Geometry Center (UIUC) Computational Geometry Pages (UIUC) Geometry in Ac ...
- C++ 中 char 与 int 转换问题
itoa 功 能:把一整数转换为字符串 函 数:char *itoa(int value, char *string, int radix); 解 释:itoa 是英文integer to ar ...
- [Liferay6.2]核心配置文件portal.properties
portal.properties是liferay中一个非常核心的配置文件.我们可以在liferay源代码或者解压liferay部署包中的portal-impl.jar中获得.以liferay6.2为 ...
- silverlight和wpf中暴露 给子类override
protected virtual void OnSelectionChanged(SelectionChangedEventArgs args) { } public TestTabControl( ...
- 梳理源码中 View 的工作原理
欢迎Follow我的GitHub, 关注我的掘金. 在View的工作过程中, 执行三大流程完成显示, 测量(measure)流程, 布局(layout)流程, 绘制(draw)流程. 从perform ...
