LCA(Lowest Common Ancesor)
LCA(Lowest Common Ancesor)
1.基于二分搜索算法
预处理father[v][k]表示v的2的k次方层祖先,时间复杂度是O(nlogn),每次查询的时间复杂度是O(logn),预处理2k表的技巧在LCA之外也会用到。用链式前向星存图,相对vector邻接表要快。
一次dfs预处理出全部点的父亲结点,然后用2分思想,处理出每个点的2的k次方的父亲结点,对于LCA核心算法,首先把深度较深的移动到与另外一个水平,然后两个结点一起移动,但他们的父亲结点不同时,先上移动,最后返回当前结点的父亲结点。
1.1 Nearest Common Ancestors POJ - 1330
在有根树下,求任意两个结点的LCA,首先找到根节点,以这个结点为起点dfs,预处理出所有结点的父节点。
链式前向星存
#include<iostream>
#include<cstring>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
const int N = 1e4 + 5;
int fa[N][15];
int head[N];
int vis[N];
int cur;
int depth[N];
bool Du[N];
int ans[N];
int n;
struct Edge {
int to;
int nex;
}edge[N];
void AddEdge(int u, int v) {
edge[cur].to = v;
edge[cur].nex = head[u];
head[u] = cur++;
}
void init() {
mem(head, -1);
mem(fa, 0);
mem(Du, 0);
mem(depth, 0);
cur = 0;
}
void dfs(int v, int p, int d) {
fa[v][0] = p;
depth[v] = d;
for (int i = head[v]; i != -1; i = edge[i].nex) {
dfs(edge[i].to, v, d + 1);
}
}
int LCA(int s, int t) {
if (depth[s] < depth[t])
swap(s, t);
int temp = depth[s] - depth[t];
for (int i = 0; (1 << i) <= temp; i++)
{
if ((1<<i)&temp)
s = fa[s][i];
}
if (s == t)return s;
for (int i = (int)log2(n*1.0); i >= 0; i--) {
if (fa[s][i] != fa[t][i]) {
s = fa[s][i];
t = fa[t][i];
}
}
return fa[s][0];
}
int main()
{
int T, s, t, root;
cin >> T;
while (T--)
{
init();
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> s >> t;
AddEdge(s, t);
Du[t] = 1;
}
for (int i = 1; i <= n; i++){
if (Du[i] == 0){
root = i;
break;
}
}
dfs(root, -1, 0);
for (int j = 0; (1 << (j + 1)) < n; j++) {
for (int i = 1; i <= n; i++) {
if (fa[i][j] < 0)
fa[i][j + 1] = -1;
else fa[i][j + 1] = fa[fa[i][j]][j];
}
}
cin >> s >> t;
cout << LCA(s, t) << endl;
}
}
连接表存
#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
const int N = 1e4 + 5;
int father[N][15];
int depth[N];
int Du[N];
int max_log;
struct Node{
vector<int>G;
};
Node tree[N];
void dfs(int v, int p, int d) {
father[v][0] = p;
depth[v] = d;
for (int i = 0; i < tree[v].G.size(); i++) {
if (tree[v].G[i] != p)dfs(tree[v].G[i], v, d + 1);
}
}
void init() {
memset(Du, 0, sizeof(Du));
//for (int i = 0; i < 15; i++)G[i].clear();
memset(tree, 0, sizeof(tree));
memset(depth, 0, sizeof(depth));
memset(father, 0, sizeof(father));
}
int LCA(int u, int v) {
if (depth[u]>depth[v])swap(u, v);
int temp = depth[v] - depth[u];
for (int i = 0; (1 << i) <= temp; i++) {
if ((1 << i)&temp) {//如果temp是1011,1分别左移1,2,3,4位,与temp&,如果当前temp在i为1,说明可以提高i位
v= father[v][i];//depth[v]大,先将v提高与u水平
}
}
if (u == v)return u;
for (int i = max_log; i >= 0; i--) {
if (father[u][i] != father[v][i]) {
u = father[u][i];
v = father[v][i];
}
}
return father[u][0];
}
int main() {
int T, s, t, root;
cin >> T;
while (T--)
{
int n;
cin >> n;
init();
max_log = int(log2(1.0*n));
for (int i = 0; i < n - 1; i++) {
cin >> s >> t;
tree[s].G.push_back(t);
Du[t] = 1;
}
for (int i = 1; i <= n; i++) {
if (Du[i] == 0) {
root = i;
break;
}
}
dfs(root, -1, 0);
for (int j = 0; (1 << (j + 1)) < n; j++) {
for (int i = 1; i <= n; i++) {
if (father[i][j] < 0)
father[i][j + 1] = -1;
else father[i][j + 1] = father[father[i][j]][j];
}
}
cin >> s >> t;
cout << LCA(s, t) << endl;
}
return 0;
}
树上最短距离Factory HDU - 6115
暴力LCA
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define mem(a,x) memset(a,x,sizeof(a))
const int INF = 0x3f3f3f3f;
using namespace std;
const int N = 100010;
int fa[N][25];
int head[N];
int cur;
int depth[N];
int dis[N];//到根节点的距离
vector<int>vec[N];
int n,m;
struct Edge {
int to;
int cost;
int nex;
}edge[2*N];
//edge[i].to表示第i条边的终点,edge[i].nex表示与第i条边同起点的下一条边的存储位置,edge[i].cost为边权值.
//head[i]保存的是以i为起点的所有边中编号最大的那个, 而把这个当作顶点i的第一条起始边的位置
void AddEdge(int u, int v, int w) {
edge[cur].to = v;
edge[cur].cost = w;
edge[cur].nex = head[u];
head[u] = cur++;
}
void init() {
mem(head, -1);
mem(fa, 0);
mem(depth, 0);
mem(dis, 0);
cur = 0;
}
void dfs(int v, int p, int d,int cost) {
fa[v][0] = p;
depth[v] = d;
dis[v] = cost;
for (int i = head[v]; i != -1; i = edge[i].nex) {
if(!depth[edge[i].to])//无向图
dfs(edge[i].to, v, d + 1,dis[v]+edge[i].cost);
}
}
/*
void dfs(int v, int f, int cost)
{
dis[v] = cost;
for (int i = head[v]; i != -1; i = edge[i].nex)
{
int u = edge[i].to;
if (u == f) continue;
if (!depth[u])
depth[u] = depth[v] + 1, fa[u][0] = v, dfs(u, v, dis[v] + edge[i].cost);
}
}
*/
int LCA(int s, int t) {
if (depth[s] < depth[t])
swap(s, t);
int temp = depth[s] - depth[t];
for (int i = 0; (1 << i) <= temp; i++)
{
if ((1 << i)&temp)
s = fa[s][i];
}
if (s == t)return s;
for (int i = (int)log2(n*1.0); i >= 0; i--) {
if (fa[s][i] != fa[t][i]) {
s = fa[s][i];
t = fa[t][i];
}
}
return fa[s][0];
}
int main()
{
int T, s, t,w, root;
scanf("%d", &T);
while (T--)
{
init();
scanf("%d%d", &n, &m);
for (int i = 0; i < n - 1; i++) {
scanf("%d%d%d", &s, &t, &w);
AddEdge(s, t,w);
AddEdge(t, s, w);
//Du[t] = 1;
}
//找到根节点
for (int i = 1; i <= m; i++)
{
int num, v;
scanf("%d", &num);
for (int j = 1; j <= num; j++)
{
scanf("%d", &v);
vec[i].push_back(v);
}
}
//选择1为根
dfs(1, -1,0,0);
for (int j = 0; (1 << (j + 1)) < n; j++) {
for (int i = 1; i <= n; i++) {
if (fa[i][j] < 0)
fa[i][j + 1] = -1;
else fa[i][j + 1] = fa[fa[i][j]][j];
}
}
int q;
scanf("%d",&q);
for (int i = 1; i <= q; i++)
{
int v, u, ans = INF;
scanf("%d%d", &v, &u);
for (int j = 0; j < vec[v].size(); j++)
for (int k = 0; k < vec[u].size(); k++)
ans = min(ans, dis[vec[v][j]] + dis[vec[u][k]] - dis[LCA(vec[v][j], vec[u][k])] * 2);
printf("%d\n", ans);
}
for (int i = 1; i <= m; i++) vec[i].clear();
}
return 0;
}
LCA(Lowest Common Ancesor)的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)
Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves) 深度优先搜索的解题详细介绍,点击 ...
- LeetCode 236. 二叉树的最近公共祖先(Lowest Common Ancestor of a Binary Tree)
题目描述 给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义: “对于有根树T的两个结点u.v,最近公共祖先表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. ...
- LCA(最近公共祖先)算法
参考博客:https://blog.csdn.net/my_sunshine26/article/details/72717112 首先看一下定义,来自于百度百科 LCA(Lowest Common ...
- Tarjan算法离线 求 LCA(最近公共祖先)
本文是网络资料整理或部分转载或部分原创,参考文章如下: https://www.cnblogs.com/JVxie/p/4854719.html http://blog.csdn.net/ywcpig ...
- LCA(最近公共祖先)--tarjan离线算法 hdu 2586
HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- leetcode 236. 二叉树的最近公共祖先LCA(后序遍历,回溯)
LCA(Least Common Ancestors),即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. 题目描述 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百 ...
- PAT A1143 Lowest Common Ancestor (30 分)——二叉搜索树,lca
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- LeetCode 235. 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 ...
- Lowest Common Ancestor of a Binary Search Tree(Java 递归与非递归)
题目描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...
随机推荐
- Redis-Sentinel Redis的哨兵模式
Redis-Sentinel Redis的哨兵模式Redis Sentinel 模式简介Redis-Sentinel是官方推荐的高可用解决方案,当redis在做master-slave的高可用方案时, ...
- 如何将自己写的verilog模块封装成IP核
如何将自己写的verilog模块封装成IP核 (2014-11-21 14:53:29) 转载▼ 标签: 财经 分类: 我的东东 =======================第一篇========= ...
- Gitee vs插件(Gitee Extension for Visual Studio)
Gitee 码云(gitee.com)是开源中国推出的代码托管平台,支持 Git 和 SVN,提供免费的私有仓库托管. https://gitee.com/GitGroup/Gitee.VisualS ...
- EntityFramework安装失败
PM> Install-Package EntityFramework正在尝试收集与目标为“.NETFramework,Version=v4.0”的项目“ConsoleApplication1” ...
- 数据仓库:Mysql大量数据快速导出
背景 写这篇文章主要是介绍一下我做数据仓库ETL同步的过程中遇到的一些有意思的内容和提升程序运行效率的过程. 关系型数据库: 项目初期:游戏的运营数据比较轻量,相关的运营数据是通过Java后台程序聚合 ...
- C++实现景区信息管理系统
景区信息管理系统 实现了: 1.1 建立主程序应用菜单选项 主程序应用菜单选项包含所实现的所有功能,并且对选项采用数字标识进行选择,对其他错误输入可以进行判别,提示输入错误. 1.2 导游线路图的创建 ...
- 外网訪问XAMPP失败 解决方式 XAMPP1.8.3
xampp 訪问出现New XAMPP security concept New XAMPP security concept: Access to the requested directory i ...
- 【数据库】——SQLite使用drop column删除表字段
由于项目需求变更,我需要在sqlite的表中删除一个字段,通用的sql操作语句如下: alter table task drop column custom_fields; 结果数据库提示如下错误: ...
- python -- Pythonic
所谓Pythonic,就是极具Python特色的Python代码(明显区别于其它语言的写法的代码) 总结如下: 两变量的内容交换 Python:a,b = b,a 非Python:t=a;a=b;b= ...
- Mysql 5.7 安装 解压缩版 忘记密码
[参考]Mysql 命令大全 [参考]MySQL 5.7.19 忘记密码 重置密码 配置文件my.ini示例 服务启动后停止 log配置 [参考]MySQL5.7.19 服务挂掉 自动关闭 mysql ...