LCA最近公共祖先模板代码
vector模拟邻接表:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#define eps 1e-8
#define memset(a,v) memset(a,v,sizeof(a))
using namespace std;
typedef long long int LL;
const int MAXL(1e4);
const int INF(0x7f7f7f7f);
const int mod(1e9+);
int dir[][]= {{-,},{,},{,},{,-}};
int father[MAXL+];
bool is_root[MAXL+];
bool vis[MAXL+];
vector<int>v[MAXL+];
int root;
int cx,cy;
int ans;
int Find(int x)
{
if(x!=father[x])
father[x]=Find(father[x]);
return father[x];
} void Join(int x,int y)
{
int fx=Find(x),fy=Find(y);
if(fx!=fy)
father[fy]=fx;
} void LCA(int u)
{
for(int i=; i<v[u].size(); i++)
{
int child=v[u][i];
if(!vis[child])
{
LCA(child);
Join(u,child);
vis[child]=true;
}
}
if(u==cx&&vis[cy]==true)
ans=Find(cy);
if(u==cy&&vis[cx]==true)
ans=Find(cx); } void init()
{
memset(is_root,true);
memset(vis,false);
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
v[i].clear();
for(int i=; i<=n; i++)
father[i]=i;
for(int i=; i<n; i++)
{
int x,y;
scanf("%d%d",&x,&y);
v[x].push_back(y);
is_root[y]=false;
}
scanf("%d%d",&cx,&cy);
for(int i=; i<=n; i++)
{
if(is_root[i]==true)
{
root=i;
break;
}
} }
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
LCA(root);
cout<<ans<<endl;
}
}
链式前向星写法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#define eps 1e-8
#define memset(a,v) memset(a,v,sizeof(a))
using namespace std;
typedef long long int LL;
const int MAXL(1e6);
const int INF(0x7f7f7f7f);
const int mod(1e9+);
int dir[][]= {{-,},{,},{,},{,-}};
struct node
{
int to;
int next;
}edge[MAXL+];
int head[MAXL+];
int father[MAXL+];
bool vis[MAXL+];
bool is_root[MAXL+];
int n;
int cnt;
int cx,cy;
int ans;
int root; int Find(int x)
{
if(x!=father[x])
father[x]=Find(father[x]);
return father[x];
} void Join(int x,int y)
{
int fx=Find(x),fy=Find(y);
if(fx!=fy)
father[fy]=fx;
} void add_edge(int x,int y)
{
edge[cnt].to=y;
edge[cnt].next=head[x];
head[x]=cnt++;
} void init()
{
cnt=;
memset(head,-);
memset(vis,false);
memset(is_root,true);
scanf("%d",&n);
for(int i=;i<=n;i++)
father[i]=i;
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add_edge(x,y);
is_root[y]=false;
}
for(int i=;i<=n;i++)
if(is_root[i]==true)
root=i;
} void LCA(int u)
{
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
LCA(v);
Join(u,v);
vis[v]=true; }
if(cx==u&&vis[cy]==true)
ans=Find(cy);
if(cy==u&&vis[cx]==true)
ans=Find(cx);
}
void solve()
{
scanf("%d%d",&cx,&cy);
LCA(root);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
solve();
cout<<ans<<endl;
}
}
转自:https://blog.csdn.net/baiyi_destroyer/article/details/81363221
LCA最近公共祖先模板代码的更多相关文章
- LCA(最近公共祖先)模板
Tarjan版本 /* gyt Live up to every day */ #pragma comment(linker,"/STACK:1024000000,1024000000&qu ...
- LCA最近公共祖先模板(求树上任意两个节点的最短距离 || 求两个点的路进(有且只有唯一的一条))
原理可以参考大神 LCA_Tarjan (离线) TarjanTarjan 算法求 LCA 的时间复杂度为 O(n+q) ,是一种离线算法,要用到并查集.(注:这里的复杂度其实应该不是 O(n+q) ...
- lca最短公共祖先模板(hdu2586)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 #include<iostream> #include<cstdio> ...
- Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)
Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...
- LCA(最近公共祖先)之倍增算法
概述 对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. 如图,3和5的最近公共祖先是1,5和2的最近公共祖先是4 在本篇中我们先介 ...
- CodeVs.1036 商务旅行 ( LCA 最近公共祖先 )
CodeVs.1036 商务旅行 ( LCA 最近公共祖先 ) 题意分析 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从 ...
- lca 最近公共祖先
http://poj.org/problem?id=1330 #include<cstdio> #include<cstring> #include<algorithm& ...
- LCA近期公共祖先
LCA近期公共祖先 该分析转之:http://kmplayer.iteye.com/blog/604518 1,并查集+dfs 对整个树进行深度优先遍历.并在遍历的过程中不断地把一些眼下可能查询到的而 ...
- LCA 近期公共祖先 小结
LCA 近期公共祖先 小结 以poj 1330为例.对LCA的3种经常使用的算法进行介绍,分别为 1. 离线tarjan 2. 基于倍增法的LCA 3. 基于RMQ的LCA 1. 离线tarjan / ...
随机推荐
- Cocos Creator Slider(进度条)的三种实现
实现原理: 方法一:动态计算,slider上增加背景图,根据滑动的进度动态计算背景图的大小:方法二:slider+progress,根据slider滑动的进度,动态改变progress的显示进度:方法 ...
- jenkins配置工程目录-启动case
1.我们在python里面编辑的脚本可以正常跑,但是在cmd里面跑就不行了,找不到自己定义的方法模块,这个时候我们要搞个环境变量 name : PYTHONPATH val : 工程目录路劲 ...
- 栈ADT
栈 栈是限制插入和删除只能在同一位置的表,这一位置称为栈顶(top),也可能称为LIFO表 对于空栈的pop(弹栈)操作是一个ADT错误,但是若是push(压栈)时空间超限并不是ADT错误 实现:基于 ...
- python_json序列化和反序列化
序列化 import json dic = {'} print(json.dumps(dic)) 反序列化;json.loads() dic = {'} print(json.dumps(dic)) ...
- 在 python3.x中安装 Crypto 库
1.安装:直接找过来 whl 安装:链接: https://pan.baidu.com/s/1zXjzchnqc1GgSWT9TjHDaA 提取码: dzbn 复制这段内容后打开百度网盘手机App,操 ...
- [转载]Javascript .then()这个方法是什么意思?
then()方法是异步执行. 意思是:就是当.then()前的方法执行完后再执行then()内部的程序,这样就避免了,数据没获取到等的问题. 语法:promise.then(onCompleted, ...
- Oarcle 入门之 order by 关键字
order by 关键字 作用:用于对查询结果进行排序 select * from emp where deptno = 20 order by sal asc /desc; 如何排序之升降问题 *用 ...
- Dom 兼容处理
获取子节点:childNodes 在IE下是可以正常使用的 但是在FF包含了文本节点需要配合nodeType做个类型判断 1是元素节点 3是文本节点 也可以采用 children IE ...
- https真的安全吗,加密登录其实不简单
登录,是做web开发的程序员做项目第一接触到的模块,看似简简单单的登录背后囊括了编程知识的方方面面. 登录安全吗?密码会不会泄露? 明文传输时代 互联网开始的时候,登录确实是使用明文校验的,甚至数据源 ...
- Shell read交互
read语句:设定客户端交互的任意输出值. 参数: -a 后跟一个变量,该变量会被认为是个数组,然后给其赋值,默认是以空格为分割符. -d 后面跟一个标志符,其实只有其后的第一个字符有用,作为结束的标 ...