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-]);
} }

最新文章

  1. 【趣事】用 JavaScript 对抗 DDOS 攻击 (下)
  2. Laravel 创建数据库
  3. JS添加父节点的方法。
  4. Linq group
  5. Java中为什么main()中不能创建内部类对象?
  6. [转]js中的字符串函数集和代码片段
  7. sencha combobox下拉框不用jsonstore,直接使用字符串数组做数据源
  8. 团队SCRUM会议(第一次)
  9. PHP Header 缓存 --- Header 参数说明
  10. 1109. Conference(二分图)
  11. HDU3727 - Jewel(主席树)
  12. Android使用百度地图定位
  13. getline函数
  14. getResources提取资源文件
  15. 团队作业4---第一次项目冲刺(ALpha)版本 第七天
  16. 头部banner根据网址高亮
  17. 2019中山大学程序设计竞赛-Monitor
  18. 小程序学习笔记三:页面文件详解之视图层WXML、WXS、WXSS文件
  19. [Angular] &#39;providedIn&#39; for service
  20. 依据Axis2官网的高速入门英文文档总结

热门文章

  1. Linux防火墙(Firewalls)
  2. GROUP BY关键字与WITH ROLLUP一起使用
  3. seaborn分类数据可视化
  4. javascript的面向对象思想知识要点
  5. Hadoop 2.7.3 分布式集群安装
  6. 并发工具类(三)控制并发线程的数量 Semphore
  7. 如何使用change命令改变密码的属性
  8. 3.纯 CSS 创作一个容器厚条纹边框特效
  9. 12.使用default-Action配置统一访问
  10. delphi const的用法