题目链接

POJ3585

题解

-二次扫描与换根法-

对于这样一个无根树的树形dp

我们先任选一根进行一次树形dp

然后再扫一遍通过计算得出每个点为根时的答案

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define cls(s) memset(s,0,sizeof(s))
using namespace std;
const int maxn = 200005,maxm = 400005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int h[maxn],ne = 2,de[maxn];
struct EDGE{int to,nxt,w;}ed[maxm];
inline void build(int u,int v,int w){
ed[ne] = (EDGE){v,h[u],w}; h[u] = ne++;
ed[ne] = (EDGE){u,h[v],w}; h[v] = ne++;
de[u]++; de[v]++;
}
int n,fa[maxn];
int d[maxn],f[maxn],g[maxn],ans;
void dfs1(int u){
f[u] = 0;
if (de[u] == 1 && u != 1) return;
Redge(u) if ((to = ed[k].to) != fa[u]){
fa[to] = u; d[to] = ed[k].w; dfs1(to);
if (de[to] == 1) f[u] += ed[k].w;
else f[u] += min(ed[k].w,f[to]);
}
}
void dfs2(int u){
g[u] = f[u];
if (fa[u]){
if (de[fa[u]] == 1) g[u] += d[u];
else g[u] += min(d[u],g[fa[u]] - min(d[u],f[u]));
}
ans = max(ans,g[u]);
Redge(u) if ((to = ed[k].to) != fa[u]){
dfs2(to);
}
}
int main(){
int T = read();
while (T--){
ne = 2; cls(h); cls(de); ans = 0;
n = read(); int a,b,w;
for (int i = 1; i < n; i++){
a = read(); b = read(); w = read();
build(a,b,w);
}
dfs1(1);
//REP(i,n) printf("%lld ",f[i]); puts("");
dfs2(1);
//REP(i,n) printf("%lld ",g[i]); puts("");
printf("%d\n",ans);
}
return 0;
}

POJ3585 Accumulation Degree 【树形dp】的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. POJ3585 Accumulation Degree (树形DP-二次扫描与换根)

    本题属于不定根的树形DP,若以每个节点为根求解一次,复杂度太高,所以可以用换根的技巧. d[u]表示以u为根向下可以流的最大流量,这个是比较好求的,直接遍历到叶子节点,由子节点信息更新父节点.然后进行 ...

  8. POJ3585 Accumulation Degree【换根dp】

    题目传送门 题意 给出一棵树,树上的边都有容量,在树上任意选一个点作为根,使得往外流(到叶节点,叶节点可以接受无限多的流量)的流量最大. 分析 首先,还是从1号点工具人开始$dfs$,可以求出$dp[ ...

  9. poj3585 Accumulation Degree(换根dp)

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

随机推荐

  1. LeetCode-177:第N高的薪水

    第N高的薪水与第二高的薪水,解题思路是一样的,只要对LeetCode-176的SQL做一下变形,便可以满足这题,详见:https://www.cnblogs.com/zouqf/p/10282392. ...

  2. MySQL自增锁等待问题解决

    有网友再群里问:在做基准测试时候,批量插入数据时,有很多自增锁等待,我告诉他解决办法: 1.innodb_autoinc_lock_mode=2 2.innodb_autoextend_increme ...

  3. PHP实现的一个时间帮助类

    背景 工作的过程中经常会遇到各种时间类的操作,因此封装了一个帮助工具类,提高代码的复用率 主要功能 根据相差的天数获取连续的时间段 /** * 根据相差的天数获取所有连续的时间段 * @param $ ...

  4. juicer

    function financingBodyTable(){ var jsonData = { FinancingBodyJSON:${relaListArr}//list数组 } var addMo ...

  5. sql语句(Oracle和sqlserver)

    查询表的首句:(Oracle) select * from (select a.*, rownum as rn from tab_name a order by col )where rn = 1 o ...

  6. linux poll用法

    相对于select来说,poll 也是在指定时间内论询一定数量的文件描述符,来测试其中是否有就绪的,不过,poll 提供了一个易用的方法,来实现 i/o 复用. 声明如下: #include < ...

  7. 笔记-redis深入学习-1

    笔记-redis深入学习-1 redis的基本使用已经会了,但存储和读取只是数据库系统最基础的功能: 数据库系统还得为可靠实现这两者提供一系列保证: 数据.操作备份和恢复,主要是持久化: 高可用:主要 ...

  8. jdk1.8源码学习笔记

    前言: 前一段时间开始学习了一些基本的数据结构和算法,算是弥补了这方面的知识短板,但是仅仅是对一些算法的了解,目前工作当中也并没有应用到这些,因此希望通过结合实际例子来学习,巩固之前学到的内容,思前想 ...

  9. Oozie wordcount实战

    一.定义 基本概念 Action: An execution/computation task (Map-Reduce job, Pig job, a shell command). It can a ...

  10. Unity Occlusion Culling 遮挡剔除研究

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52684127 作者:car ...