题面

\(\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. input按钮选择功能

    <p> <input type="checkbox" class="ischecked" id="is_pinkage" ...

  2. jquery插件layer

    //信息框-例1 layer.alert('见到你真的很高兴', {icon: 6}); //信息框-例2 layer.confirm('你确定你很帅么?', {icon: 3}, function( ...

  3. 搭建Hadoop2.6.0+Eclipse开发调试环境

    上一篇在win7虚拟机下搭建了hadoop2.6.0伪分布式环境.为了开发调试方便,本文介绍在eclipse下搭建开发环境,连接和提交任务到hadoop集群. 1. 环境 Eclipse版本Luna ...

  4. CentOS 7 安装oracle 11.2.0.4 Error in invoking target 'agent nmhs' of makefile

    %86时出现报错   Error in invoking target 'agent nmhs' of makefile 解决方案在makefile中添加链接libnnz11库的参数修改$ORACLE ...

  5. Linux7静默安装Oracle11g教程,亲测实用有效!

    1.查看swap大小,若小于150M,需添加增加虚拟空间 dd if=/dev/zero of=/swapadd bs=1024 count=2006424 mkswap /swapadd swapo ...

  6. 2小时学会spring boot 以及spring boot进阶之web进阶(已完成)

    1:更换Maven默认中心仓库的方法 <mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirr ...

  7. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  8. 【模板】tarjan算法

    tarjan求强连通分量 #include<bits/stdc++.h> #define MAX 10005 using namespace std; int dfn[MAX],low[M ...

  9. PG进程结构和内存结构

    ​ 本文主要介绍PostgreSQL数据库(后文简称PG)进程结构和内存结构,物理结构将在后续继续整理分享. ​ 上图描述了PG进程结构.内存结构和部分物理结构的内容.图中的内容包含了两个部分: PG ...

  10. PHP 序列化与反序列化简单理解

    序列化就是把保存在内存中的各种对象状态,保存起来,在需要的时候还原出来. string serialize ,产生一个可以储存的表示 mixed unserialize 从已储存的表示中创建php值( ...