定
义
:
定义:
定义:
最
近
公
共
祖
先
简
称
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)。两个节点的最近公共祖先,
最近公共祖先简称LCA(LowestCommonAncestor)。两个节点的最近公共祖先,
就
是
这
两
个
点
的
公
共
祖
先
里
面
,
离
根
最
远
的
那
个
。
就是这两个点的公共祖先里面,离根最远的那个。
就是这两个点的公共祖先里面,离根最远的那个。
.
.
.
...
...
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;
}
#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;
}
}
}