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. P2871 [USACO07DEC]手链Charm Bracelet

    题目描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like t ...

  2. Puppeteer——自动化脚本设计

    我被分配了一个繁琐的任务,就是要给100个相同的站点做同样的配置.曾经就有做过相同的事,那时还不会写脚本,全靠手动配置.机械的配置了两天的时间,身体感觉被掏空.所以这次我决定还是写一个脚本自动的进行配 ...

  3. 程序员的幽默-献给所有Java程序员

    1. 一程序员去面试,面试官问:“你毕业才两年,这三年工作经验是怎么来的?!”程序员答:“加班.” 2. 某程序员对书法十分感兴趣,退休后决定在这方面有所建树.于是花重金购买了上等的文房四宝.一日,饭 ...

  4. android接收mjpg-streamer软件视频流

    [代码]主要实现代码 package cn.dong.mjpeg; import java.io.InputStream; import java.net.HttpURLConnection; imp ...

  5. Leetcode0523--Continuous Subarray Sum 连续和倍数

    [转载请注明]https://www.cnblogs.com/igoslly/p/9341666.html class Solution { public: bool checkSubarraySum ...

  6. CSS——img标签消除3px

    1.dispaly:block 2.float:left 这两种都可以消除3px

  7. PHP 之二位数组根据某个字段排序封装

    /** * @param $array * @param $keys * @param string $sort * @return array */ function arraySort($arra ...

  8. Memcached 在Linux上的安装

    1.安装libevent wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libeve ...

  9. Linux内核系统调用处理过程

    原创作品转载请注明出处 + https://github.com/mengning/linuxkernel/ 学号末三位:168 下载并编译Linux5.0 xz -d linux-.tar.xz . ...

  10. Git学习总结二(版本回退)

    修改修改仓库中readme.txt文件,然后用git status命令看看结果: $ git status On branch master Changes not staged for commit ...