Partial Tree(DP)
Partial Tree
http://acm.hdu.edu.cn/showproblem.php?pid=5534
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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− edges. You want to complete this tree by adding n− edges. There must be exactly one path between any two nodes after adding. As you know, there are nn− 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− integers f(),f(),…,f(n−). ≤T≤
≤n≤
≤f(i)≤
There are at most test cases with n>. Output
For each test case, please output the maximum coolness of the completed tree in one line. Sample Input Sample Output Source
2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大) Recommend
hujie
因为每个点最少的度数为1,所以可以先设每个点的度数为1,剩余的度数为(2*n-2)-n=n-2,相当于把剩下的n-2个度分给n个顶点,这样就转化为完全背包的题目了
#include<bits/stdc++.h>
const int INF=0x3f3f3f3f;
using namespace std; int a[];
int dp[]; int main(){ int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
int ans=a[]*n;
for(int i=;i<=n;i++) dp[i]=-INF;
dp[]=;
for(int i=;i<n;i++) a[i]-=a[];
for(int i=;i<n;i++){
for(int j=;j<=n-;j++){
dp[i+j-]=max(dp[i+j-],dp[j]+a[i]);
}
}
printf("%d\n",ans+dp[n-]);
} }
Partial Tree(DP)的更多相关文章
- HDU 5534/ 2015长春区域H.Partial Tree DP
Partial Tree Problem Description In mathematics, and more specifically in graph theory, a tree is an ...
- hdu 5534 Partial Tree 背包DP
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- hdu 5534 (完全背包) Partial Tree
题目:这里 题意: 感觉并不能表达清楚题意,所以 Problem Description In mathematics, and more specifically in graph theory, ...
- 2015ACM/ICPC亚洲区长春站 H hdu 5534 Partial Tree
Partial Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU 5534 Partial Tree 完全背包
一棵树一共有2*(n-1)度,现在的任务就是将这些度分配到n个节点,使这n个节点的权值和最大. 思路:因为这是一棵树,所以每个节点的度数都是大于1的,所以事先给每个节点分配一度,答案 ans=f[1] ...
- Partial Tree
Partial Tree 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 完全背包 做这题前去学习了下完全背包,觉得这个优化简直神技!(以前都是 ...
- H - Partial Tree HDU - 5534 (背包)
题目链接: H - Partial Tree HDU - 5534 题目大意:首先是T组测试样例,然后n个点,然后给你度数分别为(1~n-1)对应的不同的权值,然后问你在这些点形成树的前提下的所能形 ...
- HDU - 5534 Partial Tree(每种都装的完全背包)
Partial Tree In mathematics, and more specifically in graph theory, a tree is an undirected graph in ...
- hdu5534 Partial Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submissi ...
随机推荐
- js代码的执行顺序及运算
代码执行顺序:从上往下,一行一行的执行(也叫一个模块一个模块的执行) 变量的提升(它不是变量的功能,而是浏览器的功能) js代码如何执行? js代码执行前,浏览器会给他一个全局的环境 叫window, ...
- 浅谈在Java开发中的枚举的作用和用法
枚举(enum),是指一个经过排序的.被打包成一个单一实体的项列表.一个枚举的实例可以使用枚举项列表中任意单一项的值.枚举在各个语言当中都有着广泛的应用,通常用来表示诸如颜色.方式.类别.状态等等数目 ...
- javascript创建对象之函数构造模式和原型模式结合使用(四)
创建自定义类型的常见方式就是组合使用构造函数模式与原型模式一起使用. 构造函数模式用于定义实例对象的特有的部分(属性和方法),原型模式用于定义共享的部分. 这样最大限度的节省了内存的开销. funct ...
- GameObject数组
一.声明 GameObject[] rock = ]; 二.遍历 可以先声明一个GameObject的数组GameObject[], 然后把需要遍历的对象放进去,然后可以用下面两种方式遍历:1.for ...
- Hadoop恢复namenode数据
情景再现: 在修复hadoop集群某一个datanode无法启动的问题时,搜到有一个答案说要删除hdfs-site.xml中dfs.data.dir属性所配置的目录,再重新单独启动该datanode即 ...
- PHP流程控制 - if 语句
PHP - if 语句 if 语句用于仅当指定条件成立时执行代码. 语法 if (条件) { 条件成立时要执行的代码; } 如果当前时间小于 20,下面的实例将输出 "Have a good ...
- Phoenix 安装完的几个简单使用
Phoenix 安装完之后的一些简单使用: 本屌丝的运行环境是3节点集群,先启动hadoop,每个节点分别启动zookeeper,启动hbase(具体怎么启动这里就不多说了) 进入 phoenix的b ...
- SQL中去掉字符串中最后一个字符(小技巧)
--长度减一就可以了 select left(字段名,len(字段名)-1) from 表名
- jenkins API
1.curl http://199.168.299.99:8080/job/send_message/lastBuild/api/json --user administrator:1234 获取j ...
- html调用静态json例子
1.json { "current": 2, "result": "success" } 1.html <!doctype html& ...