LCA【模板】
#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【模板】的更多相关文章
- LCA模板
/*********--LCA模板--***************/ //设置好静态参数并构建好图的邻接表,然后调用lca_setquery()设置查询 //最后调用lca_start(),在lca ...
- 倍增求lca模板
倍增求lca模板 https://www.luogu.org/problem/show?pid=3379 #include<cstdio> #include<iostream> ...
- HDU 2586——How far away ?——————【LCA模板题】
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 算法复习——LCA模板(POJ1330)
题目: Description A rooted tree is a well-known data structure in computer science and engineering. An ...
- 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 ...
- LCA模板(数剖实现)
题目链接:https://www.luogu.org/problemnew/show/P3379 题意:LCA模板题. 思路:今天开始学树剖,先拿lca练练.树剖解lca,两次dfs复杂度均为O(n) ...
- POJ 1330 Nearest Common Ancestors(LCA模板)
给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...
- HDU2586 How far away ?(LCA模板题)
题目链接:传送门 题意: 给定一棵树,求两个点之间的距离. 分析: LCA 的模板题目 ans = dis[u]+dis[v] - 2*dis[lca(u,v)]; 在线算法:详细解说 传送门 代码例 ...
- 最近公共祖先(LCA)模板
以下转自:https://www.cnblogs.com/JVxie/p/4854719.html 首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖 ...
- HDU 2586 How far away ?(LCA模板 近期公共祖先啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...
随机推荐
- js设计模式-门面模式
适用场景:门面模式在DOM脚本编程这种需要对各种不一致的浏览器接口的环境中很常用. 例子:阻止模式事件 var DED = widow.DED || {}; DED.util = { stopProp ...
- 【docker】python: can't open file 'helloworld.py': [Errno 13] Permission denied
运行容器提示权限问题 docker run -v $PWD/myapp:/usr/src/myapp -w /usr/src/myapp python:3.5 python helloworld. ...
- BZOJ 2794 DP
思路: 考虑把询问离线 按照m排序 物品按照a排序 f[i]表示c[j]的和到i b的最大值 背包就好 O(nk)竟然能过-- //By SiriusRen #include <cstdio&g ...
- 取消页面按钮的enter按下事件
<script src="../../@Javascript/jquery-1.8.1.js"></script> <script lan ...
- zookeeper单机和奇数集群
zookeeper单机和奇数集群 链接地址:https://www.cnblogs.com/lsdb/p/7297731.html
- Walking on the path of Redis --- Redis configuration
废话开篇 Redis的安装是非常简单易操作的,但是配置就有点复杂了,要想得到高性能的Redis数据服务,深入了解下如何配置是很重要的. 配置详解 下面是主要的参数及说明,至于如何配置才能最优,目前还不 ...
- python安装Django需要环境
Django==1.10.3 -e git+https://github.com/duoshuo/duoshuo-python-sdk.git#egg=duoshuo-python-sdk djang ...
- 八种Docker容器开发模式解析
原文链接:http://www.csdn.net/article/2014-10-27/2822294 Docker优点已经说过很多次,这里不做详述,Docker现在越来越受到开发人员的青睐,而且利用 ...
- 配置OpenCV的Qt开发环境
QT&openCV系列!链接:http://www.cnblogs.com/emouse/category/449213.html 本文链接:http://blog.csdn.net/qiur ...
- Rx (Reactive Extensions)介绍
Reactive Extensions (Rx) 原来是由微软提出的一个综合了异步和基于事件驱动编程的库包,使用可观察序列和LINQ-style查询操作. 使用Rx, 开发者可以用Observable ...