洛谷3379

 #include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=,inf=1e9;
int n,m,x,y,root,tot,dep[maxn],son[maxn],size[maxn],fa[maxn],top[maxn],last[maxn];
struct edge{int to,pre;}e[maxn<<];
inline void read(int &k){
k=; int f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
k*=f;
}
void add(int x,int y){e[++tot].to=y; e[tot].pre=last[x]; last[x]=tot;}
void dfs1(int x){
size[x]=; dep[x]=dep[fa[x]]+;
for (int i=last[x],to;i;i=e[i].pre)
if ((to=e[i].to)!=fa[x]){
fa[to]=x; dfs1(to);
size[x]+=size[to];
if (size[to]>size[son[x]]) son[x]=to;
}
}
void dfs2(int x,int tp){
top[x]=tp;
if (son[x]) dfs2(son[x],tp);
for (int i=last[x],to;i;i=e[i].pre)
if ((to=e[i].to)!=fa[x]&&to!=son[x]) dfs2(to,to);
}
int lca(int x,int y){
int f1=top[x],f2=top[y];
while(f1!=f2){
if (dep[f1]<dep[f2]) swap(x,y),swap(f1,f2);
x=fa[f1]; f1=top[x];
}
return dep[x]<dep[y]?x:y;
}
int main(){
read(n); read(m); read(root);
for (int i=;i<n;i++) read(x),read(y),add(x,y),add(y,x);
dfs1(root); dfs2(root,root);
for (int i=;i<=m;i++) read(x),read(y),printf("%d\n",lca(x,y));
return ;
}

【模板】树链剖分求LCA的更多相关文章

  1. 树链剖分求LCA

    树链剖分中各种数组的作用: siz[]数组,用来保存以x为根的子树节点个数 top[]数组,用来保存当前节点的所在链的顶端节点 son[]数组,用来保存重儿子 dep[]数组,用来保存当前节点的深度 ...

  2. cogs 2450. 距离 树链剖分求LCA最近公共祖先 快速求树上两点距离 详细讲解 带注释!

    2450. 距离 ★★   输入文件:distance.in   输出文件:distance.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 在一个村子里有N个房子,一 ...

  3. HDU2586 How far away ? (树链剖分求LCA)

    用树链剖分求LCA的模板: 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 const ...

  4. cogs 2109. [NOIP 2015] 运输计划 提高组Day2T3 树链剖分求LCA 二分答案 差分

    2109. [NOIP 2015] 运输计划 ★★★☆   输入文件:transport.in   输出文件:transport.out   简单对比时间限制:3 s   内存限制:256 MB [题 ...

  5. 【树链剖分】洛谷P3379 树链剖分求LCA

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  6. 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...

  7. Hdu 2586 树链剖分求LCA

    Code: #include<cstdio> #include<cstring> #include<vector> #include<algorithm> ...

  8. [luogu P3384] [模板]树链剖分

    [luogu P3384] [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点 ...

  9. Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)

    Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...

随机推荐

  1. poj2011

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17608   Accepted: 765 ...

  2. Prevent the "split brain" by configuring the majority of nodes

    ## Prevent the "split brain" by configuring the majority of nodes (total number of nodes / ...

  3. ios11--UIButton

    // // ViewController.m // 02-UIButton(在代码中使用) // #import "ViewController.h" @interface Vie ...

  4. 路一直都在——That's just life

    分享一首很喜欢的歌,有时候歌词写得就是经历,就是人生... 穿过人潮汹涌灯火栏栅 没有想过回头 一段又一段走不完的旅程 什么时候能走完 我的梦代表什么 又是什么让我们不安 That's just li ...

  5. [Swift通天遁地]七、数据与安全-(7)创建文件浏览器:以可视化的方式浏览沙箱文件

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. JavaScript--Date 日期对象

    日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 定义一个时间对象 : var Udate=new Date(); 注意:使用关键字new,Date()的首字母必须大写. 使 ...

  7. MFC学习篇(一):用OpenCV显示视频

    首先是一些基础的步骤,如建立MFC应用,添加按钮等,博主主要参考了下面这篇文章,其中的前32步都是用OpenCV显示图片和视频所必须的,即通用的.由于LZ原来有配置OpenCV的基础,所以配制还是比较 ...

  8. Educational Codeforces Round 24 题解

    A: 考你会不会除法 //By SiriusRen #include <bits/stdc++.h> using namespace std; #define int long long ...

  9. 【BZOJ4566_洛谷3181】[HAOI2016]找相同字符(SAM)

    自己yy的方法yyyyyyyy着就A了,写篇博客庆祝一下. 题目: 洛谷3181 分析: SAM(可能是)模板题(不会SAM的同学戳我:[知识总结]后缀自动机的构建). 对\(s1\)建出SAM,用\ ...

  10. 为什么现在改用int.TryParse了

    以前一直用 int.Parse(x)或者 Convert.ToInt64(x),后来项目中发现如果x变量的值为null是,就报错了,哪怕我这样写 int.Parse(x=x??"0" ...