Accumulation Degree POJ - 3585 (树形dp)(二次扫描和换根法)

【题解】poj3585 Accumulation Degree 二次扫描与换根法 题目链接 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 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 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
题意:找一个点使得,使得从这个点出发作为源点,发出的流量最大,输出这个最大的流量。
思路:以前的树规都是定点的,就是我们确定了根,或者说选择哪个点作为根都可以,但是这个题就是不一样了,
设D[x]D[x]为以x为根的子树中,把x作为源点,从x发出的流量的最大值,这个很用dfs扫描一次可以得出。
转移方程:

D[x]=∑y∈son(x)degree[y]==1?c(x,y):min(D[y],c(x,y))D[x]=∑y∈son(x)degree[y]==1?c(x,y):min(D[y],c(x,y))

设F[x]F[x]为以x作为整棵树的根是所求的答案,也只要dfs在扫描一次。
易知:D[root]==F[root]D[root]==F[root]
转移过程:
F[y]=D[y]+degree[x]==1?c(x,y):min(F[x]−min(D[y],c(x,y)),c(x,y))F[y]=D[y]+degree[x]==1?c(x,y):min(F[x]−min(D[y],c(x,y)),c(x,y))

所以二次扫描的名字由此得来.也的确是逻辑上把根换了。
代码:

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#define maxx 200005
using namespace std;
int head[maxx],to[maxx<<1],w[maxx<<1],_next[maxx<<1];
int d[maxx];
int cnt=0;
int deg[maxx];
void addEdge(int x,int y,int _w)
{
    to[++cnt]=y,w[cnt]=_w,_next[cnt]=head[x],head[x]=cnt;
    to[++cnt]=x,w[cnt]=_w,_next[cnt]=head[y],head[y]=cnt;
}
int n;
void dfs1(int root,int fa)
{
    d[root]=0;
    for(int i=head[root];i;i=_next[i])
    {
        int v=to[i];
        if(v==fa)
            continue;
        dfs1(v,root);
        if(deg[v]==1)
            d[root]+=w[i];
        else
            d[root]+=min(w[i],d[v]);
    }
}
int f[maxx];
void dfs2(int root,int fa)
{
    for(int i=head[root];i;i=_next[i])
    {
        int v=to[i];
        if(v==fa)
            continue;
        if(deg[root]==1)
            f[v]=d[v]+w[i];
        else
            f[v]=d[v]+min(f[root]-min(w[i],d[v]),w[i]);
        dfs2(v,root);
    }
}
void init()
{
    memset(head,0,sizeof(head));
    memset(f,0,sizeof(f));
    memset(deg,0,sizeof(deg));
    cnt=0;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int x,y,w;
        init();
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&w);
            deg[x]++;
            deg[y]++;
            addEdge(x,y,w);
        }
        if(n==0||n==1)
        {
            cout<<0<<endl;
            continue;
        }
        int root=1;
        dfs1(root,0);
        f[root]=d[root];
        dfs2(root,0);
        int ans=0;
        for(int i=1;i<=n;i++)
            ans=max(ans,f[i]);
        cout<<ans<<endl;
    }
    return 0;
}



GBT 27930-2015/2023国标充电协议A类CAN报文ID说明 国标充电CAN的ID,仅供学习参考。 阅读详情

相关推荐

ubuntu虚拟机搭建OP-TEE

在网上找了一些搭建OP-TEE的过程,模仿着进行搭建,结果有好多报错,为了方便自己以后重新搭建,所以将整个过程记录下来。

weixin_44213847的博客 1001

POJ3585(Accumulation Degree 换根dp模板 所有点到所有点的容量)

题目 #include<cstdio> #include<cstring> #include<algorithm> #define en '\n' #define m(a,b) memset(a,b,sizeof a) using namespace std; typedef long long ll; const int N=2e5+5; struct E...

qq_42576687的博客 263

vins-mono保存、重载地图、evo工具测试

下面咱们来对vins-mono的重定位vins-mono官方源码地址:https://github.com/HKUST-Aerial-Robotics/VINS-Mono https://blog.youkuaiyun.com/learning_tortosie/article/details/83182258 运行这条: roslaunch benchmark_publisher publish.launc...

追 1万+

poj3585【树形dp二次换根】

这道题如果用暴力的方法是枚举根节点,对每个根节点dfs下去,但是这样肯定会T,需要用树形dp优化一下。 辅助数组:d[maxn], f[manx]; d[i]:代表以任意某个节点为根,i可以向其子节点流的最大流量。 f[i]:代表以i为根节点时,最大流量。 当我们以1为根节点时,明显f[1] = d[1]; 处理d【i】数组:一遍dfs就行。 关键在于如何求f【i】? 我们可以知道f[i] = d...

qq_39921637的博客 484

多模态融合相关概念

多模态图像融合

weixin_43921949的博客 7357

