#include<cstdio>
#include<cstring>
#define INF 0x7fffffff
using namespace std;
const int N=2e5+;
inline int min(int a,int b){
return (a<b?a:b);
}
int first[N],next[N*],to[N*],c[N*],n;
int edge_count=;
inline void add(int x,int y,int w){
edge_count++; to[edge_count]=y;
c[edge_count]=w; next[edge_count]=first[x];
first[x]=edge_count;
}
int f[N][];
long long ans=;
//f[i][1]->以i结点为根的子树 向下流动 的最大流量
//f[i][2]->以i节点为根的子树 向上流动 的最大流量
void search(int root,int fa){ if(to[ first[root] ]==fa){
//错因分析:本想判断root结点是否为叶节点,但是存在那样一个结点NODE s.t. first【root】->father
//so, it should be 【 if(to[ first[root] ]==fa && !next[ first[root] ]) 】
f[root][]=INF;
return;
}
for(int i=first[root];i;i=next[i]){
if(to[i]==fa)continue;
search(to[i],root);
f[root][]+=min(f[ to[i] ][],c[i]);
//printf("root:%d",root);printf(" %d\n",f[root][0]);
}
}
void dfs(int root,int fa){ for(int i=first[root];i;i=next[i]){
if(to[i]==fa){
f[root][]=min(c[i],f[fa][]+f[fa][]-min(c[i],f[root][]));
}
}
for(int i=first[root];i;i=next[i]){
if(to[i]==fa)continue;
dfs(to[i],root);
}
long long t=0ll;
if(f[root][]!=(INF>>) )t+=f[root][];
if(f[root][]!=INF)t+=f[root][];
if(ans<t)ans=t;
}
int aa;
int main()
{
//freopen("degree.in","r",stdin);
//freopen("degree.out","w",stdout);
scanf("%d",&aa);
for(int k=;k<=aa;k++){
memset(first,,sizeof(first));
memset(next,,sizeof(next));
memset(to,,sizeof(to));
memset(c,,sizeof(c));
memset(f,,sizeof(f));
ans=0ll;
edge_count=; scanf("%d",&n);
for(int i=,a,b,w;i<n;++i){
scanf("%d%d%d",&a,&b,&w);
add(a,b,w);
add(b,a,w);
}
search(,);
if(!next[ first[] ])f[][]=INF>>;
dfs(,);
printf("%lld\n",ans);
}
return ;
}

Accumulation Degree的更多相关文章

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

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

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

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

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

    Accumulation Degree 大致题意:有一棵流量树,它的每一条边都有一个正流量,树上所有度数为一的节点都是出口,相应的树上每一个节点都有一个权值,它表示从这个节点向其他出口可以输送的最大总 ...

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

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

  5. POJ 3585 Accumulation Degree

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

  6. 【POJ3585】Accumulation Degree 二次扫描与换根法

    简单来说,这是一道树形结构上的最大流问题. 朴素的解法是可以以每个节点为源点,单独进行一次dp,时间复杂度是\(O(n^2)\) 但是在朴素求解的过程中,相当于每次都求解了一次整棵树的信息,会做了不少 ...

  7. POJ3585 Accumulation Degree(二次扫描与换根法)

    题目:http://poj.org/problem?id=3585 很容易想出暴力.那么就先扫一遍. 然后得到了指定一个根后每个点的子树值. 怎么转化利用一下呢?要是能找出当前点的父亲的 “ 不含当前 ...

  8. [POJ3585]Accumulation Degree

    题面 \(\text{Solution:}\) 有些题目不仅让我们做树型 \(\text{dp}\) ,而且还让我们换每个根分别做一次, 然后这样就愉快的 \(\text{TLE}\) 了,所以我们要 ...

  9. POJ3585 Accumulation Degree 【树形dp】

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

随机推荐

  1. jsp中【<%=request.getContextPath()%>】项目路径

    1 2 "request.getContextPath()的值是        "<%=request.getContextPath()%><br/> &q ...

  2. 在IIS上搭建WebSocket服务器(一)

    一.搭建环境 1.System.Web.WebSockets需搭建在Windows8及Server2012以上系统的上. 2.在Windows8及Server2012以上系统的上安装IIS和WebSo ...

  3. python 基本数据类型以及运算符操作

    一.基本数据类型 为何要区分类型? 数据类型的值是变量值得类型,变量值之所以区分类型,是因为变量的值 用来记录事物的状态,而事物的状态有不同的种类,对应着,也必须用不 用类型去区分它们. 1.数字类型 ...

  4. vue 拖拽移动(类似于iPhone虚拟home )

    vue 移动端 PC 兼容 元素 拖拽移动  效果演示 事件知识点 移动端 PC端 注释 touchstart mousedown 鼠标/手指按下事件 touchmove mousemove 鼠标/手 ...

  5. Django 序列化

    序列化 背景 对于Django 的queryset 对象在传递给 前端的时候,前端是无法识别的 因此需要存在一个转换过程将 queryset 对象转换成 字符串前端才可以识别 演示 QuerySet ...

  6. codeforces-1138 (div2)

    想法题是硬伤,面对卡题和卡bug的情况应对能力太差 A.求两个前缀和以及两个后缀和,相邻最小值的最大值. #include<iostream> using namespace std; ; ...

  7. 2017-12-19python全栈9期第四天第一节之昨日内容回顾与作业讲解之插入insert和extend

    #!/user/bin/python# -*- coding:utf-8 -*-li = ['zs','ls','ww','zl']li.insert(4,'cc')print(li)li.exten ...

  8. MDK填充FLASH为0xFF

    title: MDK填充FLASH为0xFF date: 2019/1/3 20:34:05 --- MDK填充FLASH为0xFF 参考 area 指定地址 https://blog.csdn.ne ...

  9. 使用C语言中qsort()函数对浮点型数组无法成功排序的问题

    一 写在开头 1.1 本节内容 本节主要内容是有关C语言中qsort()函数的探讨. 二 问题和相应解决方法 qsort()是C标准库中的一个通用的排序函数.它既能对整型数据进行排序也能对浮点型数据进 ...

  10. [物理学与PDEs]第1章第9节 Darwin 模型 9.1 拟静电模型及其修正形式

    1. 拟静电模型: 当 $\cfrac{\omega}{c}\ll \cfrac{1}{c}\lra \omega\ll \cfrac{c}{l}$ 时, $$\bex \cfrac{1}{c}\cf ...