poj3585 Accumulation Degree(换根dp)
传送门
换根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)的更多相关文章
- POJ3585:Accumulation Degree(换根树形dp)
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3425 Accepted: 85 ...
- 题解 poj3585 Accumulation Degree (树形dp)(二次扫描和换根法)
写一篇题解,以纪念调了一个小时的经历(就是因为边的数组没有乘2 phhhh QAQ) 题目 题目大意:找一个点使得从这个点出发作为源点,流出的流量最大,输出这个最大的流量. 以这道题来介绍二次扫描和换 ...
- poj3585 Accumulation Degree【树形DP】【最大流】
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions:3151 Accepted: 783 ...
- POJ3585 Accumulation Degree 【树形dp】
题目链接 POJ3585 题解 -二次扫描与换根法- 对于这样一个无根树的树形dp 我们先任选一根进行一次树形dp 然后再扫一遍通过计算得出每个点为根时的答案 #include<iostream ...
- POJ 3585 Accumulation Degree【换根DP】
传送门:http://poj.org/problem?id=3585 题意:给定一张无根图,给定每条边的容量,随便取一点使得从这个点出发作为源点,发出的流量最大,并且输出这个最大的流量. 思路:最近开 ...
- [算法学习] 换根dp
换根dp 一般来说,我们做题的树都是默认 \(1\) 为根的.但是有些题目需要计算以每个节点为根时的内容. 朴素的暴力:以每个点 \(u\) 作为 \(root\) 暴力dfs下去,复杂度\(O(n^ ...
- [BZOJ4379][POI2015]Modernizacja autostrady[树的直径+换根dp]
题意 给定一棵 \(n\) 个节点的树,可以断掉一条边再连接任意两个点,询问新构成的树的直径的最小和最大值. \(n\leq 5\times 10^5\) . 分析 记断掉一条边之后两棵树的直径为 \ ...
- 2018.10.15 NOIP训练 水流成河(换根dp)
传送门 换根dp入门题. 貌似李煜东的书上讲过? 不记得了. 先推出以1为根时的答案. 然后考虑向儿子转移. 我们记f[p]f[p]f[p]表示原树中以ppp为根的子树的答案. g[p]g[p]g[p ...
- 换根DP+树的直径【洛谷P3761】 [TJOI2017]城市
P3761 [TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公 ...
随机推荐
- 关于编码和解码问题——encode、decode
一.背景和问题 近期在做一个关于声卡录音的项目,开发环境是win10 64位家庭中文版,pycharm2019.1,python3.6(Anaconda3),python模块pyaud ...
- layui radio 单选框 效果 显示不来 解决方法
$("input[name=sex][value=女]").attr("checked", data.data.adminInfoEntity.adminInf ...
- 将SublimeText加入右键菜单
将SublimeText加入右键菜单 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\SublimeText] @=&q ...
- python基础知识8——常见内置模块
Python之路-python(常用模块学习) 模块介绍 time &datetime模块 random os sys shutil shelve xml处理 yaml处理 configpar ...
- rbac权限(2)
models.py from django.db import models class User(models.Model): name=models.CharField(max_length=32 ...
- 洛谷1363 幻象迷宫dfs
题目网址:https://www.luogu.com.cn/problem/P1363 迷宫是无限多块地图拼接而成的,问是否可以在迷宫中走无限远.解决方案是dfs,走出初始地图之后的位置映射到原位置( ...
- OpenLDAP 多主复制(基于docker容器模式部署)
**本文主要讲述在docker环境下如何进行 OpenLDAP 多主复制,至于 OpenLDAP 原理可以先参考这篇文章了解:https://cloud.tencent.com/developer/a ...
- 配置ssh免密登录遇到的问题——使用VMware多虚拟机搭建Hadoop集群
搭建环境: 虚拟机 VMware12Pro 操作系统 centos6.8 hadoop 1.2.1 1.导入镜像文件,添加java环境 1.查看当前系统中安装的java,ls ...
- 算法学习 八皇后问题的递归实现 java版 回溯思想
1.问题描述 八皇后问题是一个以国际象棋为背景的问题:如何能够在 8×8 的国际象棋棋盘上放置八个皇后,使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或 ...
- vue基础指令学习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...