HDU 6065 RXD, tree and sequence (LCA DP)

RXD, tree and sequence

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 234    Accepted Submission(s): 82

Problem Description

RXD has a rooted tree T with size n, the root ID is 1, with the depth of 1
RXD has a permutation P with size n.
RXD wants to divide the permutaion into k continuous parts 
For each part, he would calculate the depth of the least common ancestor of the part. And finally accumulate them. 
He wants to make the final result minimized. 
Please calculate the minimal answer.
1≤k≤n≤3×105,n×k≤3×105

Input

There are several test cases, please keep reading until EOF.
For each test case, the first line consists of 2 integer n,k, which means the number of the tree nodes and the size of the permutaion, and k means the number of parts.
The next line consists of n different integers, which means the permutation P.
The next n−1 lines consists of 2 integers, a,b, means a tree edge.
It is guaranteed that the edges would form a tree.
There are 6 test cases.

Output

For each test case, output an integer, which means the answer.

Sample Input

6 3
4 6 2 5 1 3
1 2
2 3
3 4
4 5
4 6

Sample Output

6

Source

2017 Multi-University Training Contest - Team 3

【题意】给你一棵树,然后给你树的节点编号的一种排列,然后要你将这个序列分成非空的k份,对于每一份求lca的深度,然后累加,   求和最小。

【分析】首先得想到区间lca的一个性质,对于区间[i,j],他们的lca肯定是lca[i,i+1],lca[i+1,i+2],...lca[j-1,j]中的某一个,也就是说,对于某一个区间,决定lca的只有某相邻的两个数。然后在这个序列中,对于某一个数,他有三种存在状态。第一,它存在于某个区间,但是这个区间的lca不是由它决定的。第二,它一个数本身作为一份。第三,它和它左边的数的lca作为某一份的lca。然后dp[i][j]表示前i个数分成j段的最小答案,对于上面三种情况分别对应三个方程

dp[i][j]=min(dp[i][j],dp[i-1][j]);
dp[i][j]=min(dp[i][j],dp[i-1][j-1]+dep[a[i]]);
dp[i][j]=min(dp[i][j],dp[i-2][j-1]+lca[i]);

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 5e5+50;
const int M = 16000009;
const int mod = 1e9+7;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,k,ans;
int dep[N],fa[N][20];
int a[N],lca[N];
vector<int>edg[N];
void dfs(int u,int f){
    fa[u][0]=f;
    for(int i=1;i<20;i++){
        fa[u][i]=fa[fa[u][i-1]][i-1];
    }
    for(int v : edg[u]){
        if(v==f)continue;
        dep[v]=dep[u]+1;
        dfs(v,u);
    }
}
int LCA(int u,int v){
    int U=u,V=v;
    if(dep[u]<dep[v])swap(u,v);
    for(int i=19;i>=0;i--){
        if(dep[fa[u][i]]>=dep[v]){
            u=fa[u][i];
        }
    }
    if(u==v)return (u);
    for(int i=19;i>=0;i--){
        if(fa[u][i]!=fa[v][i]){
            u=fa[u][i];v=fa[v][i];
        }
    }
    return (fa[u][0]);
}

int main(){
    while(~scanf("%d%d",&n,&k)){
        int dp[n+50][k+50];
        met(dp,inf);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]),edg[i].clear();
        for (int i=1,u,v;i<n;i++){
            scanf("%d%d",&u,&v);
            edg[u].pb(v);
            edg[v].pb(u);
        }
        dep[1]=1;
        dfs(1,0);
        dp[0][0]=0;
        for(int i=2;i<=n;i++){
            int m = LCA(a[i-1],a[i]);
            lca[i]=dep[m];
        }
        for(int i=1;i<=n;i++){
            for(int j=0;j<=min(i,k);j++){
                if(i-1>=j)dp[i][j]=min(dp[i][j],dp[i-1][j]);
                if(j>0)dp[i][j]=min(dp[i][j],dp[i-1][j-1]+dep[a[i]]);
                if(j>0&&i>=2&&j-1<=i-2)dp[i][j]=min(dp[i][j],dp[i-2][j-1]+lca[i]);
            }
        }
        printf("%d\n",dp[n][k]);
    }
    return 0;
}
时间: 2024-10-23 01:00:03