Ansys Lumerical 2023 R1.2新功能实战:薄膜铌酸锂调制器仿真全流程解析

本文详细解析了Ansys Lumerical 2023 R1.2在薄膜铌酸锂电光相位调制器仿真中的最新功能与应用。通过实战演示,展示了从电极配置、电场计算到模式分析的全流程,重点介绍了各向异性介电常数支持和多物理场耦合优化等关键技术,助力开发者高效设计高性能光通信器件。

weixin_28454475的博客 1213

Accumulation Degree POJ 3585(树形DP 二次扫描与换根法)

【例题】Accumulation Degree POJ 3585 有一个树形的水系,由N-1条河道和N个交叉点组成。我们可以把交叉点看作树中的节点,编号为1~N,河道则看作树中的无向边。每条河道都有一个容量,连x与y河道的容量记为c(x,y)。河道中单位时间流过的水量不能超过河道的容量。有一个节点是整个水系的发源地,可以源源不断地流出水,我们称之为源点。除了源点之外,树中所有度数为1的节点都是入海口,可以吸收无限多的水,我们称之为汇点。也就是说,水系中的水从源点出发,沿着每条河道,最终流向各个汇点。 在整个

hwdn3000的博客 466

第 1 章 Prometheus 监控 | Prometheus 入门

了解 Prometheus 监控系统,学会安装及了解页面功能。

甘蓝的专栏 486

POJ3585

POJ3585 题意 给定一棵nnn个节点的无向带权树,要你找出一个节点作为根,向叶子节点流水,使流水量最大。 思路 换根DP 这种题往往都是先从某个点出发DFS1DFS_1DFS1​,求出以这个节点ttt为根的答案,并记录与答案相关的数据到数组中。 然后DFS2DFS_2DFS2​从ttt节点出发,记录换根的答案,其中所有根的最大值就为所求值。 难点在于该如何去找寻其中的递推关系式。 ddd数组...

11eyes 434

强化学习:从经典强化学习理解强化学习

1. 背景介绍 强化学习(Reinforcement Learning,RL)作为机器学习领域的重要分支,近年来取得了长足的进步。其核心思想是让智能体通过与环境的交互学习最优策略,从而最大化累积奖励。不同于监督学习和非监督学习,强化学习无需预先标注数据,而是通过试错的方式逐步学习,具有更强的适应性和泛化能力。

AI架构师小马 1804

计算机毕设项目十四:美团酒店评论情感分析完整数据代码-可直接运行

计算机毕设项目十四:美团酒店评论情感分析完整数据代码-可直接运行

693

Accumulation Degree 【POJ - 3585】

题目链接 题目大意 找出一个点,从这个点出发到所有叶子节点可以流经的权值最大,输出这个最大的权值 解题思路 这个题就是跑两遍dfs 第一遍dfs的时候我们处理出以这个节点为根所有的子树中,最大的流量我们用zi[x]来表示。那么zi[] 的转移状态可以这样写 if(du[v]==1) zi[u]+=e[i].w; else zi[u]+=min(zi[v],e[i].w)...

qq_41925919的博客 436

YOLOv8架构革命:通过EfficientViT融合实现精度-速度的帕累托突破

目标检测领域长期存在精度与速度的权衡难题。视觉Transformer(ViT)的引入解决了长距离依赖问题,但其巨大的计算复杂度制约了实时应用。最新研究表明,将轻量级ViT与YOLO架构融合,可在几乎不损失推理速度的前提下,将mAP提升3-8个百分点。这种架构改进方案已在工业检测、自动驾驶、安防监控等多个领域得到验证,在保持实时性的同时显著提升了复杂场景下的检测可靠性。通过模块化设计,可以灵活适配不同规模的YOLOv8模型,为实际应用提供精度-速度的帕累托最优解。

FJN110的博客 148

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

