二次扫描与换根法

用于解决无根树,对于每一个节点作为根时都要统计

做法:

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的更多相关文章

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

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

  2. POJ 3585 Accumulation Degree【换根DP】

    传送门:http://poj.org/problem?id=3585 题意:给定一张无根图,给定每条边的容量,随便取一点使得从这个点出发作为源点,发出的流量最大,并且输出这个最大的流量. 思路:最近开 ...

  3. POJ 3585 Accumulation Degree 题解

    题面 一句话题意:找一个点使得,使得从这个点出发作为源点,发出的流量最大,输出这个最大的流量 这道题是换根法+二次扫描的模板: 首先若确定1为原点,那么可以写出dp方程:当v的度是1时, g[u]+= ...

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

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

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

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

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

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

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

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

  8. Accumulation Degree

    #include<cstdio> #include<cstring> #define INF 0x7fffffff using namespace std; ; inline ...

  9. POJ - 3585 树上最大流 换根法

    题意:给出一棵树,边上有容量限制,求以任一点作为根和源点,叶子作为汇点的最大流的最大值 首先上网络流等于找死 树形DP可以\(O(n)\)求出以某点\(u\)为根的最大流,只需设\(f[u]=\sum ...

随机推荐

  1. 解决Starting to watch source with Jekyll and Compass. Starting Rack on port 4000

    问题 Starting to watch source with Jekyll and Compass. Starting Rack on port 4000 rake aborted! Errno: ...

  2. websocket 入门

    什么是websocket WebSocket是HTML5新增的协议,它的目的是在浏览器和服务器之间建立一个不受限的双向通信的通道,比如说,服务器可以在任意时刻发送消息给浏览器. 为什么会出现 webs ...

  3. url编码和解码平台

    http://meyerweb.com/eric/tools/dencoder/

  4. 响应式web设计视图工具及插件总结----20150113

    响应式web设计可以说火不火是迟早的,下面就对于最开始的视口调试的方法汇总,希望有好的方法大家一起交流. 1.火狐:从Firefox升级到29.0之后就不直接支持Firesizer了. 先安装Add- ...

  5. 文件读写FILE类

    1. 新建一个文件: FILE *f = fopen("a.txt","w+"); (1)fopen()函数介绍fopen的原型是:FILE *fopen(co ...

  6. layer的iframe层的传参和回参

    从父窗口传参给iframe,参考://https://yq.aliyun.com/ziliao/133150 从iframe回参给父窗口,参考:https://www.cnblogs.com/jiqi ...

  7. Git学习——从远程库克隆

    克隆一个本地库 首先准备好一个远程库.再用命令克隆一个本地库. git clone git@github.com:<github账户>/<远程库名>.git 克隆一个仓库,首先 ...

  8. GIMP模版的制作

    GIMP自定模版,方便自己作图 新建模版: 设置开机自动加载模版:

  9. 避免使用aireplay-ng指令时出现AP通道不对的方法

    本方法搜集网络:具体使用为在调试网卡为监听模式,使用airodump-ng指令扫描wifi后,需要先执行"airmon-ng stop wlan0"指令,然后再进行一系列抓包等操作 ...

  10. python动态添加属性和方法

    ---恢复内容开始--- python动态添加属性: class Person(object): def __init__(self,newName,newAge): self.name = newN ...