题面

\(\text{Solution:}\)

有些题目不仅让我们做树型 \(\text{dp}\) ,而且还让我们换每个根分别做一次, 然后这样就愉快的 \(\text{TLE}\) 了,所以我们要用一种方法快速知道所有根的答案。

二次扫描与换根法:

就是先选任意点作根做一遍 \(\text{dp}\) ,求出相关信息,然后再从根往下 \(\text{dfs}\) ,对每一个节点往下走之前进行自顶向下的推导,计算出 "换根" 后的解。

就这题而言就是用父亲的换根后的答案来跟新自己换根前的答案,一般是 换根后父亲的答案+自己换根前的答案-自己对父亲换根后的贡献

#include <set>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <assert.h>
#include <algorithm> using namespace std; #define fir first
#define sec second
#define pb push_back
#define mp make_pair
#define LL long long
#define INF (0x3f3f3f3f)
#define mem(a, b) memset(a, b, sizeof (a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(x) cout << #x << " = " << x << endl
#define travle(i, x) for (register int i = head[x]; i; i = nxt[i])
#define For(i, a, b) for (register int (i) = (a); (i) <= (b); ++ (i))
#define Forr(i, a, b) for (register int (i) = (a); (i) >= (b); -- (i))
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define ____ debug("go\n") namespace io {
static char buf[1<<21], *pos = buf, *end = buf;
inline char getc()
{ return pos == end && (end = (pos = buf) + fread(buf, 1, 1<<21, stdin), pos == end) ? EOF : *pos ++; }
inline int rint() {
register int x = 0, f = 1;register char c;
while (!isdigit(c = getc())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getc()));
return x * f;
}
inline LL rLL() {
register LL x = 0, f = 1; register char c;
while (!isdigit(c = getc())) if (c == '-') f = -1;
while (x = (x << 1ll) + (x << 3ll) + (c ^ 48), isdigit(c = getc()));
return x * f;
}
inline void rstr(char *str) {
while (isspace(*str = getc()));
while (!isspace(*++str = getc()))
if (*str == EOF) break;
*str = '\0';
}
template<typename T>
inline bool chkmin(T &x, T y) { return x > y ? (x = y, 1) : 0; }
template<typename T>
inline bool chkmax(T &x, T y) { return x < y ? (x = y, 1) : 0; }
}
using namespace io; const int N = 2e5 + 2; int n, T;
int tot, head[N], ver[N<<1], nxt[N<<1], edge[N<<1];
int D[N], F[N], in[N]; void add(int u, int v, int w)
{ ver[++tot] = v, edge[tot] = w, nxt[tot] = head[u], head[u] = tot; } void DFS(int u, int f)
{
D[u] = 0;
for (int i = head[u]; i; i = nxt[i])
if (ver[i] != f)
{
DFS(ver[i], u);
int v = ver[i];
if (in[v] == 1) D[u] += edge[i];
else D[u] += min(D[v], edge[i]);
}
} void DP(int u, int f)
{
for (int i = head[u]; i; i = nxt[i])
if (ver[i] != f)
{
int v = ver[i];
if (in[u] == 1) F[v] = D[v] + edge[i];
else F[v] = D[v] + min(F[u] - min(edge[i], D[v]), edge[i]);
DP(v, u);
}
} int main() { T = rint();
while (T --)
{
tot = 0; mem(head, 0); mem(in, 0); mem(D, 0); mem(F, 0); n = rint();
for (int i = 1; i < n; ++ i)
{
int u = rint(), v = rint(), w = rint();
add(u, v, w);
add(v, u, w);
in[u] ++, in[v] ++;
} DFS(1, 0);
F[1] = D[1];
DP(1, 0);
for (int i = 1; i <= n; ++ i)
chkmax(F[1], F[i]);
printf("%d\n", F[1]);
}
}

[POJ3585]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. POJ3585 Accumulation Degree 【树形dp】

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

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

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

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

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

  6. 题解 poj3585 Accumulation Degree (树形dp)(二次扫描和换根法)

    写一篇题解,以纪念调了一个小时的经历(就是因为边的数组没有乘2 phhhh QAQ) 题目 题目大意:找一个点使得从这个点出发作为源点,流出的流量最大,输出这个最大的流量. 以这道题来介绍二次扫描和换 ...

  7. POJ3585 Accumulation Degree【换根dp】

    题目传送门 题意 给出一棵树,树上的边都有容量,在树上任意选一个点作为根,使得往外流(到叶节点,叶节点可以接受无限多的流量)的流量最大. 分析 首先,还是从1号点工具人开始$dfs$,可以求出$dp[ ...

  8. $Poj3585\ Accumulation Degree$ 树形$DP/$二次扫描与换根法

    Poj Description 有一个树形的水系,由n-1条河道与n个交叉点组成.每条河道有一个容量,联结x与y的河道容量记为c(x,y),河道的单位时间水量不能超过它的容量.有一个结点是整个水系的发 ...

  9. poj3585 Accumulation Degree(换根dp)

    传送门 换根dp板子题(板子型选手 题意: 一棵树确定源点和汇点找到最大的流量(拿出一整套最大瘤板子orz ; int head[maxn],tot; struct node { int nt,to; ...

随机推荐

  1. HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用)

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. centos安装nodejs二进制包

    1.下载nodejs的二进制包 wget https://nodejs.org/dist/v6.3.1/node-v6.3.1-linux-x64.tar.xz 2.解压下载的安装包 tar xf n ...

  3. 百度webAPI配合微信JSDK获取用户当前位子

    逻辑: ①通过微信JS-SDK 获取地理位置接口 获取经纬度 ②调用百度地图转换经纬度的API,得到百度地图的经纬度 ③调用百度地图 正/逆地址编码服务-->国际化逆地理编码 得到JSON数据, ...

  4. 简单实用的.htaccess文件配置

    .htaccess 文件 (Hypertext Access file) 是Apache Web服务器的一个非常强大的配置文件,对于这个文件,Apache有一堆参数可以让你配置出几乎随心所欲的功能.. ...

  5. 复制功能 js

    示例: <input class="herf" type="text" v-model="herfUrl" readonly=&quo ...

  6. oracle权限配置

    系统权限管理:1.系统权限分类:DBA: 拥有全部特权,是系统最高权限,只有DBA才可以创建数据库结构. RESOURCE:拥有Resource权限的用户只可以创建实体,不可以创建数据库结构. CON ...

  7. iOS Xcode 小技巧,提升理解查询能力,Command + 点击鼠标右键 Jump to Definition等

    前言: 介绍下Xcode 小技巧,以及一下快捷键,让你调试程序更加出类拔萃,安排! Command + 点击鼠标右键 Jump to Definition,可能你平时也在用,但是你明白全部的用法吗,试 ...

  8. c++后台开发 准备材料

    后台开发知识点 面面俱到很难,一个领域钻研的很深也很难.我认识的大神里有把C++语言吃的非常透的,也有实验室就是搞分布式的,拿offer都非常轻松. 博客(C++后台/基础架构) http://www ...

  9. Jensen 不等式

    若f(x)为区间I上的下凸(上凸)函数,则对于任意xi∈I和满足∑λi=1的λi>0(i=1,2,...,n),成立: \[f(\sum ^{n} _{i=1} \lambda _{i}x_{i ...

  10. Win10 VirtualBox虚拟机搭建lnmp环境

    之前用的是vagrant+VirtualBox搭建的环境,因为是windows系统动不动就报错,打不开环境,所以还是老老实实换了虚拟机哎.... 版本: VirtualBox 5.1.34   xsh ...