Accumulation Degree
#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的更多相关文章
- poj3585 Accumulation Degree【树形DP】【最大流】
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions:3151 Accepted: 783 ...
- POJ3585:Accumulation Degree(换根树形dp)
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3425 Accepted: 85 ...
- poj 3585 Accumulation Degree(二次扫描和换根法)
Accumulation Degree 大致题意:有一棵流量树,它的每一条边都有一个正流量,树上所有度数为一的节点都是出口,相应的树上每一个节点都有一个权值,它表示从这个节点向其他出口可以输送的最大总 ...
- poj3585 Accumulation Degree[树形DP换根]
思路其实非常简单,借用一下最大流求法即可...默认以1为根时,$f[x]$表示以$x$为根的子树最大流.转移的话分两种情况,一种由叶子转移,一种由正常孩子转移,判断一下即可.换根的时候由頂向下递推转移 ...
- POJ 3585 Accumulation Degree
二次扫描与换根法 用于解决无根树,对于每一个节点作为根时都要统计 做法: 1.先以任意一个节点为根,做树形DP,保存每个节点的DP值 2.然后自上而下dfs,对于每个节点考虑以他为根的最大值 #inc ...
- 【POJ3585】Accumulation Degree 二次扫描与换根法
简单来说,这是一道树形结构上的最大流问题. 朴素的解法是可以以每个节点为源点,单独进行一次dp,时间复杂度是\(O(n^2)\) 但是在朴素求解的过程中,相当于每次都求解了一次整棵树的信息,会做了不少 ...
- POJ3585 Accumulation Degree(二次扫描与换根法)
题目:http://poj.org/problem?id=3585 很容易想出暴力.那么就先扫一遍. 然后得到了指定一个根后每个点的子树值. 怎么转化利用一下呢?要是能找出当前点的父亲的 “ 不含当前 ...
- [POJ3585]Accumulation Degree
题面 \(\text{Solution:}\) 有些题目不仅让我们做树型 \(\text{dp}\) ,而且还让我们换每个根分别做一次, 然后这样就愉快的 \(\text{TLE}\) 了,所以我们要 ...
- POJ3585 Accumulation Degree 【树形dp】
题目链接 POJ3585 题解 -二次扫描与换根法- 对于这样一个无根树的树形dp 我们先任选一根进行一次树形dp 然后再扫一遍通过计算得出每个点为根时的答案 #include<iostream ...
随机推荐
- 【NLP】Conditional Language Models
Language Model estimates the probs that the sequences of words can be a sentence said by a human. Tr ...
- LoadRunner【第四篇】参数化
参数化的定义及使用场景 定义:将脚本中的特定值用变量替代,该变量值是变化的(注意:这个值是我们自己创建的,不是服务器返回的). 使用参数化: 1.业务考虑,不允许相同信息 2.真实模拟不同用户 3.真 ...
- Linux交换分区使用过多的处理办法
处理办法 echo "vm.swappiness=0" >>/etc/sysctl.conf sysctl -p swapoff -a && swapo ...
- Java多线程编程-线程之间的通信
转载自:这里 学习了基础的线程知识 看到了 线程之间的通信 线程之间有哪些通信方式呢? 1.同步 这里讲的同步是指多个线程通过synchronized关键字这种方式来实现线程间的通信. public ...
- BZOJ2870 最长道路
题意:给定树,有点权.求一条路径使得最小点权 * 总点数最大.只需输出这个最大值.5w. 解:树上路径问题,点分治. 考虑合并两个子树的时候,答案的形式是val1 * (d1 + d2),当1是新插入 ...
- java.io.FileNotFoundException:my-release-key.keyStore拒绝访问
安卓生成APK的时候,生成密钥的时候报java.io.FileNotFoundException:my-release-key.keyStore拒绝访问的错误 这是因为权限问题:你的jdk目录在c盘, ...
- 使用mysqlbinlog对主库binlog进行同步
#!/bin/bash BASEDIR="/usr/local/mysql" BIN="$BASEDIR/bin" MYSQLBINLOG="$BIN ...
- JDBC 关闭数据库连接与自动提交【转】
// Jdbc关闭数据库连接时,会隐含一个提交事务的操作 private final static String DB_DRIVER = "oracle.jdbc.driver.Oracle ...
- NOI-OJ 2.2 ID:6261 汉诺塔
思路 汉诺塔是递归思想最经典的例子,通过递归不断缩小问题,将n个盘子的问题简化n-1个,直至1个. 三个盘子,分别为A:from,B:to,C:by(A为起点盘,B为目标盘,C为中转盘) 过程 将1- ...
- 【转载】 C++之split字符串分割
https://blog.csdn.net/mary19920410/article/details/77372828