\(\color{#0066ff}{ 题目描述 }\)

如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先。

\(\color{#0066ff}{输入格式}\)

第一行包含三个正整数N、M、S,分别表示树的结点个数、询问的个数和树根结点的序号。

接下来N-1行每行包含两个正整数x、y,表示x结点和y结点之间有一条直接连接的边(数据保证可以构成树)。

接下来M行每行包含两个正整数a、b,表示询问a结点和b结点的最近公共祖先。

\(\color{#0066ff}{输出格式}\)

输出包含M行,每行包含一个正整数,依次为每一个询问的结果。

\(\color{#0066ff}{输入样例}\)

5 5 4
3 1
2 4
5 1
1 4
2 4
3 2
3 5
1 2
4 5

\(\color{#0066ff}{输出样例}\)

4
4
1
4
4

\(\color{#0066ff}{数据范围与提示}\)

时空限制:1000ms,128M

数据规模:

对于30%的数据:N<=10,M<=10

对于70%的数据:N<=10000,M<=10000

对于100%的数据:N<=500000,M<=500000

\(\color{#0066ff}{ 题解 }\)

拿LCT只为了求个LCA

有毒啊

access的时候记录一下上一个点(下一个链尾)

access第二个点的时候,最后一次的那个链的接触点就是LCA

// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL in() {
char ch; int x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 5e5 + 5;
struct LCT {
protected:
struct node {
node *ch[2], *fa;
int rev;
node(int rev = 0): rev(rev) { ch[0] = ch[1] = fa = NULL; }
bool ntr() { return fa && (fa->ch[1] == this || fa->ch[0] == this); }
bool isr() { return fa->ch[1] == this; }
void trn() { std::swap(ch[0], ch[1]); rev ^= 1; }
void dwn() { if(rev) { if(ch[0]) ch[0]->trn(); if(ch[1]) ch[1]->trn(); rev = 0; } }
}s[maxn], *t[maxn], *lst;
int top;
void rot(node *x) {
node *y = x->fa, *z = y->fa; int k = x->isr(); node *w = x->ch[!k];
if(y->ntr()) z->ch[y->isr()] = x;
x->ch[!k] = y, y->ch[k] = w;
y->fa = x, x->fa = z;
if(w) w->fa = y;
}
void splay(node *o) {
t[top = 1] = o;
while(t[top]->ntr()) t[top + 1] = t[top]->fa, top++;
while(top) t[top--]->dwn();
while(o->ntr()) { if(o->fa->ntr()) rot(o->isr() ^ o->fa->isr()? o : o->fa); rot(o); }
}
void access(node *x) { for(node *y = NULL; x; x = (y = x)->fa) splay(x), x->ch[1] = y, lst = x; }
void makeroot(node *x) { access(x), splay(x), x->trn(); }
node *findroot(node *x) { access(x), splay(x); while(x->dwn(), x->ch[0]) x = x->ch[0]; return splay(x), x; }
node *getLCA(node *x, node *y) { return access(x), access(y), lst; }
void link(node *x, node *y) { makeroot(x), x->fa = y; }
public:
void link(int x, int y) { link(s + x, s + y); }
int LCA(int x, int y) { return getLCA(s + x, s + y) - s; }
void makeroot(int x) { makeroot(s + x); }
}v;
int main() {
int n = in(), m = in(), s = in();
for(int i = 1; i < n; i++) v.link(in(), in());
v.makeroot(s);
while(m --> 0) printf("%d\n", v.LCA(in(), in()));
return 0;
}

P3379 【模板】最近公共祖先(LCA)(LCT)的更多相关文章

  1. [模板] 最近公共祖先/lca

    简介 最近公共祖先 \(lca(a,b)\) 指的是a到根的路径和b到n的路径的深度最大的公共点. 定理. 以 \(r\) 为根的树上的路径 \((a,b) = (r,a) + (r,b) - 2 * ...

  2. Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集)

    Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集) Description sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为 ...

  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. 【lhyaaa】最近公共祖先LCA——倍增!!!

    高级的算法——倍增!!! 根据LCA的定义,我们可以知道假如有两个节点x和y,则LCA(x,y)是 x 到根的路 径与 y 到根的路径的交汇点,同时也是 x 和 y 之间所有路径中深度最小的节 点,所 ...

  6. 【洛谷 p3379】模板-最近公共祖先(图论--倍增算法求LCA)

    题目:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 解法:倍增. 1 #include<cstdio> 2 #include<cstdlib> 3 #include ...

  7. 最近公共祖先(LCA)模板

    以下转自:https://www.cnblogs.com/JVxie/p/4854719.html 首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖 ...

  8. HDU 2586 How far away ?(LCA模板 近期公共祖先啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...

  9. luogu3379 【模板】最近公共祖先(LCA) 倍增法

    题目大意:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 整体步骤:1.使两个点深度相同:2.使两个点相同. 这两个步骤都可用倍增法进行优化.定义每个节点的Elder[i]为该节点的2^k( ...

  10. 最近公共祖先lca模板

    void dfs(int x,int root){//预处理fa和dep数组 fa[x][0]=root; dep[x]=dep[root]+1; for(int i=1;(1<<i)&l ...

随机推荐

  1. 2015.1.4 判断鼠标点击DataGridView的第几行还是空白处

    public int GetRowIndexAt(int mouseLocation_Y) { if (dvaw.FirstDisplayedScrollingRowIndex < 0) { r ...

  2. windows异常演示,指定异常类型,然后生成异常

    #include "stdafx.h"#include <Windows.h>#include <float.h> DWORD Filter (LPEXCE ...

  3. CSS3图片以中心缩放,放大超出隐藏实现

    首页,重点是有一个包裹img标签的容器,这里我们给该容器设置一个class为selfScale <div class="selfScale"> <img sr=& ...

  4. 正则表达式 notes

    nfs 网络文件系统 存储类型:块存储,对象存储,文件存储 nfs属于文件存储 crond 分钟小时日月周 命令:绝对路径 命令是由crond解释 /bin/bash a.sh */2 * * * * ...

  5. 关于EF中实体和数据表以及查询语句映射的问题

    关于EF中实体和数据表以及查询语句映射的问题? 很多人在使用的时候分不清楚 实体字段应该少于等于(数据库中的表字段或者SQL查询中的临时字段).这样在查询或者添加修改都不会出现问题 如果实体的字段大于 ...

  6. 每天一道算法题(32)——输出数组中第k小的数

    1.题目 快速输出第K小的数 2.思路 使用快速排序的思想,递归求解.若键值位置i与k相等,返回.若大于k,则在[start,i-1]中寻找第k大的数.若小于k.则在[i+1,end]中寻找第k+st ...

  7. ViewPager+Fragment基本使用方法(附源码)

    ViewPager+Fragment可以做出多页面滑动效果,让我们的应用程序界面操作起来更加灵活 对于ViewPager和Fragment组件还不熟悉的朋友,可以先看看相关的资料. 首先在activi ...

  8. KNN算法实现

    import csvimport randomimport mathimport operatorimport numpy as np def loadDataset(filename,split,t ...

  9. Python中for else注意事项

    假设有如下代码: for i in range(10): if i == 5: print 'found it! i = %s' % i else: print 'not found it ...' ...

  10. SpringJdbc 【springjdbc的使用方法】

    1 什么是springjdbc spring对jdbc的封装 2 使用SpringJdbc的编程步骤 2.1 导包 spring-jdbc : springjdbc的包 mysql : MySQL的驱 ...