POJ 3585 Accumulation Degree
二次扫描与换根法
用于解决无根树,对于每一个节点作为根时都要统计
做法:
1.先以任意一个节点为根,做树形DP,保存每个节点的DP值
2.然后自上而下dfs,对于每个节点考虑以他为根的最大值
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 200505;
int init() {
int rv = 0, fh = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') fh = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
rv = (rv<<1) + (rv<<3) + c -'0';
c = getchar();
}
return fh * rv;
}
int T, n, f[MAXN], c[MAXN], degree[MAXN], head[MAXN], nume, rot;
bool fff[MAXN];
struct edge{
int to, nxt, flow;
}e[MAXN << 1];
void adde(int from, int to, int flow) {
e[++nume].to = to;
e[nume].flow = flow;
e[nume].nxt = head[from];
head[from] = nume;
}
void dfs1(int u) {
fff[u] = 1;
for(int i = head[u]; i ; i = e[i].nxt) {
int v = e[i].to;
if(fff[v]) continue;
if(degree[v] == 1) {f[u] += e[i].flow;}
else {dfs1(v); f[u] += min(f[v], e[i].flow);}
}
}
void dfs2(int u) {
fff[u] = 1;
for(int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if(fff[v]) continue;
c[v] = f[v];
if(degree[u] == 1) c[v] += e[i].flow;
else {
c[v] += min(c[u] - min(f[u], e[i].flow), e[i].flow);
}
dfs2(v);
}
}
int main() {
T = init();
while(T--) {
n = init();
memset(fff, 0, sizeof(fff));
memset(f, 0, sizeof(f));
memset(c, 0, sizeof(c));
memset(degree, 0, sizeof(degree));
memset(head, 0, sizeof(head));
memset(e, 0, sizeof(e));
nume = 0;
for(int i = 1; i < n; i++) {
int u = init(), v = init(), flow = init();
adde(u, v, flow); adde(v, u, flow);
degree[u]++; degree[v]++;
}
rot = 1;
dfs1(rot);
c[rot] = f[rot];
memset(fff, 0, sizeof(fff));
dfs2(rot);
int ans = 0;
for(int i = 1;i <= n; i++) ans = max(ans, c[i]);
cout << ans << endl;
}
return 0;
}
POJ 3585 Accumulation Degree的更多相关文章
- poj 3585 Accumulation Degree(二次扫描和换根法)
Accumulation Degree 大致题意:有一棵流量树,它的每一条边都有一个正流量,树上所有度数为一的节点都是出口,相应的树上每一个节点都有一个权值,它表示从这个节点向其他出口可以输送的最大总 ...
- POJ 3585 Accumulation Degree【换根DP】
传送门:http://poj.org/problem?id=3585 题意:给定一张无根图,给定每条边的容量,随便取一点使得从这个点出发作为源点,发出的流量最大,并且输出这个最大的流量. 思路:最近开 ...
- POJ 3585 Accumulation Degree 题解
题面 一句话题意:找一个点使得,使得从这个点出发作为源点,发出的流量最大,输出这个最大的流量 这道题是换根法+二次扫描的模板: 首先若确定1为原点,那么可以写出dp方程:当v的度是1时, g[u]+= ...
- POJ3585:Accumulation Degree(换根树形dp)
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3425 Accepted: 85 ...
- poj3585 Accumulation Degree【树形DP】【最大流】
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions:3151 Accepted: 783 ...
- POJ3585 Accumulation Degree(二次扫描与换根法)
题目:http://poj.org/problem?id=3585 很容易想出暴力.那么就先扫一遍. 然后得到了指定一个根后每个点的子树值. 怎么转化利用一下呢?要是能找出当前点的父亲的 “ 不含当前 ...
- poj3585 Accumulation Degree[树形DP换根]
思路其实非常简单,借用一下最大流求法即可...默认以1为根时,$f[x]$表示以$x$为根的子树最大流.转移的话分两种情况,一种由叶子转移,一种由正常孩子转移,判断一下即可.换根的时候由頂向下递推转移 ...
- Accumulation Degree
#include<cstdio> #include<cstring> #define INF 0x7fffffff using namespace std; ; inline ...
- POJ - 3585 树上最大流 换根法
题意:给出一棵树,边上有容量限制,求以任一点作为根和源点,叶子作为汇点的最大流的最大值 首先上网络流等于找死 树形DP可以\(O(n)\)求出以某点\(u\)为根的最大流,只需设\(f[u]=\sum ...
随机推荐
- maven打包错误:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.118 sec <<< FAILURE! - in ...
- 在hibernate框架中配置显示sql语句
使用Hibernate的框架开发时,可在Hibernate.cfg.xml中加上 <property name="hibernate.show_sql">true< ...
- Bootstrap历练实例:危险样式按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- git 的.gitignore
# vs code .vscode # Logs *.log #npm node_modules
- js 一维数组转成tree 对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- CF547D Mike and Fish 建图
题意: 有点长→CF547DMike and Fish. 分析: 其实也没什么好分析的,我这也是看的题解. (不过,那篇题解好像文字的代码不太对劲) 这里直接说做法,正确性自证: 对输入的,将横.纵坐 ...
- Mysql的慢日志
一.开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. 二.慢日志参数: slow_query_log 慢查询开启状态slow_qu ...
- perl学习之FLOCK函数的调用(讲的非常好)
一段演示flock系统调用的perl程序http://www.extmail.org/forum/viewthread.php?tid=1066
- perl学习之正则表达式
9 Perl 中的正则表达式 正则表达式的三种形式 正则表达式中的常用模式 正则表达式的 8 大原则 正则表达式是 Perl 语言的一大特色,也是 Perl 程序中的一点难点,不过如果大家能够很 ...
- Python把类当做字典来访问
定义一个类将它实例化,我们可以通过obj.属性来访问类的属性,如果想获取类的所有实例变量,我们可以使用obj.__dict__来访问,如下: class A: def __init__(self): ...