hdu 5534 Partial Tree(dp+降唯)
来源:互联网 发布:阿里云销售工资 编辑:程序博客网 时间:2024/05/16 11:25
题目链接
Partial Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1577 Accepted Submission(s): 789
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
2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)
Recommend
hujie | We have carefully selected several similar problems for you: 6216 6215 6214 6213 6212
题意:
给出
题解:
有个定理:如果对
那么问题就转化为(假设度为
求
很容易想到一个
由于要给每个点都分配度数,且度数大于等于1,这一个限制使得
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se second#define dbg(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<endl;typedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=2017+10;int d[maxn][maxn];int v[maxn];int main(){ int cas; scanf("%d",&cas); while(cas--) { int n; scanf("%d",&n); rep(i,1,n) scanf("%d",&v[i]); rep(i,2,n) v[i]-=v[1]; int ans=n*v[1]; v[1]=0; rep(i,0,n) rep(j,0,n-1) d[i][j]=-1e9; d[0][0]=0; rep(i,1,n) rep(j,0,n-1) { d[i][j]=d[i-1][j]; if(j>=i-1) d[i][j]=max(d[i][j],d[i][j-i+1]+v[i]); } ans+=d[n-1][n-2]; printf("%d\n",ans); } return 0;}
滚动数组:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se second#define dbg(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<endl;typedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=2017+10;int d[maxn];int v[maxn];int main(){ int cas; scanf("%d",&cas); while(cas--) { int n; scanf("%d",&n); rep(i,1,n) scanf("%d",&v[i]); rep(i,2,n) v[i]-=v[1]; int ans=n*v[1]; v[1]=0; rep(j,0,n-1) d[j]=-1e9; d[0]=0; rep(i,1,n) rep(j,0,n-1) { //d[i][j]=d[i-1][j]; if(j>=i-1) d[j]=max(d[j],d[j-i+1]+v[i]); } ans+=d[n-2]; printf("%d\n",ans); } return 0;}
- hdu 5534 Partial Tree(dp+降唯)
- HDU 5534 Partial Tree [DP]
- HDU 5534 Partial Tree(dp)
- HDU 5534 Partial Tree(dp优化)
- HDU 5534 Partial Tree(dp 背包)
- hdu 5534 Partial Tree 背包DP
- hdu 5534 Partial Tree
- HDU 5534 Partial Tree
- hdu 5534 Partial Tree
- HDU-5534-Partial Tree
- HDU 5534 (ACM 2015 长春) Partial Tree [基础DP]
- hdu 5534 Partial Tree 2015长春区域赛 dp 脑洞
- HDU 5534 Partial Tree(考虑树性质的dp)
- HDU 5534(Partial Tree-背包)
- hdu5534 Partial Tree(dp)
- HDU5534 Partial Tree 【DP】
- hdu 5534 Partial Tree(完全背包)
- HDU 5534 Partial Tree(完全背包)
- 5.3
- [java学习]约瑟夫环问题
- js-定时器
- 自封装简单微信平台登录和获取用户信息
- python 模块和包
- hdu 5534 Partial Tree(dp+降唯)
- Mysql| where子句过滤数据(BETWEEN,IS NULL,=,!=,...)
- 贝叶斯(二)
- python-035
- 算法进阶3-2
- linux集群自动化免密部署服务
- spring cloud的网关服务Zuul
- 在此计算机中仅有部分visual studio2010产品已升级到SP1,只有全部升级,产品才能正常运行
- TP3.2加载外部PHPexcel类,实现导入和导出