最近公共祖先 Lowest Common Ancestors
基于深度的LCA算法: 对于两个结点u、v,它们的深度分别为depth(u)、depth(v),对于其公共祖先w,深度为depth(w),u需要向上回溯depth(u)-depth(w)步,v需要depth(v)-depth(w)步。 因此,只需要u,v中深度较大的向上移,直到 depth(u)=depth(v) && 此时指向一个结点。
因此,求出LCA的时间复杂度为O(depth(u)+depth(v)); 求深度,只需要进行一次遍历,O(n)。
如果是完全二叉树的话,相当于深度(下标)已经建立好了,直接根据LCA进行计算。
vector<int> G[MAXV]; // 树
int root; int parent[MAXV]; // 父节点; (并查集)
int depth[MAXV]; // 节点深度 // v:当前结点 p:父节点 d:深度
void DFS(int v,int p,int d){
parent[v]=p;
depth[v]=d;
for(auto& p:G[v]){
dfs(p,v,d+);
}
} void init(){
dfs(root,-,);
} int LCA(int u,int v){
while(depth[u]>depth[v]){
u=parent[u];
}
while(depth[u]<depth[v]){
v=parent[v];
}
while(u!=v){
u=parent[u];
v=parent[v];
}
return u;
}
如果需要查找多对LCA,可以考虑二分查找的性质,来找到最小步数。
最近公共祖先 Lowest Common Ancestors的更多相关文章
- [Swift]LeetCode235. 二叉搜索树的最近公共祖先 | Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [Swift]LeetCode236. 二叉树的最近公共祖先 | Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- 最近公共祖先 · Lowest Common Ancestor
[抄题]: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. “Th ...
- 最近公共祖先(least common ancestors algorithm)
lca问题是最近公共祖先问题,一般是针对树结构的.现在有两种方法来解决这样的问题 1. On-line algorithm 用比较长的时间做预处理.然后对每次询问进行回答. 思路:对于一棵树中的两个节 ...
- 最近公共祖先 Least Common Ancestors(LCA)算法 --- 与RMQ问题的转换
[简介] LCA(T,u,v):在有根树T中,询问一个距离根最远的结点x,使得x同时为结点u.v的祖先. RMQ(A,i,j):对于线性序列A中,询问区间[i,j]上的最值.见我的博客---RMQ - ...
- 最近公共祖先 LCA (Lowest Common Ancestors)-树上倍增
树上倍增是求解关于LCA问题的两个在线算法中的一个,在线算法即不需要开始全部读入查询,你给他什么查询,他都能返回它们的LCA. 树上倍增用到一个关键的数组F[i][j],这个表示第i个结点的向上2^j ...
- 最近公共祖先 LCA 倍增算法
树上倍增求LCA LCA指的是最近公共祖先(Least Common Ancestors),如下图所示: 4和5的LCA就是2 那怎么求呢?最粗暴的方法就是先dfs一次,处理出每个点的深度 ...
- 编程算法 - 二叉树的最低公共祖先 代码(C)
二叉树的最低公共祖先 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉树的最低公共祖先(lowest common ancestor), 首先先序遍 ...
- LeetCode 235. 二叉搜索树的最近公共祖先 32
235. 二叉搜索树的最近公共祖先 235. Lowest Common Ancestor of a Binary Search Tree 题目描述 给定一个二叉搜索树,找到该树中两个指定节点的最近公 ...
随机推荐
- Android中实现照片滑动时左右进出的动画的xml代码
场景 Android中通过ImageSwitcher实现相册滑动查看照片功能(附代码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/det ...
- Visual Studio Code中C/C++的环境配置
Visual Studio Code 的功能十分强大,但是对我这种小白不是很友好,它和其它的集成开发工具不同,Visual Studio Code (以下简称VS)自身其实仅仅是一个编辑器, 是不具备 ...
- vue中报错Do not use built-in or reserved HTML elements as component id details
原因是定义了一个叫做details的comonent 跟现有的html网页中的标签重合 export default { name: 'details', data () { return { equ ...
- windows下安装openjdk
redhat版openjdk,解压后就能用,下载地址https://developers.redhat.com/products/openjdk/download. Azul Zulu版openjdk ...
- 【计算语言学实验】基于 Skip-Gram with Negative Sampling (SGNS) 的汉语词向量学习和评估
一.概述 训练语料来源:维基媒体 https://dumps.wikimedia.org/backup-index.html 汉语数据 用word2vec训练词向量,并用所学得的词向量,计算 pku_ ...
- ubuntu---CUDA版本与NVIDIA显卡驱动版本对应关系查询
https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html ,如果不是CUDA 10.2 版本的,可以类似的查找 CUDA x ...
- SQL server安装时显示重启计算机失败问题解决办法
前几天在装SQL server2016的时候,一直显示重启计算机失败; 网上的方法也找来用了,但是重启完还是同样的问题,试了很多种方法,最后在一次测试中点关机的时候不小心点成了重启(win10有个毛病 ...
- 嵊州D5T3 指令 program 神奇的位运算
指令 program [问题描述] krydom 有一个神奇的机器. 一开始,可以往机器里输入若干条指令: opt x 其中,opt 是 & | ^ 中的一种,0 ≤ x ≤ 1023 . 对 ...
- css揭秘 一
当某些值相互依赖是,应该把它们的相互关系用代码表达出来 font-size: 20px; line-height: 1.5; // 行高是字体的1.5倍 当改变某个参数时候,做到只改尽量少的地方,最好 ...
- leetcode四道组合总和问题总结(39+40+216+377)
39题目: 链接:https://leetcode-cn.com/problems/combination-sum/ 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 ...