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
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
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.
4 6 2 5 1 3
1 2
2 3
3 4
4 5
4 6
【题意】给你一棵树,然后给你树的节点编号的一种排列,然后要你将这个序列分成非空的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)的更多相关文章
- HDU 6065 RXD, tree and sequence (LCA+DP)
题意:给定上一棵树和一个排列,然后问你把这个排列分成m个连续的部分,每个部分的大小的是两两相邻的LCA的最小深度,问你最小是多少. 析:首先这个肯定是DP,然后每个部分其实就是里面最小的那个LCA的深 ...
- hdu 6065 RXD, tree and sequence
题 OwO http://acm.hdu.edu.cn/showproblem.php?pid=6065 (2017 Multi-University Training Contest - Team ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- HDU 5794:A Simple Chess(Lucas + DP)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794 题意:让一个棋子从(1,1)走到(n,m),要求像马一样走日字型并只能往右下角走.里 ...
- 【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集
[题目] Tree chain problem Problem Description Coco has a tree, whose vertices are conveniently labeled ...
- HDU 1141---Brackets Sequence(区间DP)
题目链接 http://poj.org/problem?id=1141 Description Let us define a regular brackets sequence in the fol ...
- hdu 2586 How far away ?(LCA模板)(倍增法)
在dfs的过程中维护三个数组: deep[i],表示i点在树中的深度: grand[x][i],表示x的第2^i个祖先的节点编号: dis[x][i],表示x到它2^i祖 #include<io ...
- HDU6446 Tree and Permutation(树上DP)
传送门:点我 Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- HDU 4113 Construct the Great Wall(插头dp)
好久没做插头dp的样子,一开始以为这题是插头,状压,插头,状压,插头,状压,插头,状压,无限对又错. 昨天看到的这题. 百度之后发现没有人发题解,hust也没,hdu也没discuss...在acm- ...
随机推荐
- os模块学习(转)
python 文件操作 一.python中文件管理 文件管理是很多应用程序的基本功能和重要组成部分.Python可以使文件管理极其简单,特别是和其它语言相对比. 1.介绍 几乎以任何语言编写的众多 ...
- 省队集训 Day3 吴清华
[题目大意] 给网格图,共有$n * n$个关键节点,横向.纵向距离均为$d$,那么网格总长度和宽度均为$(n+1) * d + 1$,最外围一圈除了四角是终止节点.要求每个关键节点都要通过线连向终止 ...
- 课下加分项目 MYPWD 20155335 俞昆
Mypwd 的解读与实现 20155335 linux下pwd命令的编写 实验要求: 1 .学习pwd命令 2 . 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 .实现my ...
- elementui raido 单选框 循环渲染加:key
<el-radio-group v-model="adminRole"> <el-radio v-for="item in adminRoles&quo ...
- canvas_基于canvan绘制的双半圆环进度条
效果图 实现原理: 1.使用canvas绘制两个半圆弧,底图灰色半圆弧和颜色进度圆弧. 2.利用setInterval计时器,逐步改变颜色进度条,达到进度条的效果. 效果代码: <!DOCTYP ...
- End to End Sequence Labeling via Bidirectional LSTM-CNNs-CRF论文小结
本篇论文是卡内基梅隆大学语言技术研究所2016年 arXiv:1603.01354v5 [cs.LG] 29 May 2016 今天先理解一下这个是什么意思: 找到的相关理解:arXi ...
- Caffe学习笔记4图像特征进行可视化
Caffe学习笔记4图像特征进行可视化 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hit201 ...
- linux percpu机制解析【转】
转自:http://blog.csdn.net/wh8_2011/article/details/53138377 一.概述 每cpu变量是最简单也是最重要的同步技术.每cpu变量主要是数据结构数组, ...
- (转)LSI SAS 1068E Raid CentOS 5.5 安装实例浪潮NF5220系列 分类: linux
新来了一批服务器,全都是清一色的国产服务器,相同的阵列卡,令人头疼的是Linux标准内核不包含该raid驱动,需要单独安装,如果是新升级内核,肯定需要编译进去该raid驱动.一.先把主板自带的驱动光盘 ...
- pycharm模板参数
# -*- coding: utf-8 -*-# @Time : ${DATE} ${TIME}# @Author : cxa# @File : ${NAME}.py# @Software: ${PR ...