1. 程式人生 > >hdu 5534 Partial Tree(dp+降唯)

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

題意:

給出 N 個結點我們需要新增 N1 條邊使得它變成一棵樹,定義一個函式 f(x) ,給出它f(1),f(2)...f(N1) 的值,一個度為 d 的結點的炫酷值是 f(d),一棵樹的炫酷值是所有結點的炫酷值之和,問 N 個結點的樹的最大炫酷值。

題解:

有個定理:如果對 N 個結點指定其度,而且每個度都大於0且所有結點度之和為 2×N2,那麼能夠構造出滿足的一棵樹。

那麼問題就轉化為(假設度為 i 的點有 xi 個)

xi=N

xi×i=2×N2

max(f(i)×xi)

很容易想到一個 O(N3)dp ,令 d[i][j] 表示分配了 i 個點度數和為 j 的最大炫酷值,那麼答案就是 d[N][N22]。可是會超時。

由於要給每個點都分配度數,且度數大於等於1,這一個限制使得 dp 方程多了一維,因此我們可以先將每個結點分配度為1,每個 f(i)f(1),此時問題相當於一個完全揹包,O(N2) 可解。

#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;
}