problem Description
In mathematics, and more specificallyinchGraph theory, a tree isAn undirected graphinchWhich any and nodes is connected by exactly one path. In other words, any connected graph without simple cycles isa tree. You find aPartialTree on the the-the-the-same. This tree have n nodes but lacks of n−1Edges. You want to complete ThisTree by adding n−1Edges. There must is exactly one path between any and nodes after adding. As you know, there is nn−2Ways to complete ThisTree, and you want to make the completed tree asCool asPossible. The coolness of a tree isThe sum of coolness of its nodes. The coolness of a node isF (d),whereF isA predefined function and D isThe degree of ThisNode. What's The maximum coolness of the completed tree?
Input
Case inch One Line,then one line with n− 1 integers f (1), F (2),..., f (n−1). 1≤t≤2≤n≤0≤f (i) ≤1000010 test Cases with n>.
Output
Case in one line.
Sample Input
2 3 2 1 4 5 1 4
Sample Output
5 +
Source
2015ACM/ICPC Asia Changchun Station-Replay (thanks to northeast Normal University)
Note that the number of trees with a node n is 2*n-2, so the problem is converted to the maximum value that can be obtained by assigning 2*n-2 degrees to n nodes, and each node is at least 1 degrees in total. We can divide each one by one degree and then allocate n-2 nodes arbitrarily. Allocation of time because it has been divided into 1 degrees, so to 2~n-1 the degree of 1~n-1, and then make a complete backpack on the line.
1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - #definePI ACOs (-1.0) - #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long - #defineEPS 1e-10 + #defineMOD 1000000007 A #defineN 2116 at #defineINF 1e12 - intN; - intF[n],dp[n]; - intMain () - { - intT; inscanf"%d",&t); - while(t--){ toscanf"%d",&n); + for(intI=1; i<=n-1; i++){ -scanf"%d",&f[i]); the } * intans=0; $ans+=f[1]*N;Panax Notoginseng for(intI=2; i<=n-1; i++){ -f[i]-=f[1]; the } + for(intI=1; i<=n-2; i++){ Af[i]=f[i+1]; the } + //memset (Dp,0,sizeof (DP)); - for(intI=0; i<n;i++){ $dp[i]=-inf; $ } -dp[0]=0; - for(intI=1; i<=n-2; i++){ the for(intj=i;j<=n-2; j + +){ -Dp[j]=max (dp[j],dp[j-i]+f[i]);Wuyi } the } -ans+=dp[n-2]; Wuprintf"%d\n", ans); - } About return 0; $}
View Code
HDU 5534 Partial Tree (full backpack)