题目链接

Accumulation Degree

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5388   Accepted: 1309

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

给你一棵树,每个节点作为一个源头,只有一条边的节点可以视为入海口,每条边有最大流量,问这棵树源头的最大流量。

本题是一个不定根的树,可以用“二次扫描与换根法”。

1、第一次扫描时,任选一个点为根,在“有根树”上执行一次树形dp,也就是回溯时发生的,自底向上的状态转移;

2、第二次扫描时,从刚才选出的根出发,对整棵树再进行一次dfs,每次递归前自顶向下的推导,给出换根后的结果。就是相当于把这条与“父亲”相连的边转换成与儿子相连的边,进行处理。

对于本题,我们任选一个点作为根节点,化成一棵有根树。此时每一个节点作为源点的流量是其向下走(子树)和向上走(父亲)流量之和。求子树自然一次dfs,注意u的子节点v入度为1的时候,v的子树流量为0,u直接加上u,v之间的边权即可。

此时d[x]保存的是以x为根节点的子树的流量和。

对于我们选定的根节点root,其向上走的结果为0,root作为源点最终的结果已经出来了。

第二次dfs,就是求向上走的流量,此时父亲节点的最终答案d[u]已经算好了,儿子节点还是以儿子节点为根的子树的流量和d[v],直接把父亲节点的总流量减去走以这个儿子为根节点的子树的流量,此时就是父亲节点走别的路的流量,加到儿子上即可。

注意如果父亲节点的度为1,儿子节点直接加上c[u,v](u和v之间边的边权)。

“父亲节点的总流量减去走以这个儿子为根节点的子树的流量”       d[u]-min(d[x],c[u,v])

“加到儿子上” d[v]+=min(d[u]-min(d[x],c[u,v]),c[u,v])

