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:

  1. Each edge of the tree has an positive capacity.
  2. The nodes with degree of one in the tree are named terminals.
  3. The flow of each edge can't exceed its capacity.
  4. 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 xyz 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)的更多相关文章

  1. poj3585 Accumulation Degree(换根dp)

    传送门 换根dp板子题(板子型选手 题意: 一棵树确定源点和汇点找到最大的流量(拿出一整套最大瘤板子orz ; int head[maxn],tot; struct node { int nt,to; ...

  2. BZOJ2591/LG3047 「USACO12FEB」Nearby Cows 换根树形DP

    问题描述 BZOJ2591 LG3047 题解 换根树形DP. 设 \(opt[i][j]\) 代表 当 \(1\) 为根时,\(i\) 为根的子树中,到 \(i\) 的距离为 \(j\) 的权值和 ...

  3. 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 ...

  4. [BZOJ3566][SHOI2014]概率充电器 换根树形DP

    链接 题意:n个充电元件形成一棵树,每个点和每条边都有各自的充电概率,元件可以自身充电或者通过其他点和边间接充电,求充电状态元件的期望个数 题解 设1为根节点 设 \(f[x]\) 表示 \(x\) ...

  5. 51nod1812树的双直径(换根树DP)

    传送门:http://www.51nod.com/Challenge/Problem.html#!#problemId=1812 题解:头一次写换根树DP. 求两条不相交的直径乘积最大,所以可以这样考 ...

  6. 题解 poj3585 Accumulation Degree (树形dp)(二次扫描和换根法)

    写一篇题解,以纪念调了一个小时的经历(就是因为边的数组没有乘2 phhhh QAQ) 题目 题目大意:找一个点使得从这个点出发作为源点,流出的流量最大,输出这个最大的流量. 以这道题来介绍二次扫描和换 ...

  7. poj3585 Accumulation Degree[树形DP换根]

    思路其实非常简单,借用一下最大流求法即可...默认以1为根时,$f[x]$表示以$x$为根的子树最大流.转移的话分两种情况,一种由叶子转移,一种由正常孩子转移,判断一下即可.换根的时候由頂向下递推转移 ...

  8. $Poj3585\ Accumulation Degree$ 树形$DP/$二次扫描与换根法

    Poj Description 有一个树形的水系,由n-1条河道与n个交叉点组成.每条河道有一个容量,联结x与y的河道容量记为c(x,y),河道的单位时间水量不能超过它的容量.有一个结点是整个水系的发 ...

  9. poj3585 Accumulation Degree(树形dp,换根)

    题意: 给你一棵n个顶点的树,有n-1条边,每一条边有一个容量z,表示x点到y点最多能通过z容量的水. 你可以任意选择一个点,然后从这个点倒水,然后水会经过一些边流到叶节点从而流出.问你最多你能倒多少 ...

随机推荐

  1. Laravel系列 Web

    一.Homestead准备 上一篇文章已经对它的配置进行了说明,下面对Homestead.yaml进行说明 --- ip: "192.168.10.10" memory: 2048 ...

  2. php jsonp实例 mip无限滚动组件接口注意事项

    在改造mip的过程中,很多同学遇到这样一个问题.mip无限滚动问题 异步请求数据接口(仅支持 JSONP 请求) 异步请求接口需要规范 callback 为 'callback' 那么什么是JSONP ...

  3. python之doctest的用法

    doctest是python自带的一个模块,你可以把它叫做“文档测试”(doctest)模块. doctest的使用有两种方式:一个是嵌入到python源中.另一个是放到一个独立文件. doctest ...

  4. Apache2服务配置ubuntu16.04+django1.11

    话不多说直接上步骤 环境 Ubuntu 16.04 Python 3.5.2 Django 1.11 Apache 2.4 1.Apache2安装 sudo apt-get install apach ...

  5. strak组件(9):关键字搜索

    效果图: 在列表视图函数增加搜索功能. 新增函数 def get_search_list(self) 钩子方法,获取搜索条件 一.strak组件 strak/service/core_func.py ...

  6. wlr设置 Blog Ping

    ref:http://www.cnblogs.com/zhangyang/archive/2011/07/22/2113856.html 设置 Blog Ping 1.什么是Ping服务(Ping S ...

  7. Hbase的安装与部署(集群版)

    HBase 部署与使用 部署 Zookeeper 正常部署 $ ~/modules/zookeeper-3.4.5/bin/zkServer.sh start 首先保证 Zookeeper 集群的正常 ...

  8. iOS 中 AFNetworking HTTPS 的使用

    由于我们公司由HTTP转HTTPS,出现了一系列问题特此记录下. 一.HTTPS 二.App Transport Security 三.iOS 中用HTTPS 注意的问题 四.使用 AFNetwork ...

  9. python selenium 练习 自动获取豆瓣阅读当前特价书籍 chrome 元素定位 窗口切换 元素过期

    豆瓣原创电子书每周推出数十本限时免费数目,一周免费期过后恢复原价.想着豆瓣原创书中有不少值得一看,便写了个脚本,免去一个个添加的烦恼. 使用了Windows下selenium+Python的组合,有较 ...

  10. ASP.NET Core 认证与授权[2]:Cookie认证 (笔记)

    原文链接:https://www.cnblogs.com/RainingNight/p/cookie-authentication-in-asp-net-core.html 由于HTTP协议是无状态的 ...