Valley 1122 Max Subtrees
Address: http://www.luogu.org/problem/show?pid=1122
Title Description
Xiao Ming is interested in mathematics, and is a studious student, always stay in the classroom after class to ask the teacher some questions. One day he rode to class in the morning, on the way to see an old man is pruning flowers and grass, suddenly think of a question about pruning flowers. So the day after class, Xiao Ming to the teacher raised the question:
A strange flower, which has a total of n flowers, a total of n-1 branches of flowers together, and not trimmed when each flower is not isolated. Each flower has a "beautiful index", the larger the number indicates that the more beautiful the flower, there is a "beautiful index" is negative, indicating that the flower looks disgusting. The so-called "pruning", meaning: remove one of the branches, so that a flower has become two strains, throw away one of the strains. After a series of "pruning", the last flower (or perhaps one) remains. The teacher's task is to: through a series of "pruning" (also can be any "pruning" does not carry out), so that the remaining strain (that flower) all flowers on the "Beauty Index" and the largest.
The teacher thought for a moment and gave a positive solution. Xiao Ming See the problem is easy to break, quite uncomfortable, and then brought to ask you.
Input/output format
Input Format:
Enter the first line of the file maxsum3.in an integer N (1≤n≤16000). A total of n flowers on the original plant flower.
The second line has n integers, and the first integer represents the beautiful exponent of the I flower.
The next N-1 line is two integers A/b, indicating the presence of a branch of a flower and a B flower.
output Format:
The output file Maxsum3.out includes only one number, which represents the maximum value of the sum of the "beautiful exponents" that can be obtained after a series of "trims". Guaranteed absolute value not exceeding 2147483647.
Input/Output sample
Input Sample # #:
7
-1-1-1 1 1 1 0
1 4
2 5
3 6
4 7
5 7
6 7
Sample # # of output:
3
Description
"Data size and conventions"
For 60% of the data, there are n≤1000;
For 100% of the data, there is n≤16000.
Ideas
The DP on the tree.
Give a tree without a root and find the most graceful subtree.
Because the given tree is non-root and the topic requires no relation to the tree's parent-child order, you can choose a node as the root DP.
Need attention ans=max{D[u]};
Code
1#include <cstdio>2#include <vector>3 using namespacestd;4 5 Const intMAXN =16000+Ten;6 7 intN;8vector<int>G[MAXN];9 intW[MAXN],D[MAXN];Ten One intdpintUintFA) { A if(D[u])returnD[u]; - -d[u]=W[u]; the for(intI=0; I<g[u].size (); i++) { - intv=G[u][i]; - if(v!=FA) { - intx=DP (V,U); + if(x>0) D[u] + =x; - } + } A returnD[u]; at } - - intMain () { -scanf"%d",&n); - for(intI=1; i<=n;i++) scanf ("%d",&w[i]); - intu,v; in for(intI=1; i<n;i++) { -scanf"%d%d",&u,&v); to G[u].push_back (v); + g[v].push_back (u); - } thedp1,-1); * intans=0; $ for(intI=1; i<=n;i++) ans=Max (ans,d[i]);Panax Notoginsengprintf"%d", ans); - return 0; the}
Valley 1122 Max Subtrees