时刻注意边权与节点流量的取min。

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=2e5+10;
struct node
{int v,w;
//  node (): {}node(int v,int w):v(v),w(w){}
};
int in[N],d[N];
vector<node>tr[N];
void dfs1(int u,int fa)
{for(int i=0;i<tr[u].size();i++){int v=tr[u][i].v;if(v==fa) continue;dfs1(v,u);if(in[v]==1) d[u]+=tr[u][i].w;//当儿子度为1的时候,d[y]=0,不能选d[y] else d[u]+=min(tr[u][i].w,d[v]);}
}
void dfs2(int u,int fa)
{for(int i=0;i<tr[u].size();i++){int v=tr[u][i].v;if(v==fa) continue;if(in[u]==1) d[v]+=tr[u][i].w;else d[v]+=min(d[u]-min(d[v],tr[u][i].w),tr[u][i].w);dfs2(v,u);   }
}
int main()
{ios::sync_with_stdio(false);cin.tie(0);int T;cin>>T;while(T--){int n,x,y,z;cin>>n;for(int i=1;i<=n;i++)tr[i].clear();ms(in,0);ms(d,0);for(int i=1;i<n;i++){cin>>x>>y>>z;tr[x].push_back(node(y,z));tr[y].push_back(node(x,z));in[x]++;in[y]++;}dfs1(1,0);dfs2(1,0);int ans=0;for(int i=1;i<=n;i++)ans=max(ans,d[i]);cout<<ans<<endl;}}

POJ 3585 Accumulation Degree 树形dp相关推荐

  1. POJ - 4045 Power Station(树形dp/树的重心)

    题目链接:点击查看 题目大意:给出一个n个节点的树,我们需要选出一个节点,到其余任何节点的距离和最小 题目分析:这个题我的第一反应是用树的重心,先求出来符合条件的点,然后再跑一遍dfs求距离,最后输出 ...

  2. POJ - 2342 Anniversary party(树形dp入门)

    题目链接:点击查看 题目大意:每个人都有一个快乐值,给定一个树状的从属关系,仅当上司和下属都不在的时候这个个人的快乐值才能表现出来,问怎么样才能让整体的快乐值达到最大 题目分析:做线段树做吐了,来换换 ...

  3. POJ 3342- Party at Hali-Bula (树形dp+判断是否唯一)

    题目: Dear Contestant, I'm going to have a party at my villa at Hali-Bula to celebrate my retirement f ...

  4. poj 2152 Fire - 经典树形dp

    题目链接: http://poj.org/problem?id=2152 不多说.. 2006 陈启峰消防站解题报告 这dp简直是神了.. #include <cstdio> #inclu ...

  5. poj 1192(简单树形dp)

    题意:这题描述看似很复杂, 其实读懂后就是一个相邻点之间连通问题,水题. 解题思路:首先把相邻的点连接起来建立一棵树,dp[i][0]表示不选择i节点可以得到的最大价值,dp[i][1]表示选择i节点 ...

  6. POJ 3107 Godfather(树形DP(找重心))

    任重而道远 Description Last years Chicago was full of gangster fights and strange murders. The chief of t ...

  7. 『树形DP·换根法』Accumulation Degree

    题目描述 有一个树形的水系,由 N-1 条河道和 N 个交叉点组成. 我们可以把交叉点看作树中的节点,编号为 1~N,河道则看作树中的无向边. 每条河道都有一个容量,连接 x 与 y 的河道的容量记为 ...

  8. Poj·Accumulation Degree

    初见安~这里是传送门:Poj P3585 Description Trees are an important component of the natural landscape because o ...

  9. POJ 1155 TELE【树形DP】

    POJ 1155 TELE http://poj.org/problem?id=1155 大意:某电台要广播一场比赛,该电台网络是由N个网点组成的一棵树,其中M个点为客户端, 其余点为转发站.客户端i ...

最新文章

  1. 任务计划程序-Windows2008定时重启
  2. Spring boot定制错误json数据
  3. python多线程gil_Python 多线程、多进程 (一)之 源码执行流程、GIL
  4. Sql Server中三种字符串合并方法的性能比较
  5. C#正则表达式判断输入的是不是数字
  6. PCM设备的作用,为什么要选用PCM设备?
  7. 为html.EditorFor添加样式
  8. Java中什么是JAP之hibernate-mvc修改功能-Springmvc
  9. 面向对象进阶 各种可以自定制的内置方法
  10. GOOD AI Example GREAT AI Company
  11. 图片jpg格式怎么转换
  12. 直接从国家统计局上找数据,并分析人口数据变化,做成可视化图
  13. java计算机毕业设计京东仓库管理系统源码+mysql数据库+系统+lw文档+部署
  14. VScode+Latex (Recipe terminated with fatal error: spawn xelatex ENOENT)和latex简单使用介绍
  15. 毒鸡汤词汇类的前端小程序源码模板
  16. MCP2517FD应用总结
  17. python随机点名程序 图形化_python写一个随机点名软件,python随机点名,最近有个随机点名软件...
  18. Win7下安装Docker(虚拟机win7)
  19. 解决: ValueError: source code string cannot contain null bytes 问题
  20. fota 差分包_FOTA升级

热门文章

  1. 卧槽!我用Python做一个打字测试器!看看谁是最快的男人!
  2. C# 获取汉字拼音首字母(修正X问题,真正修正)
  3. C语言每日一练 —— 第20天:位运算
  4. 3dsMax---用挤出做柜子
  5. RJ45隔离变压器作用
  6. 可计算性、可判定性和可满足性
  7. 【腾讯云原生降本增效大讲堂】云原生混部技术标准解读
  8. Dell intel i5 1135笔记本 ubuntu18.04无法调节屏幕亮度
  9. 侍魂胧月传说怎么在电脑上玩 侍魂胧月传说电脑版玩法攻略
  10. 全国计算机系统导出名单,墨涩网 - 快速查询导出电脑常用信息——墨涩网