#include <algorithm>
#include <cstdio>
#include <vector> #define N 10015 using namespace std; vector<int>vec[N];
int n,a,b,m,deep[N],dad[N][]; int LCA(int x,int y)
{
if(deep[x]>deep[y]) swap(x,y);
for(int i=;i>=;i--)
if(deep[dad[y][i]]>=deep[x]) y=dad[y][i];
if(x==y) return x;
for(int i=;i>=;i--)
if(dad[x][i]!=dad[y][i])
x=dad[x][i],y=dad[y][i];
return dad[x][];
} void DFS(int x)
{
deep[x]=deep[dad[x][]]+;
for(int i=;dad[x][i];i++)
dad[x][i+]=dad[dad[x][i]][i];
for(int i=;i<vec[x].size();i++)
if(!deep[vec[x][i]]) dad[vec[x][i]][]=x,DFS(vec[x][i]);
} int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vec[b].push_back(a);
}
DFS();
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&a,&b);
printf("%d\n",LCA(a,b));
}
return ;
}
/*
6
1 2
1 3
4 2
5 2
3 6
5
4 5
2 6
3 6
1 5
4 6
*/

倍增

 #include <algorithm>
#include <cstdio>
#include <vector> using namespace std; const int N();
vector<int>vec[N];
int n,q,x,y;
int size[N],deep[N],top[N],dad[N]; void DFS(int x)
{
size[x]=; deep[x]=deep[dad[x]]+;
for(int i=;i<vec[x].size();i++)
if(vec[x][i]!=dad[x])
{
dad[vec[x][i]]=x;
DFS(vec[x][i]);
size[x]+=size[vec[x][i]];
}
} void DFS_(int x)
{
int t=; if(!top[x]) top[x]=x;
for(int i=;i<vec[x].size();i++)
if(vec[x][i]!=dad[x]&&size[t]<size[vec[x][i]]) t=vec[x][i];
if(t) top[t]=top[x], DFS_(t);
for(int i=;i<vec[x].size();i++)
if(vec[x][i]!=dad[x]&&t!=vec[x][i]) DFS_(vec[x][i]);
} int LCA(int x,int y)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
x=dad[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
return x;
} int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);
vec[x].push_back(y);
vec[y].push_back(x);
}
DFS(); DFS_();
scanf("%d",&q);
for(;q;q--)
{
scanf("%d%d",&x,&y);
printf("%d\n",LCA(x,y));
}
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模板(数剖实现)

    题目链接:https://www.luogu.org/problemnew/show/P3379 题意:LCA模板题. 思路:今天开始学树剖,先拿lca练练.树剖解lca,两次dfs复杂度均为O(n) ...

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

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

  8. HDU2586 How far away ?(LCA模板题)

    题目链接:传送门 题意: 给定一棵树,求两个点之间的距离. 分析: LCA 的模板题目 ans = dis[u]+dis[v] - 2*dis[lca(u,v)]; 在线算法:详细解说 传送门 代码例 ...

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

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

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

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

随机推荐

  1. 关于View的ScrollTo, getScrollX 和 getScrollY

    下载地址:源代码 当利用 Scroller 去滑动屏幕或者扩展 ScrollView 的时候,总是会用到 getScrollX 和 getScrollY 去获取当前View 滑动到的位置,那么getS ...

  2. bzoj2958: 序列染色(DP)

    2958: 序列染色 题目:传送门 题解: 大难题啊(还是我太菜了) %一发大佬QTT 代码: #include<cstdio> #include<cstring> #incl ...

  3. ES Segment Memory——本质上就是segment中加到内存的FST数据,因此segment越多,该内存越大

    ElasticSearch优化系列四:ES的heap是如何被瓜分掉的 转自:https://www.jianshu.com/p/f41b706db6c7 以下分别解读几个我知道的内存消耗大户: Seg ...

  4. [AHOI 2008] 聚会

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1832 [算法] 最近公共祖先 [代码] #include<bits/stdc+ ...

  5. 使用spring mvc或者resteasy构建restful服务

    看到最近一个项目里用resteasy来构建restful接口,有点不明白,不少Spring mvc4.0以后也可以很方面的实现restful服务吗,为啥还要在Spring MVC的项目里还引入rest ...

  6. 1-1restful简介及资源介绍

  7. 计算机网络自顶向下方法第2章-应用层(application-layer).2

    2.4 DNS:因特网的目录服务 2.4.1 DNS提供的服务 DNS的定义 实体层面看,DNS是一个由分层的DNS服务器实现的分布式数据库 协议层面看,DNS是一个使得主机能够查询分布式数据库的应用 ...

  8. ueditor和flexpaper的学习。。。。

    博客园的博主编辑文本的时候在博客园编辑器上编辑的,最近见到并学习了一点百度一款ueditor的编辑器可供程序猿们二次开发.... 见链接http://fex.baidu.com/ueditor/#st ...

  9. nfs server and client

    系统环境:Centos7.3 ip1:10.0.0.1 ip2:10.0.0.2 检测是否安装NFS服务 rpm -qa | grep   nfs rpm  -qa|grep    nfs 安装NFS ...

  10. It Started With A Kiss