HDU 6065 RXD, tree and sequence (LCA DP)的相关文章

HDU 6065 RXD, tree and sequence (LCA+DP)

题意:给定上一棵树和一个排列,然后问你把这个排列分成m个连续的部分,每个部分的大小的是两两相邻的LCA的最小深度,问你最小是多少. 析:首先这个肯定是DP,然后每个部分其实就是里面最小的那个LCA的深度.很容易知道某个区间的值肯定是 [li, li+1] .. [ri-1, ri]这些区间之间的一个,并且我们还可以知道,举个例子,1 2 3 4  5 6 如果知道分成两部分 其中 2 和 6 是最优的,那么中间的 3 4 5 ,这三个数其实属于哪个区间都无所谓,所以对于第 i 个数只有三种可能.

【HDU 5828】Rikka with Sequence(线段树)

[HDU 5828]Rikka with Sequence(线段树) Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2311    Accepted Submission(s): 391 Problem Description As we know, Rikka is poor at math.

【HDU 5647】DZY Loves Connecting(树DP)

pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 112 Problem Description DZY has an unroote

hdu 5136 Yue Fei&#39;s Battle(计数DP)

Yue Fei's Battle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 151    Accepted Submission(s): 48 Problem Description Yue Fei is one of the most famous military general in Chinese history.He

hdu 4865 Peter&amp;#39;s Hobby(概率dp)

http://acm.hdu.edu.cn/showproblem.php? pid=4865 大致题意:有三种天气和四种叶子状态.给出两个表,各自是每种天气下叶子呈现状态的概率和今天天气对明天天气的概率. 给出n天叶子的状态.输出最有可能的天气序列. 思路:wl[i][j]表示天气为i,叶子为j的概率,ww[i][j]表示今天天气为i明天天气为j的概率,st[i]表示第一天天气为i的概率. 对于叶子序列{a1,a2......an},存在一个天气序列{b1,b2......bn},那么总的概率

HDU 5794:A Simple Chess(Lucas + DP)

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794 题意:让一个棋子从(1,1)走到(n,m),要求像马一样走日字型并只能往右下角走.里面还有r个障碍点不能经过或者到达,问有多少种走法可以走到(n,m). 思路:画个图可以发现走的点像一个斜着的杨辉三角.所以可以得到一个从点 i 走到点 j 的路径数是一个组合数. 大概就是长这样,杨辉三角的每个点的数如下. 1 1       1 1      2      1 1       3 

hdu 1024 Max Sum Plus Plus(简单dp)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:给定一个数组,求其分成m个不相交子段和的最大值. 这题有点问题其实m挺小的但题目并没有给出. dp[i][j]表示取第i 位的数共取了j段然后转移方程显然为 dp[i][j]=max(dp[i - 1][j]+a[j] , max(dp[j - 1][j - 1]~dp[i - 1][j - 1]))(大致意思是取第i位要么i-1位取了j个那么a[j]刚好能与i-1拼成一段,或者j -

HDU 4283 You Are the One (区间dp)

HDU 4283 题意:有n个男屌丝依次排队要登台,如果某个男屌丝前面排有k个人,则该屌丝很生气,生气程度 = (k-1)*D(D代表屌丝程度).现在有一个小黑屋,小黑屋先进后出,如果把某屌丝放进去,那么他后面的人就能先登台.给出每个人的屌丝程度与原定上台顺序,求怎样利用小黑屋,能够使众屌丝生气程度之和最小,求出最小值. 思路: #define 愤怒值 生气程度 dp[i][j]表示从第i个屌丝到第j个屌丝这段区间的min(sum(愤怒值))(假设只有这j-i+1个屌丝) 那么对于dp[i][j

hdu 1087 Super Jumping! Jumping! Jumping!(动态规划DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24452    Accepted Submission(s): 10786 Problem Description No