传送门

换根dp板子题(板子型选手

题意:

  一棵树确定源点和汇点找到最大的流量(拿出一整套最大瘤板子orz

const int maxn=2e5+;
int head[maxn],tot;
struct node
{
int nt,to;long long w;
}q[*maxn];
long long dp[maxn];int cnt[maxn];
void insert(int u,int v,long long w)
{
q[tot].nt=head[u];q[tot].w=w;q[tot].to=v;head[u]=tot++;
q[tot].nt=head[v];q[tot].w=w;q[tot].to=u;head[v]=tot++;
}
long long ans;
void dfs(int u,int fa)
{
//cout<<u<<endl;
for(int i=head[u];i!=-;i=q[i].nt){
int v=q[i].to;long long w=q[i].w;
if(v==fa) continue;
dfs(v,u);
if(cnt[v]==)
dp[u]+=w;
else
dp[u]+=min(w,dp[v]);
//cout<<u<<" "<<dp[u]<<endl;
}
}
void dfs1(int u,int fa)
{
//cout<<u<<" "<<dp[u]<<endl;
ans=max(ans,dp[u]);
for(int i=head[u];i!=-;i=q[i].nt){
int v=q[i].to;long long w=q[i].w;
if(v==fa) continue;
dp[v]+=min(w,dp[u]-min(dp[v],w));
dfs1(v,u);
}
}
int main()
{
int t;scanf("%d",&t);
while(t--){
int n;scanf("%d",&n);ans=;
for(int i=;i<=n;i++) head[i]=-,cnt[i]=,dp[i]=;tot=;
for(int i=;i<n;i++){
int t1,t2;long long t3;scanf("%d%d%lld",&t1,&t2,&t3);
insert(t1,t2,t3);cnt[t1]++;cnt[t2]++;
}
dfs(,);
dfs1(,);
cout<<ans<<endl;
}
}

poj3585 Accumulation Degree(换根dp)的更多相关文章

  1. POJ3585:Accumulation Degree(换根树形dp)

    Accumulation Degree Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3425   Accepted: 85 ...

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

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

  3. poj3585 Accumulation Degree【树形DP】【最大流】

    Accumulation Degree Time Limit: 5000MS   Memory Limit: 65536K Total Submissions:3151   Accepted: 783 ...

  4. POJ3585 Accumulation Degree 【树形dp】

    题目链接 POJ3585 题解 -二次扫描与换根法- 对于这样一个无根树的树形dp 我们先任选一根进行一次树形dp 然后再扫一遍通过计算得出每个点为根时的答案 #include<iostream ...

  5. POJ 3585 Accumulation Degree【换根DP】

    传送门:http://poj.org/problem?id=3585 题意:给定一张无根图,给定每条边的容量,随便取一点使得从这个点出发作为源点,发出的流量最大,并且输出这个最大的流量. 思路:最近开 ...

  6. [算法学习] 换根dp

    换根dp 一般来说,我们做题的树都是默认 \(1\) 为根的.但是有些题目需要计算以每个节点为根时的内容. 朴素的暴力:以每个点 \(u\) 作为 \(root\) 暴力dfs下去,复杂度\(O(n^ ...

  7. [BZOJ4379][POI2015]Modernizacja autostrady[树的直径+换根dp]

    题意 给定一棵 \(n\) 个节点的树,可以断掉一条边再连接任意两个点,询问新构成的树的直径的最小和最大值. \(n\leq 5\times 10^5\) . 分析 记断掉一条边之后两棵树的直径为 \ ...

  8. 2018.10.15 NOIP训练 水流成河(换根dp)

    传送门 换根dp入门题. 貌似李煜东的书上讲过? 不记得了. 先推出以1为根时的答案. 然后考虑向儿子转移. 我们记f[p]f[p]f[p]表示原树中以ppp为根的子树的答案. g[p]g[p]g[p ...

  9. 换根DP+树的直径【洛谷P3761】 [TJOI2017]城市

    P3761 [TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公 ...

随机推荐

  1. flex 居中

    display: flex; justify-content: space-between; align-items: center;

  2. 【Android】Retrofit源码学习

    使用Retrofit的流程 通过Builder创建Retrofit对象: Retrofit retrofit = new Retrofit.Builder().baseUrl("" ...

  3. Linux - 文件的三种时间之atime、ctime、mtime的区别和简单用法

    在Linux中,文件或者目录中有三个时间属性 atime ctime mtime 简名 全名 中文 作用 atime Access Time 访问时间 最后一次访问文件(读取或执行)的时间 ctime ...

  4. 为何Keras中的CNN是有问题的,如何修复它们?

    在训练了 50 个 epoch 之后,本文作者惊讶地发现模型什么都没学到,于是开始深挖背后的问题,并最终从恺明大神论文中得到的知识解决了问题. 上个星期我做了一些实验,用了在 CIFAR10 数据集上 ...

  5. macbook中出现2003 - Can't connect to MySQL server on '127.0.0.1' (61 "Connection refused") 如何解决

    第一步 关闭mysql服务: 苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务(点击stop mysql server) 如果这种方法没有成功: 可以使用命令行 ...

  6. 基于.NetCore3.1搭建项目系列 —— 使用Swagger导出文档 (番外篇)

    前言 回顾之前的两篇Swagger做Api接口文档,我们大体上学会了如何在net core3.1的项目基础上,搭建一套自动生产API接口说明文档的框架. 本来在Swagger的基础上,前后端开发人员在 ...

  7. kerberos系列之flink认证配置

    大数据安全系列的其它文章 https://www.cnblogs.com/bainianminguo/p/12548076.html-----------安装kerberos https://www. ...

  8. java.lang.NoSuchMethodException: java.util.List.<init>()

    报错信息如下 java.lang.NoSuchMethodException: java.util.List.<init>() at java.lang.Class.getConstruc ...

  9. Java 数组 字符 函数

    一. 1. package Hello; import java.util.Scanner; public class hello_test { public static void main(Str ...

  10. Java是未来的第一编程语言吗?

    目录 一.前言 二.Java帝国的今天 2.1 依然霸占TIOBE热门编程语言的榜首 2.2 曾经想扼杀Java的微软宣布加入OpenJDK 2.3 Oracle发布开源全栈虚拟机GraalVM 三. ...