POJ3585:Accumulation Degree(换根树形dp)
Accumulation Degree
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 3425 | Accepted: 859 |
题目链接:http://poj.org/problem?id=3585
Description:
Trees are an important component of the natural landscape because of their prevention of erosion and the provision of a specific ather-sheltered ecosystem in and under their foliage. Trees have also been found to play an important role in producing oxygen and reducing carbon dioxide in the atmosphere, as well as moderating ground temperatures. They are also significant elements in landscaping and agriculture, both for their aesthetic appeal and their orchard crops (such as apples). Wood from trees is a common building material.
Trees also play an intimate role in many of the world's mythologies. Many scholars are interested in finding peculiar properties about trees, such as the center of a tree, tree counting, tree coloring. A(x) is one of such properties.
A(x) (accumulation degree of node x) is defined as follows:
- Each edge of the tree has an positive capacity.
- The nodes with degree of one in the tree are named terminals.
- The flow of each edge can't exceed its capacity.
- A(x) is the maximal flow that node x can flow to other terminal nodes.
Since it may be hard to understand the definition, an example is showed below:
| A(1)=11+5+8=24 | ||
| Details: | 1->2 | 11 |
| 1->4->3 | 5 | |
| 1->4->5 | 8(since 1->4 has capacity of 13) | |
| A(2)=5+6=11 | ||
| Details: | 2->1->4->3 | 5 |
| 2->1->4->5 | 6 | |
| A(3)=5 | ||
| Details: | 3->4->5 | 5 |
| A(4)=11+5+10=26 | ||
| Details: | 4->1->2 | 11 |
| 4->3 | 5 | |
| 4->5 | 10 | |
| A(5)=10 | ||
| Details: | 5->4->1->2 | 10 |
The accumulation degree of a tree is the maximal accumulation degree among its nodes. Here your task is to find the accumulation degree of the given trees.
Input:
The first line of the input is an integer T which indicates the number of test cases. The first line of each test case is a positive integer n. Each of the following n - 1 lines contains three integers x, y, z separated by spaces, representing there is an edge between node x and node y, and the capacity of the edge is z. Nodes are numbered from 1 to n.
All the elements are nonnegative integers no more than 200000. You may assume that the test data are all tree metrics.
Output:
For each test case, output the result on a single line.
Sample Input:
1
5
1 2 11
1 4 13
3 4 5
4 5 10
Sample Output:
26
题意:
给出一棵树,树上的边都有其权值,让我们求一个点能往外流的最大流量(会受到其它边权容量的限制)。
题解:
我们可以对于每个点进行一次树形dp来求,但是显然时间复杂度很高,会TLE...
我们考虑先对1号点dp一次,并且维护每个结点的dp值,表示当前结点流向其子树的最大流量,然后再dfs一次进行换根。
换根的过程中我们需要考虑清楚我们需要哪些知道量,需要更新哪些量,子节点的值能否被父亲结点更新。
这一题中,我们从u->v,假设已经知道了f(u),即以u为根的最大流量(不同于之前的dp数组),那么对于v而言,f(v)=dp(v)+min( w(u,v) , min( f(u)-min(w(u,v),dp(v)) ) )。
要注意下u的另一颗子树即g[u].size()=1的情况,这时转移直接为f(v)=w(u,v)+dp(v)。
具体见代码吧:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 1e9
#define mp make_pair
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e5+;
int n,T;
int dp[N];
vector <pair<int,int> > g[N];
void dfs(int node,int pa){
int tmp = ;
for(int i=;i<g[node].size();i++){
int v=g[node][i].first,w=g[node][i].second;
if(v==pa) continue ;
dfs(v,node);
tmp+=min(dp[v],w);
}
if(tmp>) dp[node]=tmp;
return ;
}
int ans=;
void go(int u,int pa,int sum){
ans=max(ans,sum);
int len = g[u].size();
for(int i=;i<len;i++){
int v=g[u][i].first,w=g[u][i].second;
if(v==pa) continue ;
if(len==) go(v,u,dp[v]+w);
else go(v,u,dp[v]+min(sum-min(dp[v],w),w));
}
}
int main(){
cin>>T;
while(T--){
scanf("%d",&n);
if(n==){
puts("");
continue ;
}
ans=;memset(dp,INF,sizeof(dp));
for(int i=;i<=n;i++) g[i].clear();
for(int i=;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
g[u].push_back(mp(v,w));
g[v].push_back(mp(u,w));
}
dfs(,-);
for(int i=;i<=n;i++) dp[i]=(dp[i]==INF?:dp[i]);
go(,-,dp[]);
cout<<ans<<endl;
}
return ;
}
POJ3585:Accumulation Degree(换根树形dp)的更多相关文章
- poj3585 Accumulation Degree(换根dp)
传送门 换根dp板子题(板子型选手 题意: 一棵树确定源点和汇点找到最大的流量(拿出一整套最大瘤板子orz ; int head[maxn],tot; struct node { int nt,to; ...
- BZOJ2591/LG3047 「USACO12FEB」Nearby Cows 换根树形DP
问题描述 BZOJ2591 LG3047 题解 换根树形DP. 设 \(opt[i][j]\) 代表 当 \(1\) 为根时,\(i\) 为根的子树中,到 \(i\) 的距离为 \(j\) 的权值和 ...
- Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】
传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...
- [BZOJ3566][SHOI2014]概率充电器 换根树形DP
链接 题意:n个充电元件形成一棵树,每个点和每条边都有各自的充电概率,元件可以自身充电或者通过其他点和边间接充电,求充电状态元件的期望个数 题解 设1为根节点 设 \(f[x]\) 表示 \(x\) ...
- 51nod1812树的双直径(换根树DP)
传送门:http://www.51nod.com/Challenge/Problem.html#!#problemId=1812 题解:头一次写换根树DP. 求两条不相交的直径乘积最大,所以可以这样考 ...
- 题解 poj3585 Accumulation Degree (树形dp)(二次扫描和换根法)
写一篇题解,以纪念调了一个小时的经历(就是因为边的数组没有乘2 phhhh QAQ) 题目 题目大意:找一个点使得从这个点出发作为源点,流出的流量最大,输出这个最大的流量. 以这道题来介绍二次扫描和换 ...
- poj3585 Accumulation Degree[树形DP换根]
思路其实非常简单,借用一下最大流求法即可...默认以1为根时,$f[x]$表示以$x$为根的子树最大流.转移的话分两种情况,一种由叶子转移,一种由正常孩子转移,判断一下即可.换根的时候由頂向下递推转移 ...
- $Poj3585\ Accumulation Degree$ 树形$DP/$二次扫描与换根法
Poj Description 有一个树形的水系,由n-1条河道与n个交叉点组成.每条河道有一个容量,联结x与y的河道容量记为c(x,y),河道的单位时间水量不能超过它的容量.有一个结点是整个水系的发 ...
- poj3585 Accumulation Degree(树形dp,换根)
题意: 给你一棵n个顶点的树,有n-1条边,每一条边有一个容量z,表示x点到y点最多能通过z容量的水. 你可以任意选择一个点,然后从这个点倒水,然后水会经过一些边流到叶节点从而流出.问你最多你能倒多少 ...
随机推荐
- MySQL - Mac下安装MySQL
1. 去官网下载dmg的安装文件. 2. 下载完成后,运行安装文件,按步骤进行安装,安装完成后会弹出一个框显示临时密码! 3. 编辑~/.bashrc文件,配置快速启动/停止/重启/cdhome/别名 ...
- Oracle常用傻瓜问题1000问
Oracle常用傻瓜问题1000问 大家在应用ORACLE的时候可能会遇到很多看起来不难的问题, 特别对新手来说, 今天我简单把它总结一下, 发布给大家, 希望对大家有帮助! 和大家一起探讨, 共同进 ...
- http网络协议 学习摘要
一:HTTP协议状态码 状态码主要用于描述当客户端向服务器发送请求时的返回结果,标记服务端的处理是否正常,通知出现的错误等工作. 状态码整体分为五大类: 1开头的状态码:信息类状态码,主要接收请求,表 ...
- PLC状态机编程第六篇-优化PLC程序生成
还记得第一篇博客中,我们在状态机中手写上升沿来处理有别于传统的一键启停程序,那个手写的上升沿就是优化手段.stateflow状态机是带事件的,事件本身支持上升沿和下降沿等事件,在这里,如果我们选择用事 ...
- J.U.C 系列之Atomic原子类
一 什么是原子类? 所谓原子类必然是具有原子性的类,原子性操作--原子操作,百度百科中给的定义如下 "原子操作(atomic operation)是不需要synchronized" ...
- [bzoj1552][Cerc2007]robotic sort&&[bzoj3506][Cqoi2014]排序机械臂
非常垃圾的一道平衡树,结果被日了一天.很难受嗷嗷嗷 首先不得不说网上的题解让我这个本来就不熟悉平衡树的彩笔很难受——并不好理解. 还好Sinogi大佬非常的神,一眼就切掉了,而且用更加美妙的解法. 题 ...
- [Django]我的第一个网页,报错啦~(自己实现过程中遇到问题以及解决办法)
环境配置: python :2.7.13 django:1.10.5 OS:Win7(64位)& Centos7 问题描述 解决办法 global name 'render' is no ...
- groupNoAdj
public boolean groupNoAdj(int start, int[] nums, int target) { if( start >= nums.length){ return ...
- 【The VC Dimension】林轩田机器学习基石
首先回顾上节课末尾引出来的VC Bound概念,对于机器学习来说,VC dimension理论到底有啥用. 三点: 1. 如果有Break Point证明是一个好的假设集合 2. 如果N足够大,那么E ...
- elk-logstash: window下指定jdk目录
\bin\logstash.bat文件中, SETLOCAL的后面,CALL "%SCRIPT_DIR%\setup.bat" 的前面增加一行: @echo off SETLOCA ...