题目链接:https://www.luogu.org/problemnew/show/P3379

题意:LCA模板题。

思路:今天开始学树剖,先拿lca练练。树剖解lca,两次dfs复杂度均为O(n),每次查询为logn,因此总复杂度为:O(2*n+m*logn)。

代码:

#include<cstdio>
#include<cstring>
using namespace std; const int maxn=; struct node{
int v,next;
}edge[*maxn]; int n,m,s,cnt,size[maxn],head[maxn],depth[maxn],son[maxn],fa[maxn],top[maxn]; void add(int u,int v){
edge[++cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt;
} void dfs1(int x){
size[x]=;
depth[x]=depth[fa[x]]+;
for(int i=head[x];i;i=edge[i].next){
int v=edge[i].v;
if(v==fa[x]) continue;
fa[v]=x;
dfs1(v);
size[x]+=size[v];
if(!son[x]||size[son[x]]<size[v])
son[x]=v;
}
} void dfs2(int x,int f){
top[x]=f;
if(son[x]) dfs2(son[x],f);
for(int i=head[x];i;i=edge[i].next){
int v=edge[i].v;
if(v==fa[x]||v==son[x]) continue;
dfs2(v,v);
}
} void lca(){
for(int i=;i<m;++i){
int x,y;
scanf("%d%d",&x,&y);
while(top[x]!=top[y]){
if(depth[top[x]]>depth[top[y]]) x=fa[top[x]];
else y=fa[top[y]];
}
printf("%d\n",depth[x]<depth[y]?x:y);
}
} int main(){
scanf("%d%d%d",&n,&m,&s);
for(int i=;i<n;++i){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs1(s);
dfs2(s,s);
lca();
return ;
}

LCA模板(数剖实现)的更多相关文章

  1. LCA模板

    /*********--LCA模板--***************/ //设置好静态参数并构建好图的邻接表,然后调用lca_setquery()设置查询 //最后调用lca_start(),在lca ...

  2. 倍增求lca模板

    倍增求lca模板 https://www.luogu.org/problem/show?pid=3379 #include<cstdio> #include<iostream> ...

  3. HDU 2586——How far away ?——————【LCA模板题】

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

  4. 算法复习——LCA模板(POJ1330)

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

  5. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

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

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

    LCA 有几种经典的求取方法.这里只给出模板,至于原理我完全不懂. 1.RMQ转LCA.复杂度O(n+nlog2n+m) 大致就是 DFS求出欧拉序 => 对欧拉序做ST表 => LCA( ...

  7. 【树剖求LCA】树剖知识点

    不太优美但是有注释的版本: #include<cstdio> #include<iostream> using namespace std; struct edge{ int ...

  8. Codeforces 832D: Misha, Grisha and Underground 【LCA模板】

    题目链接 模板copy from http://codeforces.com/contest/832/submission/28835143 题意,给出一棵有n个结点的树,再给出其中的三个结点 s,t ...

  9. POJ 1330 Nearest Common Ancestors(LCA模板)

    给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...

随机推荐

  1. Dapper源码学习

    序言 资料 https://github.com/StackExchange/Dapper

  2. CDOJ 1132 酱神赏花 dp+单调栈降低复杂度+滚动数组

    酱神赏花 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 262143/262143KB (Java/Others) Submit St ...

  3. FTP服务器安装配置

    1.安装:yum install vsftpd -y 2.修改配置文件:cd /etc/vsftpd/ cat vsftpd.conf | grep -Ev '^$|^#' listen_port= ...

  4. vue中全局组件与局部组件的注册,以及动态绑定props值

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 【译】XMLHttpRequest和Fetch, 谁最适合AJAX?

    原文地址:https://www.sitepoint.com/xmlhttprequest-vs-the-fetch-api-whats-best-for-ajax-in-2019/ 目录 从AJAX ...

  6. 筛选前十按a-z顺序排

    需求: 从arr数组中筛选出num最多的前十个,若是最后几个num的值一样,则最后几个按字母a-z的顺序排序,最后取出num最大的前十个: var arr = [{"id":&qu ...

  7. JAVA开发框架

    1.Bootstrap     Bootstrap是美国Twitter公司的设计师Mark Otto和Jacob Thornton合作基于HTML.CSS.JavaScript 开发的简洁.直观.强悍 ...

  8. 【python / mxnet / gluoncv / jupyter notebook】变换场景的同一行人多重识别

    程序环境为高性能集群:CPU:Intel Xeon Gold 6140 Processor * 2(共36核心)内存:512GB RAMGPU:Tesla P100-PCIE-16GB * 2   数 ...

  9. linux mmap 内存映射

    mmap() vs read()/write()/lseek() 通过strace统计系统调用的时候,经常可以看到mmap()与mmap2().系统调用mmap()可以将某文件映射至内存(进程空间), ...

  10. 微信小程序之阻止冒泡事件

    众所周知,在微信小程序给标签绑定点击方法大家都会想到 "bindtap" 但是在页面中会遇到 点击 会冒泡而触发其他元素的时间发生 那么怎么办呢 就把引发冒泡事件的始作俑者的 bi ...