hdu 5534 Partial Tree(dp+降唯)
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;
}