写一篇题解,以纪念调了一个小时的经历(就是因为边的数组没有乘2 phhhh QAQ) 题目 题目大意:找一个点使得从这个点出发作为源点,流出的流量最大,输出这个最大的流量。 以这道题来介绍二次扫描和换根法 作为一道不定根的树形DP,如果直接对每个点进行DP,可能时间会炸掉 但是,优秀的二次换根和扫描法可以再O(n^2)内解决问题。 二次扫描的含义:(来自lyd 算法竞赛...

172

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

POJ 3585 Accumulation Degree(二次扫描+换根)

目录题目题解题解一code1题解2code2 题目 http://poj.org/problem?id=3585 题目大意:给一棵无根树,求出最大流量 题解 题解一 暴力做法枚举源点sss,让sss作为整棵树的根,然后就变成了树形DPDPDP,因为答案的取得与子树有关 设f[x]f[x]f[x]表示已xxx为根,把xxx作为源点,从xxx向子树出发所获得的最大流量。 设degree[i]degre...

MILLOPE的博客 572

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

POJ - 3585 Accumulation Degree 树形DP 二次扫描 与 换根法

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

hutwuguangrong的博客 300

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

poj 3585 Accumulation Degree【树形DP二次扫描换根法】

题目链接:http://poj.org/problem?id=3585; /* 题意:找一个点使得,使得从这个点出发作为源点,发出的流量最大,输出这个最大的流量。 思路:设pd[x]为以x为根的子树中,把x作为源点,从x发出的流量的最大值,dfs一次得到。 转移方程: pd[x]+= in[y]==1?w :min(pd[y],w) (x的所有儿子) ...

几许情愁的博客 263

poj 3585 Accumulation Degree(二次扫描和换根法)

Accumulation Degree 大致题意:有一棵流量树,它的每一条边都有一个正流量,树上所有度数为一的节点都是出口,相应的树上每一个节点都有一个权值,它表示从这个节点向其他出口可以输送的最大总流量。我们的任务就是求这个最大总流量。 \(solution:\) 这一道题需要仔细思考其性质,我们发现如果我们把某一个节点当做是这棵树的根,并求出了这一个点的权值,那么与它相连的节点我们也可以求...

weixin_30405421的博客 172

POJ 3585 Accumulation Degree 树最大流(树形dp+二次扫描与换根法)

题意:给出一棵树,树上的边都有其权值,让我们求一个点能往外流的最大流量(会受到其它边权容量的限制)。 分析:二次扫描与换根法模板题,详见《算法竞赛进阶指南》P292-295,讲的很详细,一看就懂。 代码: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm...

常胜将军的博客 370

[POJ 3585] Accumulation Degree

[题目链接] http://poj.org/problem?id=3585 [算法] 树形DP——二次扫描与换根法 [代码] #include <algorithm> #include <bitset> #include <cctype> #include &lt...

123

POJ 3585 Accumulation Degree 树形dp

题目链接 Accumulation Degree Time Limit:5000MS Memory Limit:65536K Total Submissions:5388 Accepted:1309 Description Trees are an important component of the natural landscape bec...

Luowaterbi的博客 253

poj3585 Accumulation Degree (树形dp二次扫描换根)

问题描述 有一个树形的水系,由 N-1 条河道和 N 个交叉点组成。 我们可以把交叉点看作树中的节点,编号为 1~N,河道则看作树中的无向边。 每条河道都有一个容量,连接 x 与 y 的河道的容量记为 c(x,y)。 河道中单位时间流过的水量不能超过河道的容量。 有一个节点是整个水系的发源地,可以源源不断地流出水,我们称之为源点。 除了源点之外,树中所有度数为 1 的节点都是入海口,可以吸收无限多...

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 237

POJ 3585 :Accumulation Degree (树形DP + 两次扫描换根法)

题目大意:给你一棵树,每条边有一个最大容量,根结点可以流出无限的流,叶子结点可以接收无限多的流,问你以哪一个点为原点整棵树的总容量最大。 其实就是在问以那个点为原点最大流最大,数据很大肯定不是用最大流算法来跑 n 遍,因为整体结构是一棵树,容易想到两次DFS处理出每一个点作为根节点的最大流,再找答案。 (所谓两次扫描换根法就是两次DFS扫描得到每一个点作为根结点的解,了解之后发现就是自己用过的东...

qq_41997978的博客 409

POJ - 3585 Accumulation Degree 树形DP + 二次扫描 + 换根法 + 最大流问题

题目描述 给定n个点, n-1条边, 边上有权值代表流量, 与网络流最大流问题相似 问以任意一点为起点, 全图的最大流量是多少 输入包括多组数据 样例 输入样例: 1 5 1 2 11 1 4 13 3 4 5 4 5 10 输出样例: 26 样例分析        5个点, 4条边。当以4为起点时, 得到最大流量为26。...

正月看雪花的博客 454

Accumulation Degree (POJ - 3585 ,树形 DP 二次扫描与换根法)

一.题目链接: POJ-3585 二.题目大意: 给出一颗无根树,每条边有权值 w[i] 当选择一点为根时,从该点灌水,要求每条路上的水都不能溢出 现可以任意一点为根,求最大灌水量. 三.分析: 好像是个模板题.... 大佬的题解 四.代码实现: #include <cstdio> #include <algorithm> using namespac...

The___Flash的博客 353

POJ 3585 Accumulation Degree

二次扫描与换根法 用于解决无根树,对于每一个节点作为根时都要统计 做法: 1.先以任意一个节点为根,做树形DP,保存每个节点的DP值 2.然后自上而下dfs,对于每个节点考虑以他为根的最大值 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm&g...

123
上一篇: 最小乘积(基本型) 贪心
下一篇: Yet Another Game of Stones ZOJ - 3964 (博弈)
coldfresh
博客等级 码龄10年 107粉丝 405原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值