LCA 各种神奇的LCA优化方法

Posted Winniechen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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,0002N50,000), conveniently numbered 1 \ldots N1N. 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,0001K100,000). For the iith such pair, you are told two stalls s_is?i?? and t_it?i??, 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_is?i?? to t_it?i??, then it counts as being pumped through the endpoint stalls s_is?i?? and

t_it?i??, 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 yxy) 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][32],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,-1,sizeof(head));
    return ;
}
void dfs(int x,int from)
{
    dep[x]=dep[from]+1;
    fa[x][0]=from;
    for(int i=head[x];i!=-1;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=0;i<=20;i++)
    {
        if((dep1&(1<<i))!=0)
        {
            x=fa[x][i];
        }
    }
    if(x==y)
    {
        return x;
    }
    for(int i=20;i>=0;i--)
    {
        if(fa[x][i]!=fa[y][i])
        {
            x=fa[x][i];
            y=fa[y][i];
        }
    }
    return fa[x][0];
}
void renew(int x)
{
    for(int i=head[x];i!=-1;i=e[i].next)
    {
        int to1=e[i].to;
        if(to1!=fa[x][0])
        {
            renew(to1);
            sum[x]+=sum[to1]; 
        }
    }
    ans=max(ans,sum[x]);
    return ;
}
int main()
{
    init();
    scanf("%d%d",&n,&K);
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    dep[1]=1;
    dfs(1,0);
    for(int i=1;i<=20;i++)
    {
        for(int j=1;j<=n;j++)
        {
            fa[j][i]=fa[fa[j][i-1]][i-1];
        }
    }
    for(int i=1;i<=K;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        int z=lca(x,y);
        sum[z]--;
        sum[fa[z][0]]--;
        sum[x]++;
        sum[y]++;
    }
    renew(1);
    printf("%d\n",ans);
    return 0;
}
View Code

明天继续更新,今天先写到这里...

2.离线tarjan_lca

3.树链剖分_lca

 

 

题目描述

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,0002N50,000), conveniently numbered 1 \ldots N1N. 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,0001K100,000). For the iith such pair, you are told two stalls s_is?i?? and t_it?i??, 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_is?i?? to t_it?i??, then it counts as being pumped through the endpoint stalls s_is?i?? and

t_it?i??, 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 yxy) 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.

 

输入输出样例

输入样例#1:
5 10
3 4
1 5
4 2
5 4
5 4
5 4
3 5
4 3
4 3
1 3
3 5
5 4
1 5
3 4
输出样例#1:
9


以上是关于LCA 各种神奇的LCA优化方法的主要内容,如果未能解决你的问题,请参考以下文章

tarjan求lca的神奇

BZOJ 3626 [LNOI2014]LCA

P2597 [ZJOI2012]灾难 拓扑排序

bzoj3626: [LNOI2014]LCA (树链剖分)

[LNOI2014] LCA

LCA 模板