hdu5534 Partial Tree dp(难)

hdu5534 Partial Tree  dp(难)

Partial Tree

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

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亚洲区长春站-重现赛(感谢东北师大)

将前n个度数分给n个点,剩下的就可以随便分了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define MS0(a) memset(a,0,sizeof(a))

using namespace std;

typedef long long ll;
const int maxn=100100;
const int INF=(1<<29);

int f[maxn],n;
ll dp[maxn];

int main()
{
    int T;cin>>T;
    while(T--){
        cin>>n;
        REP(i,1,n-1) scanf("%d",&f[i]);
        ll ans=f[1]*n;
        MS0(dp);
        REP(i,0,n-2) dp[i]=-INF;
        dp[0]=0;
        REP(i,1,n-2){
            REP(j,0,i){
                dp[i]=max(dp[i],dp[i-j]+f[j+1]-f[1]);
                //cout<<i<<" "<<dp[i]<<endl;
            }
        }
        ans+=dp[n-2];
        printf("%I64d\n",ans);
    }
    return 0;
}

时间: 2024-11-04 04:12:08

hdu5534 Partial Tree dp(难)的相关文章

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这些

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 undirect

hdu 5534 (完全背包) Partial Tree

题目:这里 题意: 感觉并不能表达清楚题意,所以 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

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

HDU 4359 Easy Tree DP?

Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1487    Accepted Submission(s): 567 Problem Description A Bear tree is a binary tree with such properties : each node has a value o

HDU 4359——Easy Tree DP?——————【dp+组合计数】

Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1460    Accepted Submission(s): 557 Problem Description A Bear tree is a binary tree with such properties : each node has a value o

codeforces 514E Darth Vader and Tree (dp+快速幂)

codeforces 514E Darth Vader and Tree (dp+快速幂) 题意: 有一棵树,每个节点有n个儿子,给出父亲到每个儿子的距离di,问离祖先距离不超过x的子孙有多少个(子孙包括祖先)对1e9+7取模. 限制: 1 <= n <= 1e5; 0 <= x <= 1e9; 1 <= di <= 100 思路: 因为di <= 100,所以可以用快速幂来处理这道题, 大概过程为:先用dp算出前100的答案,剩下的用快速幂来处理.

TYOI Day1 travel:Tree dp【处理重复走边】

题意: 给你一棵树,n个节点,每条边有长度. 然后有q组询问(u,k),每次问你:从节点u出发,走到某个节点的距离mod k的最大值. 题解: 对于无根树上的dp,一般都是先转成以1为根的有根树,然后分别从上到下和从下到上两遍dp. 另一个技巧是:处理重复走边的情况时,可以让dp值表示达到某种状态的方案数. 表示状态: dp[i][j][k] = max dis 表示从i节点出发,走的距离mod k = j时的方案数 找出答案: 对于每次询问(u,k),答案为:满足dp[u][d][k]>0的最

C. Helga Hufflepuff&#39;s Cup 树形dp 难

C. Helga Hufflepuff's Cup 这个题目我感觉挺难的,想了好久也写了很久,还是没有写出来. dp[i][j][k] 代表以 i 为根的子树中共选择了 j 个特殊颜色,且当前节点 i 的状态为 k 的染色方案数. k=0 ,代表当前节点 i 的颜色值小于 K . k=1,代表当前节点 i 的颜色值等于 K . k=2,代表当前节点 i 的颜色值大于 K . 但是这个dfs过程的处理我觉得很复杂. 我们需要一个数组来进行临时的存储. tmp[i][k] 表示选了 i 个  状态为