Partial Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 228    Accepted Submission(s): 138

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
 
Source
 
题意:一个树的权值被定义为sigma(f(di), 1<=i<=n)其中di是第i个点的度数,求一个n个点的数的最大权值
分析:先把每个点的度数当成1,然后问题转化为总度数为2n-2,现有度数n,求增加n-2度数的最大权值
dp[i]表示增加i度数的最大权值
dp[i] = max(f[i+1], dp[j]+dp[i-j]), 1<=j<=i/2
完了
 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define MLL (1000000000000000001LL)
#define INF (1000000001)
#define For(i, s, t) for(int i = (s); i <= (t); i ++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i --)
#define Rep(i, n) for(int i = (0); i < (n); i ++)
#define Repn(i, n) for(int i = (n)-1; i >= (0); i --)
#define mk make_pair
#define ft first
#define sd second
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define sz(x) ((int) (x).size())
#define clr(x, y) (memset(x, y, sizeof(x)))
inline void SetIO(string Name)
{
string Input = Name + ".in";
string Output = Name + ".out";
freopen(Input.c_str(), "r", stdin);
freopen(Output.c_str(), "w", stdout);
} inline int Getint()
{
char ch = ' ';
int Ret = ;
bool Flag = ;
while(!(ch >= '' && ch <= ''))
{
if(ch == '-') Flag ^= ;
ch = getchar();
}
while(ch >= '' && ch <= '')
{
Ret = Ret * + ch - '';
ch = getchar();
}
return Ret;
} const int N = ;
int n, F[N];
int Dp[N]; inline void Solve(); inline void Input()
{
int TestNumber = Getint();
while(TestNumber--)
{
n = Getint();
For(i, , n - ) F[i] = Getint();
Solve();
}
} inline void Solve()
{
For(i, , n - ) F[i] -= F[];
Dp[] = ;
For(i, , n - )
{
Dp[i] = F[i + ];
For(j, , (i / ) + )
Dp[i] = max(Dp[i], Dp[j] + Dp[i - j]);
}
printf("%d\n", Dp[n - ] + n * F[]);
} int main()
{
Input();
//Solve();
return ;
}

最新文章

  1. 使用Apache的DigestUtils类实现哈希摘要(SHA/MD5)
  2. 找window的三种方法
  3. maven异常
  4. web压力测试工具
  5. (转)阴影锥(Shadow Volume)
  6. 【转】IOS屏幕旋转与View的transform属性之间的关系,比较底层
  7. 解决在 使用 AjaxFileUploder 插件时,不能获取返回的 json 结果数据
  8. (转)Maven实战(二)构建简单Maven项目
  9. Libgdx开发ios游戏
  10. 前端魔法堂——异常不仅仅是try/catch
  11. MYSQL数据库学习十八 数据库维护和性能提高
  12. G1 GC技术解析
  13. LOJ2831 JOISC2018 道路建设 LCT、树状数组
  14. 十二.HTTPS网站安全访问实践
  15. lua --- 表操作
  16. ztree删除某个节点下的全部子节点后,父节点图标还是文件夹
  17. 解决xadmin登录卡顿延迟的问题
  18. 音视频处理ffmpeg使用
  19. Easy Graphics Engine vs2015使用
  20. JBOSS安装与配置搭建本地项目环境(方便前端开发调式)

热门文章

  1. IOS开发的目录结构
  2. Centos下samba共享打印机
  3. 【Spring】Spring系列7之Spring整合MVC框架
  4. 什么是iis服务器
  5. CARP 使用笔记
  6. iOS 使用Storyboard 和 xib时的一些知识
  7. php中static静态关键字的使用
  8. css3学习总结2--CSS3圆角边框
  9. CUDA学习笔记(三)——CUDA内存
  10. CSS备忘