Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 126    Accepted Submission(s): 68


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
 
注意到一个节点数为n的树的度数和玮2*n-2,所以问题就转换为了把2*n-2个度分配给n个节点所能获得的最大价值,而且每一个节点至少分到1个度。我们可以先每一个分一个度,然后把n-2个节点任意分配完。分配的时候因为已经分了1个度了,所以要把2~n-1的度看为1~n-1,然后做个完全背包就行了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
int v[2200],dp[2200];
int main()
{
int n,m,i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n-1;i++){
scanf("%d",&v[i]);
}
int ans=0;
ans+=v[1]*n;
for(i=2;i<=n-1;i++){
v[i]-=v[1];
}
for(i=1;i<=n-2;i++){
dp[i]=-inf;
}
dp[0]=0;
for(i=1;i<=n-2;i++){
v[i]=v[i+1];
}
for(i=1;i<=n-2;i++){
for(j=i;j<=n-2;j++){
dp[j]=max(dp[j],dp[j-i]+v[i]);
}
}
ans+=dp[n-2];
printf("%d\n",ans);
}
}

hdu5534 Partial Tree的更多相关文章

  1. HDU-5534 Partial Tree 完全背包 设定初始选择

    题目链接:https://cn.vjudge.net/problem/HDU-5534 题意 放学路上看到n个节点,突然想把这几个节点连成一颗树. 树上每个节点有一个清凉度,清凉度是一个关于节点度的函 ...

  2. hdu 5534 (完全背包) Partial Tree

    题目:这里 题意: 感觉并不能表达清楚题意,所以 Problem Description In mathematics, and more specifically in graph theory, ...

  3. 2015ACM/ICPC亚洲区长春站 H hdu 5534 Partial Tree

    Partial Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  4. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  5. HDU 5534 Partial Tree 完全背包

    一棵树一共有2*(n-1)度,现在的任务就是将这些度分配到n个节点,使这n个节点的权值和最大. 思路:因为这是一棵树,所以每个节点的度数都是大于1的,所以事先给每个节点分配一度,答案 ans=f[1] ...

  6. Partial Tree

    Partial Tree 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 完全背包 做这题前去学习了下完全背包,觉得这个优化简直神技!(以前都是 ...

  7. H - Partial Tree HDU - 5534 (背包)

    题目链接: H - Partial Tree  HDU - 5534 题目大意:首先是T组测试样例,然后n个点,然后给你度数分别为(1~n-1)对应的不同的权值,然后问你在这些点形成树的前提下的所能形 ...

  8. Partial Tree(DP)

    Partial Tree http://acm.hdu.edu.cn/showproblem.php?pid=5534 Time Limit: / MS (Java/Others) Memory Li ...

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

    Partial Tree In mathematics, and more specifically in graph theory, a tree is an undirected graph in ...

随机推荐

  1. mmall商城用户模块开发总结

    1.需要实现的功能介绍 注册 登录 用户名校验 忘记密码 提交问题答案 重置密码 获取用户信息 更新用户信息 退出登录 目标: 避免横向越权,纵向越权的安全漏洞 MD5明文加密级增加的salt值 Gu ...

  2. SQL中的主键,候选键,外键,主码,外码

    1.码=超键:能够唯一标识一条记录的属性或属性集. 标识性:一个数据表的所有记录都具有不同的超键 非空性:不能为空 有些时候也把码称作"键" 2.候选键=候选码:能够唯一标识一条记 ...

  3. ASP.NET Core错误处理中间件[2]: 开发者异常页面

    <呈现错误信息>通过几个简单的实例演示了如何呈现一个错误页面,该过程由3个对应的中间件来完成.下面先介绍用来呈现开发者异常页面的DeveloperExceptionPageMiddlewa ...

  4. 集成Redis缓存

    一.简介 1.场景 由于首页数据变化不是很频繁,而且首页访问量相对较大,所以我们有必要把首页数据缓存到redis中,减少数据库压力和提高访问速度. 2.RedisTemplate Jedis是Redi ...

  5. 关于springboot2.X使用外部tomcat服务器进行部署的操作详细步骤

    1.修改pom.xml文件(4个地方) ①<packaging>war</packaging>将其中的jar该为war ②<dependency> <grou ...

  6. Arduino 上手实战呼吸灯

    前言 这篇稿子比以往的时候来的稍晚了一些,望fans们见谅,那即便如此,最终还是姗姗来迟了,公司新一轮战略性部署,被拖出去孵化新产品,开拓新市场去了,手头精力没有那么多了,另外产品一茬接一茬.韭菜一波 ...

  7. jemeter断言和性能分析

    一.添加断言 1.原因:检查是否有该结果,一般一个请求过去除了400和500的只要通过的都会代表请求成功,比如登录页面及时填写了错误密码,虽然会返回密码错误,但这个请求还是成功的,所以我们要添加断言, ...

  8. OPTIONS的预请求(Preflighted Request)

    OPTIONS的预请求(Preflighted Request) Ajax 请求中出现OPTIONS(Request Method: OPTIONS)_qiao-CSDN博客 https://blog ...

  9. class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware):

    class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware):

  10. Linux下nf_conntrack(最全面)_董明磊-CSDN博客_nf_conntrack https://blog.csdn.net/qq_35299863/article/details/79530732

    Linux下nf_conntrack(最全面)_董明磊-CSDN博客_nf_conntrack https://blog.csdn.net/qq_35299863/article/details/79 ...