[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.8.1 logstash基础语法学习
数据类型 bool:debug => true string:host => "hostname" int:port => 514 array:match =&g ...
- ecplise内存配置
-server -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256m -XX:+UseG1GC
- java中native方法的使用
在非常多情况下,java须要调用其它语言的代码,比方c的代码.那么这个时候java中native方法就发挥作用了.以下就介绍native方法的使用. 一.JNI使用流程 a.编写带有native声明的 ...
- pomodoro源码
有网友问我要pomodoro源码.事实上这个程序非常easy,仅仅是定时器,定时弹出置顶窗体.用c++builder6.0写.放一个TPopupMenu 右键菜单,一个TTrayIcon 一个托盘图标 ...
- 分组password算法
代换,S盒.扩散和混淆,这些概念构成了分组password学的基础. 假设明文和密文的分组长度都为n比特,则明文的每个分组都有2n个可能的取值; 代换: 为使加密运算可逆(即解密运算可行),明文的每个 ...
- Android下Fragment使用(全集)
1 http://blog.csdn.net/niu_gao/article/details/7163263 思路清晰,讲解详细,代码偏少,推荐高手阅读 2 http://blog.csdn.net ...
- GridControl 二次封装,自定义颜色样式风格
1.自定义颜色格式,分组,筛选 1.封装类 必须引用类库 using System; using System.Collections.Generic; using System.Linq; usin ...
- css 控制文本显示两行 超过省略号显示
display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; word-break: break-all; ov ...
- 从头认识Spring-2.4 基于java的标准注解装配-@Inject-限定器@Named
这一章节我们来讨论一下基于java的标准注解装配标签@Inject的限定器@Named. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_s ...
- PHP 依据IP地址获取所在城市
有这种需求,须要依据用户的IP地址,定位用户所在的城市. 本文记录性文章,无逻辑性.有这样需求的朋友.能够直接拷贝使用.直接上代码,不需赘述. <? php header('Content-Ty ...