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

Posted lipoicyclic

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3585 Accumulation Degree(二次扫描与换根法)相关的知识,希望对你有一定的参考价值。

Trees are an important component of the natural landscape because of their prevention of erosion and the provision of a specific ather-sheltered ecosystem in and under their foliage. Trees have also been found to play an important role in producing oxygen and reducing carbon dioxide in the atmosphere, as well as moderating ground temperatures. They are also significant elements in landscaping and agriculture, both for their aesthetic appeal and their orchard crops (such as apples). Wood from trees is a common building material.

Trees also play an intimate role in many of the world‘s mythologies. Many scholars are interested in finding peculiar properties about trees, such as the center of a tree, tree counting, tree coloring. A(x) is one of such properties.

A(x) (accumulation degree of node x) is defined as follows:

 

  1. Each edge of the tree has an positive capacity.
  2. The nodes with degree of one in the tree are named terminals.
  3. The flow of each edge can‘t exceed its capacity.
  4. A(x) is the maximal flow that node x can flow to other terminal nodes.

Since it may be hard to understand the definition, an example is showed below:

技术图片


A(1)=11+5+8=24
Details: 1->2 11
  1->4->3 5
  1->4->5 8(since 1->4 has capacity of 13)
A(2)=5+6=11
Details: 2->1->4->3 5
  2->1->4->5 6
A(3)=5
Details: 3->4->5 5
A(4)=11+5+10=26
Details: 4->1->2 11
  4->3 5
  4->5 10
A(5)=10
Details: 5->4->1->2 10

The accumulation degree of a tree is the maximal accumulation degree among its nodes. Here your task is to find the accumulation degree of the given trees.

Input

The first line of the input is an integer T which indicates the number of test cases. The first line of each test case is a positive integer n. Each of the following n - 1 lines contains three integers x, y, z separated by spaces, representing there is an edge between node x and node y, and the capacity of the edge is z. Nodes are numbered from 1 to n.
All the elements are nonnegative integers no more than 200000. You may assume that the test data are all tree metrics.

Output

For each test case, output the result on a single line.
 

Sample Input

1
5
1 2 11
1 4 13
3 4 5
4 5 10

Sample Output

26
基本是按照蓝书讲的写的,具体看代码注释。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 200005
using namespace std;
int n;
int tot=0,head[N],ver[2*N],edge[2*N],Next[2*N],f[N],d[N],dig[N];//d[i]表示以i为原点,从i出发流向子树的流量最大值 f[i]表示以i为根流向整个水系的最大值 
inline void add(int x,int y,int z)
{
    ver[++tot]=y,edge[tot]=z,Next[tot]=head[x],head[x]=tot;
}
inline void dp(int x,int pre)
{
    d[x]=0;
    int i;
    for(i=head[x];i;i=Next[i])
    {
        int y=ver[i],z=edge[i];
        if(y==pre)continue;
        dp(y,x);
        if(dig[y]==1) d[x]+=z;//x是叶子节点 
        else d[x]+=min(z,d[y]);//木桶原理 
    }
}
inline void dfs(int x,int pre)
{
    int i;
    for(i=head[x];i;i=Next[i])
    {
        int y=ver[i],z=edge[i];
        if(y==pre)continue;
        //不要在这里dfs 因为d[y]已经求出来了 这和之前的树背包不太一样 这个题是用已知的f[x]去求未知的f[y],是根像叶子结点的更新,而且是先更新再递归 
        if(dig[x]==1)f[y]=d[y]+z;//以y为原点的话x直接是入海口,所以直接加即可
        else f[y]=d[y]+min(z,f[x]-min(z,d[y]));
        dfs(y,x);
    }
} 
signed main()
{
    int t;cin>>t;
    while(t--)
    {
        tot=0;
        memset(f,0,sizeof(f));//-1表示没有正确求出 
        memset(d,0,sizeof(d));
        memset(head,0,sizeof(head));
        memset(ver,0,sizeof(ver));
        memset(edge,0,sizeof(edge));
        memset(Next,0,sizeof(Next));
        memset(dig,0,sizeof(dig));
        cin>>n;
        int i;
        for(i=1;i<=n-1;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
            dig[x]++,dig[y]++;
        }
        dp(1,0);
        f[1]=d[1];
        dfs(1,0);
        int ans=0;
        for(i=1;i<=n;i++)ans=max(ans,f[i]);
        cout<<ans<<endl;
    }
    return 0;
}

还是记得开二倍数组大小!

以上是关于POJ3585 Accumulation Degree(二次扫描与换根法)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3585 Accumulation Degree 题解

POJ3585 Accumulation Degree 树形dp

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

poj3585 Accumulation Degree(换根dp)

[POJ3585]Accumulation Degree

POJ 3585Accumulation Degree