[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 ...
随机推荐
- 2017.5.24 在intelliJ IDEA 中生成war包
1.勾选Build on make file -> project structure -> Artifacts 2.compile module "***" 选择项目 ...
- requirejs主流程解读
近期读了下requirejs源代码,那叫一个复杂啊(相对于seajs来说).整理出了逻辑的主要部分的流程图,感兴趣的能够看下.欢迎批评指正~ http://www.gliffy.com/go/publ ...
- 关于Laravel5.2在php5.3.6X和在php7.1.10下的内存溢出
php5.3.6X是编译安装,在debug模式下,频繁报出内存泄露警告 php7.1.10下则不会有此错误. 顺便提下:测试发现ThinkPHP也不会有该内存泄露警告! 希望知道如何解决该问题的童鞋能 ...
- mysql_affected_rows的注意点
取得最近一次与 link_identifier 关联的 INSERT,UPDATE 或 DELETE 查询所影响的记录行数. 1.执行成功,则返回受影响的行的数目,如果最近一次查询失败的话,函数返回 ...
- List装form
List<MemberPrivilegeForm> formlist = new ArrayList<MemberPrivilegeForm>(); int status = ...
- 【DB2】新建用户
1.创建用户(切换到root用户下操作) useradd -g users -d /home/qinys -s /bin/bash -m qinys 2.修改密码 passwd qinys 备注:此处 ...
- 【VBA】修改Excle的标题
打开Excle的时候,有时候不需要显示Miscrosoft Excle,如下图标记,想把标记内的内容替换为:管理系统 操作代码: Public Sub 修改标题() Application.Capti ...
- 我是怎样理解web页面的
事实上web页面包括三部分东东 1.页面展示的元素(HTML) 2.页面元素展示的样式(CSS) 3.控制页面元素的交互(JavaScript) 不管页面多么复杂,从这三方面去看,都会得到清晰的认识的 ...
- apktool反编译时各种问题汇总
问题1:apktool d -d 时出现错误Error occured while disassembling class办法:这不是你的错误,这是apktool本身的错误,目前正式release的1 ...
- CentOS7网络桥接模式下配置-经典完备
原文地址:http://blog.csdn.net/youzhouliu/article/details/51175364 首先要将Vmware设置为桥接模式: 并选择宿主机连接的网路进行桥接: Ce ...