Tarjan离线求LCA,倍增法求LCA

定 义 : 定义:
最 近 公 共 祖 先 简 称 L C A ( L o w e s t C o m m o n A n c e s t o r ) 。 两 个 节 点 的 最 近 公 共 祖 先 , 最近公共祖先简称 LCA(Lowest Common Ancestor)。两个节点的最近公共祖先, LCALowestCommonAncestor
就 是 这 两 个 点 的 公 共 祖 先 里 面 , 离 根 最 远 的 那 个 。 就是这两个点的公共祖先里面,离根最远的那个。

Tarjan(离线)算法的基本思路及其算法实现

. . . ... ...
P3379 【模板】最近公共祖先(LCA)

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
typedef unsigned long long ull;
inline int read(){int s=0,w=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn = 5e5+5;
int tot,head[maxn];
struct Edge{
    int to,next;
}edge[maxn<<2];
struct Node{
    int to,next,id;
}node[maxn<<2];
int Tot,Head[maxn];
int pre[maxn];
int lca[maxn];
void add_edge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void add(int u,int v,int id)
{
    node[Tot].to=v;
    node[Tot].next=Head[u];
    node[Tot].id=id;
    Head[u]=Tot++;
}
int find(int x)
{
    if(x!=pre[x]) pre[x]=find(pre[x]);
    return pre[x];
}
bool vis[maxn];
void Tarjan(int x)
{
    vis[x]=true;
    for(int i=head[x];i!=-1;i=edge[i].next) // Tarjan 核心
    {
        int y=edge[i].to;
        if(vis[y]) continue;
        Tarjan(y);
        pre[y]=x;
    }
    for(int i=Head[x];i!=-1;i=node[i].next)  // 离线部分
    {
        int y=node[i].to,id=node[i].id;
        if(vis[y]) lca[id]=find(y);
    }
}
int main()
{
    int n=read(),m=read(),s=read();
    memset(Head,-1,sizeof(Head));
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++) pre[i]=i;

    for(int i=1;i<n;i++)
    {
        int u=read(),v=read();
        add_edge(u,v);
        add_edge(v,u);
    }
    for(int i=1;i<=m;i++)
    {
        int u=read(),v=read();
        add(u,v,i);
        add(v,u,i);
    }
    Tarjan(s);
    for(int i=1;i<=m;i++)
        printf("%d\n",lca[i]);
}

倍增法:

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
typedef unsigned long long ull;
inline int read(){int s=0,w=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn = 5e5+5;
int head[maxn],tot;
struct E{
    int to,next;
}e[maxn<<1];
void add(int u,int v)
{
    e[tot]={v,head[u]};
    head[u]=tot++;
}
// 倍增法求LCA 模板
int f[maxn][50],lg[maxn],dep[maxn];
void dfs(int x,int fa)
{
    f[x][0]=fa,dep[x]=dep[fa]+1;

//    for(int i=1;i<=lg[dep[x]];i++)
//        f[x][i]=f[f[x][i-1]][i-1];
    for(int i=1;(1<<i)<=dep[x];i++)
        f[x][i]=f[f[x][i-1]][i-1];

    for(int i=head[x];i!=-1;i=e[i].next)
        if(e[i].to!=fa) dfs(e[i].to,x);
}
int LCA(int x,int y)
{
    if(dep[x]<dep[y]) swap(x,y);

    while (dep[x]>dep[y]) x=f[x][lg[dep[x]-dep[y]]-1];

    if(x==y) return x;

    for(int i=lg[dep[x]]-1;i>=0;i--)
        if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];

    return f[x][0];
}
int main()
{
    int n=read(),m=read(),s=read();
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n-1;i++)
    {
        int u=read(),v=read();
        add(u,v),add(v,u);
    }
    for(int i=1;i<=n;i++)
        lg[i]=lg[i-1]+(1<<lg[i-1]==i);
    dfs(s,0);
    for(int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        printf("%d\n",LCA(x,y));
    }
    return 0;
}

Nearest Common Ancestors

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
typedef unsigned long long ull;
inline int read(){int s=0,w=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn = 1e5+5;
int tot,head[maxn];
struct Edge{
    int to,next;
}edge[maxn<<2];
int pre[maxn];
void add_edge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
int find(int x)
{
    if(x!=pre[x]) pre[x]=find(pre[x]);
    return pre[x];
}
bool vis[maxn];
int In[maxn],U,V;
bool flag;
void Tarjan(int x)
{
    if(flag) return;
    vis[x]=true;
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        int y=edge[i].to;
        if(vis[y]) continue;;
        Tarjan(y);
        pre[y]=x;
    }
    if(x==U&&vis[V]&&!flag){
        flag=true;
        printf("%d\n",find(V));
        return;
    }
    if(x==V&&vis[U]&&!flag){
        flag=true;
        printf("%d\n",find(U));
        return;
    }
}
int main()
{
    int t=read();

    while (t--)
    {
        int n=read();

        tot=0;
        flag=false;
        memset(vis,false,sizeof(vis));
        memset(head,-1,sizeof(head));
        memset(In,0,sizeof(In));

        for(int i=1;i<=n;i++) pre[i]=i;

        for(int i=1;i<n;i++)
        {
            int u=read(),v=read();
            add_edge(u,v);
            In[v]++;
        }
        U=read(),V=read();
        for(int i=1;i<=n;i++)
            if(In[i]==0){
                Tarjan(i);
                break;
            }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值