2015ACM/ICPC亚洲区长春站 G hdu 5534 Partial Tree

Partial Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 228    Accepted Submission(s): 138

Problem Description

In mathematics, and more specifically in graph theory, a tree is an undirected graph in which any two nodes are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.

You find a partial tree on the way home. This tree has n nodes but lacks of n−1 edges. You want to complete this tree by adding n−1 edges. There must be exactly one path between any two nodes after adding. As you know, there are nn−2 ways to complete this tree, and you want to make the completed tree as cool as possible. The coolness of a tree is the sum of coolness of its nodes. The coolness of a node is f(d), where f is a predefined function and d is the degree of this node. What‘s the maximum coolness of the completed tree?

Input

The first line contains an integer T indicating the total number of test cases.
Each test case starts with an integer n in one line,
then one line with n−1 integers f(1),f(2),…,f(n−1).

1≤T≤2015
2≤n≤2015
0≤f(i)≤10000
There are at most 10 test cases with n>100.

Output

For each test case, please output the maximum coolness of the completed tree in one line.

Sample Input

2
3
2 1
4
5 1 4

Sample Output

5
19

Source

2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)

题意:一个树的权值被定义为sigma(f(di), 1<=i<=n)其中di是第i个点的度数,求一个n个点的数的最大权值

分析:先把每个点的度数当成1,然后问题转化为总度数为2n-2,现有度数n,求增加n-2度数的最大权值

dp[i]表示增加i度数的最大权值

dp[i] = max(f[i+1], dp[j]+dp[i-j]), 1<=j<=i/2

完了

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <ctime>
 6 #include <iostream>
 7 #include <map>
 8 #include <set>
 9 #include <algorithm>
10 #include <vector>
11 #include <deque>
12 #include <queue>
13 #include <stack>
14 using namespace std;
15 typedef long long LL;
16 typedef double DB;
17 #define MIT (2147483647)
18 #define MLL (1000000000000000001LL)
19 #define INF (1000000001)
20 #define For(i, s, t) for(int i = (s); i <= (t); i ++)
21 #define Ford(i, s, t) for(int i = (s); i >= (t); i --)
22 #define Rep(i, n) for(int i = (0); i < (n); i ++)
23 #define Repn(i, n) for(int i = (n)-1; i >= (0); i --)
24 #define mk make_pair
25 #define ft first
26 #define sd second
27 #define puf push_front
28 #define pub push_back
29 #define pof pop_front
30 #define pob pop_back
31 #define sz(x) ((int) (x).size())
32 #define clr(x, y) (memset(x, y, sizeof(x)))
33 inline void SetIO(string Name)
34 {
35     string Input = Name + ".in";
36     string Output = Name + ".out";
37     freopen(Input.c_str(), "r", stdin);
38     freopen(Output.c_str(), "w", stdout);
39 }
40
41 inline int Getint()
42 {
43     char ch = ‘ ‘;
44     int Ret = 0;
45     bool Flag = 0;
46     while(!(ch >= ‘0‘ && ch <= ‘9‘))
47     {
48         if(ch == ‘-‘) Flag ^= 1;
49         ch = getchar();
50     }
51     while(ch >= ‘0‘ && ch <= ‘9‘)
52     {
53         Ret = Ret * 10 + ch - ‘0‘;
54         ch = getchar();
55     }
56     return Ret;
57 }
58
59 const int N = 2050;
60 int n, F[N];
61 int Dp[N];
62
63 inline void Solve();
64
65 inline void Input()
66 {
67     int TestNumber = Getint();
68     while(TestNumber--)
69     {
70         n = Getint();
71         For(i, 1, n - 1) F[i] = Getint();
72         Solve();
73     }
74 }
75
76 inline void Solve()
77 {
78     For(i, 2, n - 1) F[i] -= F[1];
79     Dp[0] = 0;
80     For(i, 1, n - 2)
81     {
82         Dp[i] = F[i + 1];
83         For(j, 1, (i / 2) + 1)
84             Dp[i] = max(Dp[i], Dp[j] + Dp[i - j]);
85     }
86     printf("%d\n", Dp[n - 2] + n * F[1]);
87 }
88
89 int main()
90 {
91     Input();
92     //Solve();
93     return 0;
94 }

时间: 2024-12-27 15:35:00

2015ACM/ICPC亚洲区长春站 G hdu 5534 Partial Tree的相关文章

2015ACM/ICPC亚洲区长春站 G hdu 5533 Dancing Stars on Me

Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 186    Accepted Submission(s): 124 Problem Description The sky was brushed clean by the wind and the stars were cold in a bl

2015ACM/ICPC亚洲区长春站 B hdu 5528 Count a * b

Count a * b Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 211    Accepted Submission(s): 116 Problem Description Marry likes to count the number of ways to choose two non-negative integers a

2015ACM/ICPC亚洲区长春站 L hdu 5538 House Building

House Building Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 145    Accepted Submission(s): 123 Problem Description Have you ever played the video game Minecraft? This game has been one of t

2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory

Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 368    Accepted Submission(s): 202 Problem Description John is a manager of a CPU chip factory, the factory produces lots of chips

2015ACM/ICPC亚洲区长春站 A hdu 5527 Too Rich

Too Rich Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 245    Accepted Submission(s): 76 Problem Description You are a rich person, and you think your wallet is too heavy and full now. So yo

2015ACM/ICPC亚洲区长春站 F hdu 5533 Almost Sorted Array

Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 447    Accepted Submission(s): 201 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort,

2015ACM/ICPC亚洲区长春站 E hdu 5531 Rebuild

Rebuild Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 446    Accepted Submission(s): 113 Problem Description Archaeologists find ruins of Ancient ACM Civilization, and they want to rebuild i

HDU 5534 Partial Tree 完全背包

一棵树一共有2*(n-1)度,现在的任务就是将这些度分配到n个节点,使这n个节点的权值和最大. 思路:因为这是一棵树,所以每个节点的度数都是大于1的,所以事先给每个节点分配一度,答案 ans=f[1]*n 先将答案赋值 所以接下来研究的就是,将剩下的n-2个度分配 即分别看 分配度数为1到n-2的节点的有几个(因为每个节点已经有一度),然后因为每个节点都加上了权值f[1],所以这时f[2]=f[2]-f[1],以此类推, 看到这里,就是一个完全背包问题:如果还没看出来,详细一点 有1到n-2这些

HDU - 5534 Partial Tree(每种都装的完全背包)

Partial Tree In mathematics, and more specifically in graph theory, a tree is an undirected graph in which any two nodes are connected by exactly one path. In other words, any connected graph without simple cycles is a tree. You find a partial tree o