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

题目链接:

https://www.luogu.org/problemnew/show/P3379

题意:

给定一颗树,还有一定数量的询问,对于每个询问,输出它的LCA.

分析:

裸的LCA,这里先给出tarjan的写法,实际上这里用tarjan是不太好的,一开始没加入读入优化,有些数据过大直接TLE了,加了之后才AC,倍增和ST表之后再补

关于tarjan:

tarjan的实现是这样的:首先沿着根节点dfs访问与它相邻的节点,并标记这个点,这样当访问完某个子树的某个节点之后,用数组标记它的父节点,当还没有向上回溯的时候,开始遍历查询(读入的时候把询问存起来,正所谓离线),查询与当前子树的根节点即当前节点(假设当前节点为u)有关的询问(u,v),如果与当前节点(即u)查询有关的查询的另一个节点(即v)已经在之前dfs过程中被访问过了,那么说明这个查询的lca就是节点v所在子树的根节点,因为你是dfs到v,然后dfs到u的,在你从v到u的这个过程中是肯定经过lca的;注意,虽然说这里是一个子树,但他并没有形成集合上的树(就是假如用f[i]存这颗树,但这个形成树的过程就是dfs到叶子节点,然后开始向上回溯的时候开始建立联系开始的,你一开始从根节点向下遍历的时候,实际上是不存在的,仅仅是一个图而不是树,建议模拟一下,画个图,把f[i]的具体变化看一下),只是存在边的联系,在访问完整个图后,才有了树;建立画个图照着代码模拟一下;

代码:
#include<iostream>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

const double inf=0x7f7f7f7f;
const int maxn=5e5+50;
const int N=5e3+50;
typedef long long ll;
typedef struct{
    int u,v,next,lca;
}Edge;
//e用来存边,query存查询
Edge e[2*maxn],query[2*maxn];

int cnt,head[maxn],head1[maxn],cnt1;

void add(int u,int v){
    e[cnt].u=u;
    e[cnt].v=v;
    /*e[cnt].w=w;
    e[cnt].f=f;*/
    e[cnt].next=head[u];
    head[u]=cnt++;
    e[cnt].u=v;
    e[cnt].v=u;
/*	e[cnt].w=0;
    e[cnt].f=-f;*/
    e[cnt].next=head[v];
    head[v]=cnt++;
}
//用来存query即存查询
void addp(int u,int v){
    query[cnt1].u=u;
    query[cnt1].v=v;
    /*e[cnt].w=w;
    e[cnt].f=f;*/
    query[cnt1].next=head1[u];
    head1[u]=cnt1++;
    query[cnt1].u=v;
    query[cnt1].v=u;
/*  e[cnt].w=0;
    e[cnt].f=-f;*/
    query[cnt1].next=head1[v];
    head1[v]=cnt1++;
}
int read()
{
    int x = 0;
    int f = 1;
    char c = getchar();
    while (c<'0' || c>'9')
    {    
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0'&&c <= '9')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x*f;
}

int n,m,s,f[maxn],vis[maxn];

int find(int x){
    return x==f[x]?x:f[x]=find(f[x]);
}


void tarjan(int x){
    vis[x]=1;
    f[x]=x;
    for(int i=head[x];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(!vis[v]){
            tarjan(v);
            f[v]=x;
        }
    }
    for(int i=head1[x];i!=-1;i=query[i].next){
        int v=query[i].v;
        if(vis[v]){
            query[i].lca=find(v);
            query[i^1].lca=f[v];
        }
    }
}
int main() {
   // std::ios::sync_with_stdio(false);
    n=read(),m=read(),s=read();
    int u,v;
    memset(head,-1,sizeof(head));
    memset(head1,-1,sizeof(head1));
    for(int i=0;i<n-1;i++){
        u=read(),v=read();
        add(u,v);
    }
    for(int i=0;i<m;i++){
        u=read(),v=read();
        addp(u,v);
    } 
    tarjan(s);
    for(int i=0;i<cnt1;i+=2){
        cout<<query[i].lca<<endl;
    }
    return 0;
}

(仅供个人理解)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值