2019 summer training camp and the largest sub-tree

Title Description

Xiao Ming full interest in mathematics, and is a studious student, always stay in the classroom after class to ask some questions to the teacher. One day he went to school in the morning ride, the road to see the old man is a pruning plants and flowers, suddenly thought of a question about pruning flowers. So day after school, Xiao Ming to raise the issue with the teacher:

A strange flower, above even have a total of N flowers, there are N - 1 Tiao branches will be linked to the flowers, and when not pruned every flower is not isolated. Each flower has a "beautiful index", the greater the number of flower more beautiful, but also "beautiful index" is negative, indicating flower looking at all disgusting. The so-called "pruning", which means: get rid of a branch of them, so a flower became two, which one throw. After a series of "pruning", he left the last one flower (possibly a). The teacher's task is to: (what can be "trimmed" are not carried out) through a series of "pruning", so that the rest of the kindred (flower) all the flowers on the flower "Beautiful index" and the largest sum.

The teacher thought for a moment, gives positive solution. Xiao Ming see the problem is easily broken, very unhappy, so he used to ask you.

Input Format

The first line of an integer N ( . 1 N . 1 . 6 0 0 0 ). It represents the original flowers growing on a total of N flowers.

The second line has N integers, the first I integer represents the I -flowered beauty index.

Then N-1 lines each two integers a , b, indicating the presence of a connection a flower and b flower branches.

Output Format

A number that represents the maximum value after a series of "pruning" can get "Beautiful index" sum. Absolute value is not more than 2 . 1 . 4 . 7 . 4 . 8 . 3 . 6 . 4 7

Sample input and output

Input # 1
7
-1 -1 -1 1 1 1 0
1 4
2 5
3 6
4 7
5 7
6 7
Output # 1
3

Description / Tips

[Agreed] with the scale data

For . 6 0 % of the data, there are N . 1 0 0 0;

For . 1 0 0 % of the data, there are N . 1 . 6 0 0 0.

Source title Luo Gu P1122


Artwork is easy to get a tree, and tree pruning does not affect how the child of his father, so consider a tree dp

 

For each of them the tree rooted at x, we consider all of its sub-tree Check / uncheck its value dp (dp [x] to x is the root of the subtree maximum beauty index)
First, we use subtree [x] represents the initialization value dp in which x is a subtree rooted not cut, the dp [x] = max (dp [x] -subtree [y], dp [x] -subtree [y ] + dp [y])
Cause subtree must be subtracted [Y] is a subtree for each of x, we discuss only take whichever dp value or not, rather than its subtree value
When the update is meant dp [x] its all dp [y] have been updated, every time we are made for each sub-tree of the optimal solution
The Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<climits>
using namespace std;
int subtree[100500],dp[100500],n,head[100500],num,c[100500];
struct edge
{
    int u,v,nxt;
}e[200500];
void add(int u,int v)
{
    e[++num].u=u,e[num].v=v;
    e[num].nxt=head[u];head[u]=num;
}
void init(int x,int fa)
{
    int ans=c[x];
    for(int st=head[x];st!=-1;st=e[st].nxt)
    {
        int y=e[st].v;
        if(y==fa)continue;
        init(y,x);
        ans+=subtree[y];
    }
    subtree[x]=ans;
    return;
}
int DP(int x,int fa)
{
    dp[x]=subtree[x];
    for(int st=head[x];st!=-1;st=e[st].nxt)
    {
        int y=e[st].v;
        if(y==fa)continue;
        DP(y,x);
        dp[x]=max(dp[x]-subtree[y],dp[x]-subtree[y]+dp[y]);
    }
    return dp[x];
}
int main()
{
    memset(head,-1,sizeof head);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&c[i]);
    int a,b;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
        add(b,a);
    }
    init(1,-1);
    DP(1,-1);
    int ans=INT_MIN;
    for(int i=1;i<=n;i++)
        ans=max(ans,dp[i]);
    printf("%d",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/qxds/p/11373551.html