[POJ 3585] Accumulation Degree

[题目链接]

http://poj.org/problem?id=3585

[算法]

树形DP——二次扫描与换根法

[代码]

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 200010

int i,n,tot,u,v,w,ans,T;
int d[MAXN],f[MAXN],degree[MAXN],head[MAXN];

struct Edge
{
    int to,w,nxt;
} e[MAXN<<1];

inline void addedge(int u,int v,int w)
{
    tot++;
    e[tot] = (Edge){v,w,head[u]};
    head[u] = tot;
}
inline void dp(int u,int fa)
{
    int i,v,w;
    d[u] = 0;
    for (i = head[u]; i; i = e[i].nxt)
    {
        v = e[i].to;
        w = e[i].w;
        if (fa != v)
        {
            dp(v,u);
            if (degree[v] == 1) d[u] += w;
            else d[u] += min(d[v],w);
        }
    }
}
inline void dfs(int u,int fa)
{
    int i,v,w;
    for (i = head[u]; i; i = e[i].nxt)
    {
        v = e[i].to;
        w = e[i].w;
        if (fa != v)
        {
            if (degree[u] == 1) f[v] = d[v] + w;
            else f[v] = d[v] + min(f[u] - min(d[v],w),w);
            dfs(v,u);
        }
    }
}

int main()
{

    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&n);
        tot = 0;
        for (i = 1; i <= n; i++)
        {
            head[i] = 0;
            degree[i] = 0;
            d[i] = 0;
            f[i] = 0;
        }
        for (i = 1; i < n; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
            degree[u]++;
            degree[v]++;
        }
        dp(1,-1);
        f[1] = d[1];
        dfs(1,-1);
        ans = 0;
        for (i = 1; i <= n; i++) ans = max(ans,f[i]);
        printf("%d\n",ans);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/evenbao/p/9330950.html

时间: 2024-07-30 22:09:30

[POJ 3585] Accumulation Degree的相关文章

【POJ 3585】Accumulation Degree

[原题题面]传送门 [题解大意] 乍一看感觉以为网络流表示一点都不会不会不会. 然后发现可以树形dp搞一下. 再然后知道了换根dp. 设d[x]表示以x为根的子树中把x做为源点,从x出发流向子树的流量最大是多少. d[x] += min(d[y],z);(deg[y]!=1) d[x] = z; (deg[y]==1) f[x]表示以x做为源点,从x出发流向整个水系的流量最大是多少. f[y] = d[y] + min(f[x] - min(d[y],z));(deg[x]!=1) f[y] =

POJ3585 Accumulation Degree 【树形dp】

题目链接 POJ3585 题解 -二次扫描与换根法- 对于这样一个无根树的树形dp 我们先任选一根进行一次树形dp 然后再扫一遍通过计算得出每个点为根时的答案 #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #define LL long long int #define Redge(u) for (int k =

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

写一篇题解,以纪念调了一个小时的经历(就是因为边的数组没有乘2 phhhh QAQ) 题目 题目大意:找一个点使得从这个点出发作为源点,流出的流量最大,输出这个最大的流量. 以这道题来介绍二次扫描和换根法 作为一道不定根的树形DP,如果直接对每个点进行DP,可能时间会炸掉 但是,优秀的二次换根和扫描法可以再O(n^2)内解决问题. 二次扫描的含义:(来自lyd 算法竞赛进阶指南) 第一次扫描:任选一个节点为根节点(我会选1)在树上进行树形DP,在回溯时,从儿子节点向父节点(从底向上)进行状态转移

[POJ3585]Accumulation Degree

题面 \(\text{Solution:}\) 有些题目不仅让我们做树型 \(\text{dp}\) ,而且还让我们换每个根分别做一次, 然后这样就愉快的 \(\text{TLE}\) 了,所以我们要用一种方法快速知道所有根的答案. 二次扫描与换根法: 就是先选任意点作根做一遍 \(\text{dp}\) ,求出相关信息,然后再从根往下 \(\text{dfs}\) ,对每一个节点往下走之前进行自顶向下的推导,计算出 "换根" 后的解. 就这题而言就是用父亲的换根后的答案来跟新自己换根

poj3585 Accumulation Degree(换根dp)

传送门 换根dp板子题(板子型选手 题意: 一棵树确定源点和汇点找到最大的流量(拿出一整套最大瘤板子orz const int maxn=2e5+10; int head[maxn],tot; struct node { int nt,to;long long w; }q[2*maxn]; long long dp[maxn];int cnt[maxn]; void insert(int u,int v,long long w) { q[tot].nt=head[u];q[tot].w=w;q[

[Accumulation Degree]题解

换根dp板子题,首先,我们要想想如果根为1时,1的答案 我们设\(dp[i]\)表示以\(i\)为根子树的中,若\(i\)有无限流量,i点能往下流的最大流量. 我们不难推出式子\(dp[i]=\sum_{v\in son(i)}min(dp[v],w(u->v))\) 意义就是,我们知道一个儿子v可以向下流的最大流量是\(dp[v]\),我们最多可以向儿子v流\(w(u->v)\)的流量,所以我们最多向该儿子流\(min(dp[v],w(u->v))\)的流量,所有儿子的这个值的和就是\

换根DP

换根dp的通法:1.第一次扫描时,任选一个点为根,在"有根树"上执行一次树形DP,也就在回溯时发生的,自底向上的状态转移. 2.第二次扫描时,从刚才选出的根出发,对整棵树执行一次dfs,在每次递归前进行自上向下的推导,计算出换根后的解. 1.POJ3585 Accumulation Degree dp[i]以i为根的子树中,把i作为源点的最大流量 转移\(dp[x]=\sum_{y\epsilon son(x)}^{}\left\{\begin{matrix} min(dp[y],le

NC201400 树学题解

一.题解 ? 这道题又是一道换根dp板子题,代码结构与 Accumulation Degree 这道题基本一致,唯一不同的就是转移了[不过转移的时候,因为方程的原因不需要特殊考虑叶节点] ? 我们先套路的设\(dp[i]\)表示以\(i\)为根的子树中,所有点的深度和,现在,我们来想想转移. ? 我们发现,如果我们要从\(i\)的一个儿子v转移到i的话,以v为根的子树中的所有节点的深度都加了1,现在我们就不能够直接求值了,怎么办呢?很简单,我们发现,既然以v为根的子树中的所有节点的深度都加了1,

[poj 2331] Water pipe ID A*迭代加深搜索(dfs)

Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description The Eastowner city is perpetually haunted with water supply shortages, so in order to remedy this problem a new water-pipe has been built. Builders s