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 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个点,给你1到n-1的度数的价值,让你构造一棵树这颗树的价值就是,度数代表的价值和,问最大是多少,
 
题解:首先度的总和为2(n-1),并且每个节点度不为0。如果用二维dp[i][j]表示第i个节点还剩j个度的最优值,是没问题,但是复杂度为o(n3)。但是其实每个节点都要分配一个度,那么我们先给每个节点分配一个度,剩下n-2的度分给n个点,可以减掉一维,dp[i]表示i个度的最优值,因为度的个数是严格小于节点个数的。背包转移的权值为val[i]-val[1](可能有负数)
 
#include<cstdio>
using namespace std ;
#define inf 1000000
int main(){
int dp[],a,b,n,i,T,j;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&a);
for( i=;i<n;i++){dp[i]=-inf;}
for( i=;i<n;i++){
scanf("%d",&b);b=b-a;
for( j=i-;j<=n-;j++){
if(dp[j-(i-)]+b>dp[j])
dp[j]=dp[j-(i-)]+b;
}
}
printf("%d\n",n*a+dp[n-]);
}
return ;
}

代码

HDU 5534/ 2015长春区域H.Partial Tree DP的更多相关文章

  1. HDU 5538/ 2015长春区域 L.House Building 水题

    题意:求给出图的表面积,不包括底面 #include<bits/stdc++.h> using namespace std ; typedef long long ll; #define ...

  2. HDU 5533/ 2015长春区域 G.Dancing Stars on Me 暴力

    Dancing Stars on Me Problem Description The sky was brushed clean by the wind and the stars were col ...

  3. Travel(HDU 5441 2015长春区域赛 带权并查集)

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  4. HDU 5536/ 2015长春区域 J.Chip Factory Trie

    Chip Factory Problem Description John is a manager of a CPU chip factory, the factory produces lots ...

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

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

  6. Partial Tree(DP)

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

  7. hdu 5443 (2015长春网赛G题 求区间最值)

    求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 2 ...

  8. hdu 5446(2015长春网络赛J题 Lucas定理+中国剩余定理)

    题意:M=p1*p2*...pk:求C(n,m)%M,pi小于10^5,n,m,M都是小于10^18. pi为质数 M不一定是质数 所以只能用Lucas定理求k次 C(n,m)%Pi最后会得到一个同余 ...

  9. hdu 4274 2012长春赛区网络赛 树形dp ***

    设定每个节点的上限和下限,之后向上更新,判断是否出现矛盾 #include<cstdio> #include<iostream> #include<algorithm&g ...

随机推荐

  1. JS——arguments

    1.只在函数中使用 2.返回的是实参的数组 <script> getNum(1, 2);//(2) [1, 2, callee: ƒ, Symbol(Symbol.iterator): ƒ ...

  2. Python语言之变量1(数值,字符串,布尔)

    1.数值 整数:2, -2 长整数:2**1024, 2**2048(真的可以很~长~~~,手残算了个2**100000,IDLE还真给打出来了,ORZ) 浮点数:7.05, 1E2(100.0), ...

  3. hibernate Hql 更新的参数只能设置String类型

    最近在项目中发现一个奇怪的现象,请看下面的代码 实体类MyEmployeeEntity @Table(name="myemployee") public class MyEmplo ...

  4. HDU_5734_数学推公式

    题意:给一个向量W={w1,w2……,wn},和一个向量B,B的分量只能为1和-1.求||W-αB||²的最小值. 思路:一来一直在想距离的问题,想怎么改变每一维的值才能使这个向量的长度最小,最后无果 ...

  5. SSL协议提供的服务

    SSL协议提供的服务主要有: 1)认证用户和服务器,确保数据发送到正确的客户机和服务器: 2)加密数据以防止数据中途被窃取: 3)维护数据的完整性,确保数据在传输过程中不被改变.

  6. js的三种对象

    JS中,可以将对象分为“内部对象”.“宿主对象”和“自定义对象”三种. 1,内部对象 js中的内部对象包括Array.Boolean.Date.Function.Global.Math.Number. ...

  7. EF-Linq

    一丶基本语法(from a in Table where a.id="001" select a).Tolist(); 隐式内连接from a in table1 join b i ...

  8. 猜数字游戏的提示(Master-Mind Hints, UVa 340)

    实现一个经典"猜数字"游戏. 给定答案序列和用户猜的序列,统计有多少数字位置正确 (A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度 ...

  9. R 安装car包失败

    在RStudio里安装car包的时候报错 /usr/bin/ld: cannot find -llapack /usr/bin/ld: cannot find -lblas make: *** [qu ...

  10. namespace的作用及用法

    namespace 所谓namespace,是指标识符的可见范围.C++标准库中的所有标识符都被定义在一个名为 std 的namespace 中. 一.<iostream>和<ios ...