[note]最近公共祖先
最近公共祖先(LCA)https://www.luogu.org/problemnew/show/P3379
#define RG register
#include<cstdio>
#include<iostream>
using namespace std;
const int N=500010;
int n,m,s,cnt;//n点数,m边数,s根节点
int dep[N],f[N][25],last[N];//dep深度,f[i][j]表示第i个节点的2^j祖先的序号
inline int read()
{
RG int x=0,w=1;RG char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*w;
}
struct edge{//邻接表
int to,next;
}e[2*N];
void insert(int u,int v)//连边
{
e[++cnt]=(edge){v,last[u]};last[u]=cnt;
e[++cnt]=(edge){u,last[v]};last[v]=cnt;
}
void dfs(int now)//dfs预处理出每个点的深度以及每个点的父亲
{
for(int i=last[now];i;i=e[i].next)
{
int v=e[i].to;
if(dep[v])continue;
dep[v]=dep[now]+1;
f[v][0]=now;//f[v][0]即父亲
dfs(v);
}
}
void init()
{
dep[s]=1;//根节点深度置为1
dfs(s);
for(int j=1;j<=20;j++)//注意j循环在i循环外
for(int i=1;i<=n;i++)
f[i][j]=f[f[i][j-1]][j-1];
}
int LCA(int a,int b)
{
if(dep[b]>dep[a])swap(a,b);
for(int i=20;i>=0;i--)if(f[a][i]&&dep[f[a][i]]>=dep[b])a=f[a][i];//倍增上跳,使a与b位于同一深度
if(a==b)return a;//特判b是a的祖先情况
for(int i=20;i>=0;i--)
if(f[a][i]&&f[b][i]&&f[a][i]!=f[b][i]){a=f[a][i];b=f[b][i];}//倍增上跳
return f[a][0];//注意返回值,因为只跳到了LCA的儿子处
}
int main()
{
n=read();m=read();s=read();
for(int i=1;i<n;i++)insert(read(),read());
init();
while(m--)printf("%d\n",LCA(read(),read()));
return 0;
}
[note]最近公共祖先的更多相关文章
- [最近公共祖先] POJ 1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27316 Accept ...
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- [Swift]LeetCode235. 二叉搜索树的最近公共祖先 | Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [Swift]LeetCode236. 二叉树的最近公共祖先 | Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)
A - Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &am ...
- hdu2586&&poj1330 求点间最短距&&最近公共祖先(在线&&离线处理):::可做模板
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- [Tools] Support VS Code Navigation and Autocomplete Based on Webpack Aliases with jsconfig.json
It's common to setup Webpack aliases to make imports much more convenient, but then you lose the abi ...
- Android Gradle基础实践
1,gradle是全新的一种IDE编程环境,Android Studio集成了Gradle IDE 2,要下载gradle(比方gradle-2.10)解压.配置环境变量.比方G:\Program ...
- JAVA反射机制--怎么不受泛型束缚给数组赋值
在Java中,声明数组的时候我们经常会写泛型限制用户输入或者获取的数据.如:ArrayList<String> list = new ArrayList<>(); String ...
- NativeCode中通过JNI反射调用Java层的代码,以获取IMEI为例
简单说,就是在NativeCode中做一些正常情况下可以在Java code中做的事儿,比如获取IMEI. 这种做法会使得静态分析Java层代码的方法失效. JNIEXPORT jstring JNI ...
- PS 如何制作球面化文字效果
球面化文字效果图.... 00newopen-a 00newopen-b 01mew+channel 02ctrl+L 03ctrl+I 04new+wenzi 05R ...
- C++11之function模板和bind函数适配器
在C++98中,可以使用函数指针,调用函数,可以参考之前的一篇文章:类的成员函数指针和mem_fun适配器的用法. 简单的函数调用 对于函数: void foo(const string &a ...
- Github上的PHP开源资源汇总
依赖管理 ——用于依赖管理的包和框架 Composer/Packagist : 一个包和依赖管理器 Composer Installers: 一个多框架Composer库安装器 Pickle: 可以 ...
- Java后台代码调用Spring的@Service Bean的方式
比如:在我的project中有一个类CompassIndexOperation,以: @Service("CompassIndexOperation") @Transactiona ...
- GLSL经典新手教程汇总
权威官方文档:https://www.opengl.org/documentation/glsl/ 权威民间金典新手教程:http://blog.csdn.net/racehorse 一个具体完整的G ...
- js 正则表达式 取反
http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp 以匹配中文为例 const test_value = '李钊鸿' if (/[^\u4e00 ...