最近公共祖先(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]最近公共祖先的更多相关文章

  1. [最近公共祖先] POJ 1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27316   Accept ...

  2. 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18136   Accept ...

  3. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  4. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  5. [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 ...

  6. [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 ...

  7. POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)

    A - Nearest Common Ancestors Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld &am ...

  8. hdu2586&&poj1330 求点间最短距&&最近公共祖先(在线&&离线处理):::可做模板

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. [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 ...

随机推荐

  1. SSO单点登录系列4:cas-server登录页面自定义修改过程(jsp页面修改)

    落雨 cas 单点登录 SSO单点登录系列4:cas-server登录页面自定义修改过程,全新DIY. 目标:    下面是正文: 打开cas的默认首页,映入眼帘的是满眼的中文and英文混杂体,作为一 ...

  2. SpringMVC 文本文件下载的配置

    页面: <fieldset> <legend>Download annotator list</legend> <img src="pages/to ...

  3. TFS 设置(转)

    一 参考以下两个链接进行相关软件的安装和用户权限配置: http://www.cnblogs.com/WilsonWu/archive/2011/11/24/2261674.html http://w ...

  4. 仿IOS中下拉刷新的“雨滴”效果

    在IOS中,有非常赞的"水滴"下拉效果.非常久之前也想在Android上实现,可是苦于能力有限,一直未能付诸行动.这几天趁着空隙时间.写了一版初步实现,基本达到了"水滴& ...

  5. mysql用merge合并表

    merge合并表的要求 1.合并的表使用的必须是MyISAM引擎 2.表的结构必须一致,包括索引.字段类型.引擎和字符集 实例: create table if not exists user1( i ...

  6. Android网络框架Volley

    Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...

  7. hdu 2349 最小生成树

    /* 刚開始想错了,我以为必须是相邻的点才干连接.原来无线距离能够随意连接 对最小生成树理解不够深啊 */ #include<stdio.h> #include<math.h> ...

  8. ArrayList add remove 代码分析

    Add 首次add 元素需要对数组进行扩容(初始化Capacity 10, 第二次扩容10>>1 为5, 第三次扩容15>>1 为7), 每次扩容之前长度的1.5倍,当add ...

  9. CentOS: Make Command not Found and linux xinetd 服务不能启动

    在centos 安装编译器 yum -y install gcc automake autoconf libtool make linux xinetd 服务不能启动: [root@capaa xin ...

  10. C语言include预处理命令与多文件编译

    #include预处理命令几乎使我们在第一次接触C的时候就会碰到的预处理命令,可我现在还不怎么清楚,这次争取一次搞懂. 一.#include预处理指令的基本使用 预处理指令可以将别处的源代码内容插入到 ...