#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. OpenCV__elemSize

    elemSize 矩阵一个元素占用的字节数,例如:type是CV_16SC3,那么elemSize = 3 * 16 / 8 = 6 bytes elemSize1 矩阵元素一个通道占用的字节数,例如 ...

  2. FJUTOJ-周赛2016-11-25

    注:fjutoj基本每周都有一次周赛,欢迎大家都来参加! 网址:http://59.77.139.92/ 或 acm.fjut.edu.cn A题 题意:一年中,每个月有可能亏x 元,有可能赚y 元, ...

  3. TsinsenA1221 大楼【矩阵快速幂】

    题目分析: 重新定义矩阵运算,$*$等价于$+$,$+$等价于$max$. 然后倍增一下,再二分一下. 代码: #include<bits/stdc++.h> using namespac ...

  4. bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2816 题面: http://www.lydsy.com/JudgeOnline/upload ...

  5. jenkins拉源码设置参数化构建选项为tagname

    安装插件:https://mirrors.tuna.tsinghua.edu.cn/jenkins/plugins/jquery/1.12.4-0/jquery.hpi 安装插件:https://mi ...

  6. 降维【PCA & SVD】

    PCA(principle component analysis)主成分分析 理论依据 最大方差理论 最小平方误差理论 一.最大方差理论(白面机器学习) 对一个矩阵进行降维,我们希望降维之后的每一维数 ...

  7. 版本控制工具之git

    git存储区域详解 命令快速总结 初始化 git init 当前文件夹初始化 代码提交 git add file/. 自动检测工作区修改的内容提交到暂存区 git status 查看当前文件夹工作区的 ...

  8. JDBC事务管理

    JDBC事务管理 概念回顾: 事务:一个包含多个步骤的业务操作,如果这个业务操作被事务管理,那么这个业务操作的多个步骤要么同时失败,要么同时成功 事务操作: 开启事务 提交事务 回滚事务 使用Conn ...

  9. free命令查看内存信息

    free介绍 FREE(1) Linux User’s Manual FREE(1) NAME free - Display amount of free and used memory in the ...

  10. [物理学与PDEs]第1章第8节 静电场和静磁场 8.2 稳定电流的电场

    1. 此时, Maxwell 方程组为 $$\beex \bea \Div{\bf D}&=\rho_f,\\ \rot {\bf E}&={\bf 0},\\ \Div{\bf B} ...