Luo Gu P1122 and the largest sub-tree (tree DP)

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 NN N flowers, a total of N-1N-1 N - 1 Tiao branches to flower together, 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", 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≤16000) N (. 1 ≤ N ≤ 16000) N ( . 1 N . 1 . 6 0 0 0 ). It represents the total of the original flowers growing on NN N flowers.

The second line has NN N integers, the first II the I integer represents II the I beautiful flower index.

Next, N-1N. 1- N - . 1 lines each two integers A, BA, b A , b, indicating the presence of a connection AA A flower and bb branches of flowers b.

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 21474836472147483647 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 
do more packets beginning backpack did not even react ...
see N = 1e5 know dp arrays can only be a one-dimensional, dp [i] is the maximum value of beautiful subtree rooted at i. Transfer equation: dp [i] = max ( dp [i], dp [i] + dp [j]) (j i the son), careful not. Repeat to go for this problem, there is no clear root, so just find as a DFS root can be carried out.
Open array must be open to double the size of the deposit reverse side! ! ! !
#include <bits / STDC ++ H.>
 #define N 32005 
 the using  namespace STD;
 int n-, P, head [N], Ver [N], the Next [N], TOT = 0 ;
 int DP [N] = {- 0x3f3f3f3f }, a [N]; // DP [i] i in the tree rooted maximum 
int ANS = - 0x3f3f3f3f ;
 void the Add ( int X, int Y) 
{ 
    Ver [ ++ TOT] = Y, the Next [TOT ] = head [X], head [X] = TOT;     
 } 
void DFS ( int X, int pre) 
 { 
     // initialization 
     DP [X] = A [X];
     int i,j,k;
     for(i=head[x];i;i=Next[i])
     {
         int y=ver[i];
         if(y==pre)continue;
         dfs(y,x);
         dp[x]=max(dp[x],dp[x]+dp[y]);
    }
    ans=max(ans,dp[x]);
 }
int main()
{
    cin>>n;
    int i;
    memset(dp,-0x3f3f3f3f,sizeof(dp));//注意初始化 
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
     } 
    for(i=1;i<=n-1;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    dfs(1,0);
    cout<<ans;
} 

 

Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12571815.html