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. DML和DQL语句

    DML操作: a.插入单行数据:      INSERT INTO 表名(列名,列名,...) VALUE(列值,列值,...); 表的字段名是可选的,如果省略,则一次插入所有字段 多个列表和多个值之 ...

  2. String字符串的完美度

    题目详情: 我们要给每个字母配一个1-26之间的整数,具体怎么分配由你决定,但不同字母的完美度不同, 而一个字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小写,也就是说字母F和f的完美度 ...

  3. 一、CSS的基础样式

    CSS的基础样式 border 边框 复合写法 border:border-width  border-style border-color: border-width:边框宽度 top right ...

  4. zblog实现后台导航栏增加链接功能的最简单方法

    首先在ftp中找到这个目录   zb_system/admin/ 然后找到    admin_top.php      这个文件 再然后找到这行代码      <?php ResponseAdm ...

  5. Android进入一个新页面,EditText失去焦点并禁止弹出键盘

    android在进入一个新页面后,edittext会自动获取焦点并弹出软键盘,这样并不符合用户操作习惯. 在其父控件下,添加如下的属性,就可以完美解决,使其进入页面后不主动获取焦点,并且不弹出软键盘: ...

  6. JS弹出子窗口

    目的 在一个主窗口中,点击一个链接, 弹出一个子窗口 , 父窗口保留 在子窗口中点击关闭, 关闭子窗口. 子窗口的位置位于屏幕的中间 实现 main.html <!DOCTYPE html> ...

  7. mybatis批量操作(foreach)

    foreach可以在SQL语句中通过拼接的方式进行集合迭代.foreach元素的属性主要有collection,item,index,separator,open,close. item属性:表示循环 ...

  8. 学习MPI并行编程记录

    简单的MPI程序示例 首先,我们来看一个简单的MPI程序实例.如同我们学习各种语言的第一个程序一样,对于MPI的第一个程序同样是"Hello Word". /* Case 1 he ...

  9. centos中安装jdk

    1.上传jdk安装文件到根目录 2.解压到相关目录 (1)创建相应目录mkdir -p /usr/local/java (2)解压 tar -zxvf jdk-7u80-linux-x64.tar.g ...

  10. python 易忘操作整理

    >>> l=[1,2,3,4,5] >>> s='$'.join(str(i) for i in l) >>> print(s,end=" ...