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. Criteria 查询

    Criteria.Criterion接口和Expression类组成,他支持在运行时动态生成查询语句. Criteria查询是Hibernate提供的一种查询方式 Hibernate检索方式:  PO ...

  2. canves图形变换

    canves用得好可以有好多效果: html:<canvas id="myCanvas" width="700" height="300&quo ...

  3. ubuntu下sudo命令不能使用问题

    不知道从什么时候开始,ctrl+alt+F1进入命令行之后,登录成功.使用sudo命令,不能使用....被坑了很久. 解决方法: 出现 [sudo ] username !!! 之后,在输入一遍 密码 ...

  4. CNN结构:色彩空间建模-色彩空间分析

    原文: 色彩空间基础 好一个NB的知乎专栏:色彩空间基础 第一章:色彩空间基础 关于色彩分析,引出了专门的数学基础.整个过程给出了完备的数学阐述,虽然没有试验数据,论述的相当精彩. 摘抄出一段:  上 ...

  5. which

    功能说明:显示命令的全路径.   参数选项: -a  默认在PATH路径中由前往后查找命令,如果查找到了,就停止匹配.使用-a选项将遍历所有PATH路径,输出所有匹配项.   参数-a把所有匹配命令路 ...

  6. Python之模块、正则

    一.模块import 模块的实质就是把要导入模块里面的代码,从上到下执行一遍,找模块的顺序是,先从当前目录下找,找不到的话,再环境变量里面找导入的模块名字最好不要有.,a.import sysprin ...

  7. GatewayWorker + LayIM实现即时聊天

    一.程序目录结构 二.代码展示 附LayIM开发文档:https://www.layui.com/doc/modules/layim.html 1.前端代码 <!DOCTYPE html> ...

  8. 用shell编写dhcp自动获取脚本

    #!/bin/bash#net=$(ifconfig ens33 | awk -F'[ .]+' '/inet\>/{print $3"."$4"."$5 ...

  9. MATLAB仿真学习笔记(一)

    一.Simulink概述 1.特点 simulink是对动态系统进行建模.仿真和综合分析的图形化软件,可以处理线性和非线性.离散.连续和混合系统,也可以处理单任务和多任务系统,并支持多种采样频率的系统 ...

  10. String formatting in Python

    | \n | 换行   || \t | 制表符 || \  | 转义   || \\ | \      | the '%' operator is used to format a set of va ...