[POJ 3585] Accumulation Degree

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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
",ans);
    }

    return 0;
}

 

以上是关于[POJ 3585] Accumulation Degree的主要内容,如果未能解决你的问题,请参考以下文章

[POJ 3585] Accumulation Degree

POJ3585:Accumulation Degree(换根树形dp)

POJ 3585 Accumulation Degree 题解

POJ3585 Accumulation Degree 树形dp

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

poj3585 Accumulation Degree(换根dp)