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

【题意】给你一棵树,然后给你树的节点编号的一种排列,然后要你将这个序列分成非空的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+;
const int M = ;
const int mod = 1e9+;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,k,ans;
int dep[N],fa[N][];
int a[N],lca[N];
vector<int>edg[N];
void dfs(int u,int f){
fa[u][]=f;
for(int i=;i<;i++){
fa[u][i]=fa[fa[u][i-]][i-];
}
for(int v : edg[u]){
if(v==f)continue;
dep[v]=dep[u]+;
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=;i>=;i--){
if(dep[fa[u][i]]>=dep[v]){
u=fa[u][i];
}
}
if(u==v)return (u);
for(int i=;i>=;i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];v=fa[v][i];
}
}
return (fa[u][]);
} int main(){
while(~scanf("%d%d",&n,&k)){
int dp[n+][k+];
met(dp,inf);
for(int i=;i<=n;i++)scanf("%d",&a[i]),edg[i].clear();
for (int i=,u,v;i<n;i++){
scanf("%d%d",&u,&v);
edg[u].pb(v);
edg[v].pb(u);
}
dep[]=;
dfs(,);
dp[][]=;
for(int i=;i<=n;i++){
int m = LCA(a[i-],a[i]);
lca[i]=dep[m];
}
for(int i=;i<=n;i++){
for(int j=;j<=min(i,k);j++){
if(i->=j)dp[i][j]=min(dp[i][j],dp[i-][j]);
if(j>)dp[i][j]=min(dp[i][j],dp[i-][j-]+dep[a[i]]);
if(j>&&i>=&&j-<=i-)dp[i][j]=min(dp[i][j],dp[i-][j-]+lca[i]);
}
}
printf("%d\n",dp[n][k]);
}
return ;
}

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

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

    题意:给定上一棵树和一个排列,然后问你把这个排列分成m个连续的部分,每个部分的大小的是两两相邻的LCA的最小深度,问你最小是多少. 析:首先这个肯定是DP,然后每个部分其实就是里面最小的那个LCA的深 ...

  2. hdu 6065 RXD, tree and sequence

    题 OwO http://acm.hdu.edu.cn/showproblem.php?pid=6065 (2017 Multi-University Training Contest - Team ...

  3. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  4. HDU 5794:A Simple Chess(Lucas + DP)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794 题意:让一个棋子从(1,1)走到(n,m),要求像马一样走日字型并只能往右下角走.里 ...

  5. 【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集

    [题目] Tree chain problem Problem Description Coco has a tree, whose vertices are conveniently labeled ...

  6. HDU 1141---Brackets Sequence(区间DP)

    题目链接 http://poj.org/problem?id=1141 Description Let us define a regular brackets sequence in the fol ...

  7. hdu 2586 How far away ?(LCA模板)(倍增法)

    在dfs的过程中维护三个数组: deep[i],表示i点在树中的深度: grand[x][i],表示x的第2^i个祖先的节点编号: dis[x][i],表示x到它2^i祖 #include<io ...

  8. HDU6446 Tree and Permutation(树上DP)

    传送门:点我 Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (J ...

  9. HDU 4113 Construct the Great Wall(插头dp)

    好久没做插头dp的样子,一开始以为这题是插头,状压,插头,状压,插头,状压,插头,状压,无限对又错. 昨天看到的这题. 百度之后发现没有人发题解,hust也没,hdu也没discuss...在acm- ...

随机推荐

  1. 51Nod 1003 阶乘后面0的数量 | 思维

    题意:n的阶乘后面0的个数,如果直接算出阶乘再数0的数量一定会超时的. 因为10=2*5,所以求出5贡献的次数就行. #include "bits/stdc++.h" using ...

  2. 【C++ STL】Map和Multimap

    1.结构 Map和multimap将key/value pair(键值/实值 队组)当作元素,进行管理.他们根据key的排序准则将元素排序.multimap允许重复元素,map不允许. 元素要求: k ...

  3. 诱惑当前 你的孩子能hold住吗?

    1.打好态度.动机.价值观基础 培养未成熟主体远大的志向,并不是向他们讲一些抽象的道理,而是根据他们的年龄特点,从形象的故事.童话开始,从身边的人物.事例出发,逐渐渗透一些人生的哲理.有一位自觉性非常 ...

  4. Windows下端口占用查看

    假如我们需要确定谁占用了我们的80端口 1.Windows平台在windows命令行窗口下执行:C:\>netstat -aon|findstr "80" TCP     1 ...

  5. 【51NOD-5】1293 球与切换器

    [算法]DP [题解]f[i][j][0]表示在i,j位置往下走的球数,f[i][j][1]表示在i,j位置往右走的球数,经过i,j的球若为-1则(num+1)/2往下,其余往右.+1类似. 转移见代 ...

  6. Spring boot 集成Dubbox(山东数漫江湖)

    前言 因为工作原因,需要在项目中集成dubbo,所以去查询dubbo相关文档,发现dubbo目前已经不更新了,所以把目光投向了dubbox,dubbox是当当网基于dubbo二次开发的一个项目,dub ...

  7. 【COGS2622】后缀平衡树

    这是个后缀平衡树的裸题.... 然后傻逼的我调了一下午. #include<bits/stdc++.h> typedef long long ll; using namespace std ...

  8. 【模板】SPOJ FACT0 大数分解 miller-rabin & pollard-rho

    http://www.spoj.com/problems/FACT0/en/ 给一个小于1e15的数,将他分解. miller-rabin & pollard-rho模板 #include & ...

  9. docker数据管理--数据卷的备份

    /* 先在宿主机创建一个备份的文 件夹, 然后将其以另外一个名字的目录挂载到容器里, 此时不管容器里,或宿主机里做什么操作, 数据都会及时更新,并得到备份. */ [root@localhost ~] ...

  10. 通过第三方组件NPOI读取Excel的方法

    public class ExcelHelper { public class x2003 { #region Excel2003 /// <summary> /// 将Excel文件中的 ...