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最近公共祖先模板代码的更多相关文章

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

    Tarjan版本 /* gyt Live up to every day */ #pragma comment(linker,"/STACK:1024000000,1024000000&qu ...

  2. LCA最近公共祖先模板(求树上任意两个节点的最短距离 || 求两个点的路进(有且只有唯一的一条))

    原理可以参考大神 LCA_Tarjan (离线) TarjanTarjan 算法求 LCA 的时间复杂度为 O(n+q) ,是一种离线算法,要用到并查集.(注:这里的复杂度其实应该不是 O(n+q)  ...

  3. lca最短公共祖先模板(hdu2586)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 #include<iostream> #include<cstdio> ...

  4. Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)

    Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...

  5. LCA(最近公共祖先)之倍增算法

    概述 对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. 如图,3和5的最近公共祖先是1,5和2的最近公共祖先是4 在本篇中我们先介 ...

  6. CodeVs.1036 商务旅行 ( LCA 最近公共祖先 )

    CodeVs.1036 商务旅行 ( LCA 最近公共祖先 ) 题意分析 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从 ...

  7. lca 最近公共祖先

    http://poj.org/problem?id=1330 #include<cstdio> #include<cstring> #include<algorithm& ...

  8. LCA近期公共祖先

    LCA近期公共祖先 该分析转之:http://kmplayer.iteye.com/blog/604518 1,并查集+dfs 对整个树进行深度优先遍历.并在遍历的过程中不断地把一些眼下可能查询到的而 ...

  9. LCA 近期公共祖先 小结

    LCA 近期公共祖先 小结 以poj 1330为例.对LCA的3种经常使用的算法进行介绍,分别为 1. 离线tarjan 2. 基于倍增法的LCA 3. 基于RMQ的LCA 1. 离线tarjan / ...

随机推荐

  1. await异步的,容易理解一点

    C# 5.0中引入了async 和 await.这两个关键字可以让你更方便的写出异步代码. 看个例子: public class MyClass { public MyClass() { Displa ...

  2. QCon2019全球软件开发大会广州站即将来袭

    QCon广州2019|全球软件开发大会 会议时间:2019-05-25 08:00至 2019-05-28 18:00结束 会议地点: 广州  广州万富希尔顿酒店  中国广州市白云区云城东路515-5 ...

  3. phpstudy如何安装ssl证书

    网站上面部署ssl证书的站点越来越大,但有很多集成式的web服务器无法按照一般站点的配置来部署ssl证书,现在,卓趣科技就以集成式phpstudy为例(apache+mysql),为大家展示一下正确的 ...

  4. Python记录12:迭代器+生成器+生成式

    '''1. 什么是迭代器 什么是迭代:迭代就是一个重复的过程,但是每一次重复都是基于上一次的结果而进行的 单纯的重复不是迭代: while True: print(1) 迭代的过程 l=['a','b ...

  5. nginx配置详细解析

    转自 http://blog.csdn.net/zhongguozhichuang/article/details/528168871.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将 ...

  6. python locust 性能测试:locust 参数化(list) ---循环取数据,数据可重复使用

    from locust import TaskSet, task, HttpLocust class UserBehavior(TaskSet): def on_start(self): # 当模拟用 ...

  7. 记账本微信小程序开发四

    学习添加组件 集成日期组件 添加组件 需要在main.js文件中,声明一个data值date与wxml中的{{date}}绑定关联,然后在onLoad中初始化字符串格式的日期值, 处理日期组件点击确认 ...

  8. Flutter 获取控件尺寸和位置

    1. 插件必须渲染好, final RenderBox box = globalKey.currentContext.findRenderObject(); final size = box.size ...

  9. CSS Class 选择器

    CSS Class 选择器 再<stype>标签内,通过class属性来对标签进行css样式配置 <html> <head> <!-- style 设置头部标 ...

  10. Python 类的特性讲解

    类的特性讲解 类的特性 #定义一个类, class是定义类的语法,Role是类名, (object)是新式类的写法,必须这样 写,以后再讲为什么 class Role(object): #初始化函数, ...