LCA 各种神奇的LCA优化方法
LCA(Least Common Ancestors)
树上问题的一种。
朴素lca很简单啦,我就不多说了,时间复杂度n^2
1.倍增LCA
时间复杂度 nlongn+klogn
其实是一种基于朴素lca的优化方法,
朴素lca只能一层层的向上查询,而这个有一定状态压缩的想法
即每一次跳2^i层,让O(n)的查找变成O(logn)。
以上就是我对倍增lca的理解。
以洛谷P3128为例,
[USACO15DEC]最大流Max Flow
题目描述
Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls in his barn (2 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1 \ldots N1…N. Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.
FJ is pumping milk between KK pairs of stalls (1 \leq K \leq 100,0001≤K≤100,000). For the iith such pair, you are told two stalls s_isi and t_iti, endpoints of a path along which milk is being pumped at a unit rate. FJ is concerned that some stalls might end up overwhelmed with all the milk being pumped through them, since a stall can serve as a waypoint along many of the KKpaths along which milk is being pumped. Please help him determine the maximum amount of milk being pumped through any stall. If milk is being pumped along a path from s_isi to t_iti, then it counts as being pumped through the endpoint stalls s_isi and
t_iti, as well as through every stall along the path between them.
FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N。所有隔间都被管道连通了。
FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。
输入输出格式
输入格式:
The first line of the input contains NN and KK.
The next N-1N−1 lines each contain two integers xx and yy (x \ne yx≠y) describing a pipe
between stalls xx and yy.
The next KK lines each contain two integers ss and tt describing the endpoint
stalls of a path through which milk is being pumped.
输出格式:
An integer specifying the maximum amount of milk pumped through any stall in the
barn.
差分数组+倍增LCA
先DFS建树,再倍增求lca,最后在求lca时维护一个sum数组(表示被遍历的次数),最后再一遍dfs求出最大被遍历次数
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define N 100001
int n,K;
struct node
{
int to,next;
}e[N];
int ans;
int dep[N],fa[N][],head[N],cnt,sum[N];
void add(int x,int y)
{
e[cnt].to=y;
e[cnt].next=head[x];
head[x]=cnt++;
return ;
}
void init()
{
memset(head,-,sizeof(head));
return ;
}
void dfs(int x,int from)
{
dep[x]=dep[from]+;
fa[x][]=from;
for(int i=head[x];i!=-;i=e[i].next)
{
if(e[i].to!=from)
{
dfs(e[i].to,x);
}
}
return ;
}
int lca(int x,int y)
{
if(dep[x]<dep[y])
{
swap(x,y);
}
int dep1=dep[x]-dep[y];
for(int i=;i<=;i++)
{
if((dep1&(<<i))!=)
{
x=fa[x][i];
}
}
if(x==y)
{
return x;
}
for(int i=;i>=;i--)
{
if(fa[x][i]!=fa[y][i])
{
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][];
}
void renew(int x)
{
for(int i=head[x];i!=-;i=e[i].next)
{
int to1=e[i].to;
if(to1!=fa[x][])
{
renew(to1);
sum[x]+=sum[to1];
}
}
ans=max(ans,sum[x]);
return ;
}
int main()
{
init();
scanf("%d%d",&n,&K);
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dep[]=;
dfs(,);
for(int i=;i<=;i++)
{
for(int j=;j<=n;j++)
{
fa[j][i]=fa[fa[j][i-]][i-];
}
}
for(int i=;i<=K;i++)
{
int x,y;
scanf("%d%d",&x,&y);
int z=lca(x,y);
sum[z]--;
sum[fa[z][]]--;
sum[x]++;
sum[y]++;
}
renew();
printf("%d\n",ans);
return ;
}
明天继续更新,今天先写到这里...
2.离线tarjan_lca
时间复杂度O(n)
唯一一个lcaO(n)的优化方法...
但是实用性低,代码难写,思想复杂...
并查集+树形DP+离线思想
把查询离线,一遍dfs遍历,查出lca并储存
例题 JLOI2014松鼠的新家
题解 查分数组+tarjanlca
JLOI2014松鼠的新家
Description
松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的。天哪,他居然真的住在“树”上。松鼠想邀请小熊维尼前来参观,并且还指定一份参观指南,他希望维尼能够按照他的指南顺序,先去a1,再去a2,……,最后到an,去参观新家。
可是这样会导致维尼重复走很多房间,懒惰的维尼不听地推辞。可是松鼠告诉他,每走到一个房间,他就可以从房间拿一块糖果吃。维尼是个馋家伙,立马就答应了。
现在松鼠希望知道为了保证维尼有糖果吃,他需要在每一个房间各放至少多少个糖果。因为松鼠参观指南上的最后一个房间an是餐厅,餐厅里他准备了丰盛的大餐,所以当维尼在参观的最后到达餐厅时就不需要再拿糖果吃了。
Input
第一行一个整数n,表示房间个数
第二行n个整数,依次描述a1-an
接下来n-1行,每行两个整数x,y,表示标号x和y的两个房间之间有树枝相连。
Output
一共n行,第i行输出标号为i的房间至少需要放多少个糖果,才能让维尼有糖果吃。
Sample Input
Sample Output
HINT
30%的数据,n<=4000
80%的数据,n<=50000
100%的数据,2<= n <=300000
3.树链剖分_lca
树剖的用处实在很多
时间复杂度:O(nlogn)
定义:
树链:就是树上的路径。
剖分:就是把路径分类为重链和轻链。
树链剖分:把一棵树剖分为若干条链,然后利用数据结构(树状数组,SBT,Splay,线段树等等)去维护每一条链,复杂度为O(logn)
重儿子:siz[v]为u的子节点中siz值最大的,那么v就是u的重儿子。
轻儿子:u的其它子节点。
重边:点u与其重儿子的连边。
轻边:点u与其轻儿子的连边。
重链:由重边连成的路径。
轻链:轻边。剖分后的树有如下性质:
性质1:如果(v,u)为轻边,则siz[v] * 2 <= siz[u];性质2:从根到某一点的路径上轻链、重链的个数都不大于logn。
那么,树链剖分的第一步当然是对树进行轻重边的划分。剖分过程分为两次dfs,或者bfs也可以。
如果是两次dfs,那么第一次dfs就是找重边,也就是记录下所有的重边。
然后第二次dfs就是连接重边形成重链,具体过程就是:以根节点为起点,沿着重边向下拓展,拉成重链,不在当前重链上的节点,都以该节点为起点向下重新拉一条重链。
剖分完毕后,每条重链相当于一段区间,然后用数据结构去维护,把所有重链首尾相接,放到数据结构上,然后维护整体。
在这里,当然有很多数组,现在我来分别介绍它们的作用:
size1[]数组,用来保存以x为根的子树节点个数
anc[]数组,用来保存当前节点的所在链的顶端节点
son[]数组,用来保存重儿子
dep[]数组,用来保存当前节点的深度
fa[]数组,用来保存当前节点的父亲
Nearest Common Ancestors
Description
给定N个节点的一棵树,有K次查询,每次查询a和b的最近公共祖先。
样例中的16和7的公共祖先(LCA:Least Common Ancestors)是4。
Input
第一行两个整数N(1 < N <= 105)、K(1 <= K <= 105)
第2~N行,每行两个整数a、b(1 <= a,b <= N),表示a是b的父亲。
第N+1~N+K+1行,每行两个整数a、b(1 <= a,b <= N),表示询问a和b的最近公共祖先是谁。
Output
输出K行,第i行表示第i个查询的最近公共祖先是谁。
Sample Input
Sample Output
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
#define N 100005
int n,K;
struct node
{
int next,to;
}e[*N];
int head[N],dep[N],anc[N],son[N],fa[N];
int in1[N],cnt,size1[N],root;
void init()
{
memset(head,-,sizeof(head));
return ;
}
void add(int x,int y)
{
e[cnt].to=y;
e[cnt].next=head[x];
head[x]=cnt++;
return ;
}
void dfs1(int x,int from)
{
fa[x]=from;
size1[x]=;
for(int i=head[x];i!=-;i=e[i].next)
{
int to1=e[i].to;
if(to1!=from)
{
dep[to1]=dep[x]+;
dfs1(to1,x);
size1[x]+=size1[to1];
if(size1[to1]>size1[son[x]])
{
son[x]=to1;
}
}
}
return ;
}
void dfs2(int x,int top1)
{
anc[x]=top1;
if(son[x])
{
dfs2(son[x],top1);
}
for(int i=head[x];i!=-;i=e[i].next)
{
int to1=e[i].to;
if(to1!=fa[x]&&to1!=son[x])
{
dfs2(to1,to1);
}
}
return ;
}
int getlca(int x,int y)
{
int ancx,ancy;
while()
{
ancx=anc[x],ancy=anc[y];
if(ancx==ancy)break;
if(dep[ancx]<dep[ancy])
{
y=fa[ancy];
}else
{
x=fa[ancx];
}
}
if(dep[x]<dep[y])
{
return x;
}
return y;
}
int main()
{
init();
scanf("%d%d",&n,&K);
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
in1[y]++;
}
for(int i=;i<=n;i++)
{
if(in1[i]==)
{
root=i;
dfs1(i,);
}
}
dfs2(root,root);
for(int i=;i<=K;i++)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",getlca(x,y));
}
return ;
}
4.lca转RMQ
依据DFS序,用RMQ优化求区间最小值(dep的值)
而区间最小值即为所求
样题同上....
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
using namespace std;
#define N 200005
int n,m,in1[N],a[N*];
struct node
{
int to,next;
}e[N];
int head[N],place[N*][],dep[N*],minn[N*][],cnt,num;
void init()
{
memset(head,-,sizeof(head));
return ;
}
void add(int x,int y)
{
e[cnt].to=y;
e[cnt].next=head[x];
head[x]=cnt++;
return ;
}
void dfs(int from,int x)
{
a[x]=++num;
place[num][]=x;
dep[num]=dep[a[from]]+;
minn[num][]=dep[num];
for(int i=head[x];i!=-;i=e[i].next)
{
int to1=e[i].to;
if(to1!=from)
{
dfs(x,to1);
a[x]=++num;
dep[num]=dep[a[from]]+;
minn[num][]=dep[num];
place[num][]=x;
}
}
return ;
}
int main()
{
init();
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
in1[y]++;
}
for(int i=;i<=n;i++)
{
if(in1[i]==)
{
dfs(,i);
break;
}
}
for(int j=;j<=;j++)
{
for(int i=;i<=*n-;i++)
{
if(i+(<<j-)>n*-)break;
if(minn[i][j-]<minn[i+(<<(j-))][j-])
{
minn[i][j]=minn[i][j-];
place[i][j]=place[i][j-];
}else
{
minn[i][j]=minn[i+(<<(j-))][j-];
place[i][j]=place[i+(<<(j-))][j-];
}
}
}
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(a[x]>a[y])
{
swap(x,y);
}
int len=a[y]-a[x]+;
int t=(int)log2(len);
if(minn[a[x]][t]<minn[a[y]-(<<t)+][t])
{
printf("%d\n",place[a[x]][t]);
}else
{
printf("%d\n",place[a[y]-(<<t)+][t]);
}
}
return ;
}
膜拜众大佬,如有问题,请帮我指出,O(∩_∩)O谢谢!
LCA 各种神奇的LCA优化方法的更多相关文章
- 提升网速的路由器优化方法(UPnP、QoS、MTU、交换机模式、无线中继)
在上一篇<为什么房间的 Wi-Fi 信号这么差>中,猫哥从微波炉.相对论.人存原理出发,介绍了影响 Wi-Fi 信号强弱的几大因素,接下来猫哥再给大家介绍几种不用升级带宽套餐也能提升网速的 ...
- php-fpm优化方法详解
php-fpm优化方法 php-fpm存在两种方式,一种是直接开启指定数量的php-fpm进程,不再增加或者减少:另一种则是开始时开启一定数量的php-fpm进程,当请求量变大时,动态的增加php-f ...
- 30多条mysql数据库优化方法,千万级数据库记录查询轻松解决(转载)
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- Android中ListView的几种常见的优化方法
Android中的ListView应该算是布局中几种最常用的组件之一了,使用也十分方便,下面将介绍ListView几种比较常见的优化方法: 首先我们给出一个没有任何优化的Listview的Adapte ...
- php-fpm进程数优化方法
原文地址:https://www.douban.com/note/315222037/ 背景最近将Wordpress迁移至阿里云.由于自己的服务器是云服务器,硬盘和内存都比较小,所以内存经常不够使,通 ...
- DevExpress ChartControl大数据加载时有哪些性能优化方法
DevExpress ChartControl加载大数据量数据时的性能优化方法有哪些? 关于图表优化,可从以下几个方面解决: 1.关闭不需要的可视化的元素(如LineMarkers, Labels等) ...
- Tomcat从内存、并发、缓存方面优化方法
Tomcat有很多方面,从内存.并发.缓存四个方面介绍优化方法. 一.Tomcat内存优化 Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 cata ...
- 股票投资组合-前进优化方法(Walk forward optimization)
code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...
- Caffe学习系列(8):solver优化方法
上文提到,到目前为止,caffe总共提供了六种优化方法: Stochastic Gradient Descent (type: "SGD"), AdaDelta (type: &q ...
随机推荐
- 高并发教程-基础篇-之nginx负载均衡的搭建
温馨提示:请不要盲目的进行横向扩展,优先考虑对单台服务器的性能优化,只有单台服务器的性能达到最优化之后,集群才会被最大的发挥作用. 一.架构图: 服务器准备:3台,ubuntu16.04系统maste ...
- java小知识点汇总
1.ConcurrentHashMap使用segment来分段和管理锁,segment继承自ReentrantLock,因此ConcurrentHashMap使用ReentrantLock来保证线程安 ...
- 使用lombok的@Data @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode注解,编译时报错 找不到符号
使用lombok添加@AllArgsConstructor后报错"错误:找不到符号 符号: 问题:未启用lombok注解 解决: settings->build->compile ...
- classes目录中没有class文件的一个原因
可能是你的build设置有问题:比如本来有的jar被删除的情况下.build不会报错,但是classes目录下什么都没有.
- PyCOn2013大会笔记
DAE的设计 By洪强宁 hongon@douban.com 3个aaS服务都不能模块化灵活组合服务 DAE的起因:代码横向拆分模块化,重用基础设施 最佳实践对新App复用 Scale SA D ...
- sql server数据字符串分割功能sql
--分割字符串函数 create FUNCTION [dbo].[GetSplitStringValueInIndex] ( ), --要分割的字符串 ), --分隔符号 @index INT --取 ...
- SAP中的BRF+
即便努力去尝试最小化SAP系统中的自定义内容,大量自定义业务逻辑通常还是无法避免的.在过去,这意味着需要在系统的各种地方引入自定义ABAP代码,包括user-exits,enhancement,BAd ...
- 第二届强网杯部分writeup
MD5部分 第一题 一看就有些眼熟 emmmm 查看一下源代码: 重点是这里 这里面要求POST上去的参数 param1 != param2 && md5('param1') == m ...
- 在线引用js资源积累
[jQuery]https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js[Vue]https://cdnjs.cloudfla ...
- echarts3 迁徙图 迁入迁出
geoCoordMap = { '上海': [121.4648,31.2891], '佛山': [112.8955,23.1097], '保定': [115.0488,39.0948], '兰州': ...