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. Kafka笔记2(安装)

    1.安装java 2.安装zookeeper 3.安装kafka Broker 测试:发布消息 测试:读取消息 4,broker配置 常规配置: broker.id: 默认0  每个broker都需要 ...

  2. POJ 1986 Distance Queries(LCA Tarjan法)

    Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...

  3. 前端学习历程--css①

    ---恢复内容开始--- 本文用自己的理解,总结网上或者自身经历的问题,加以汇总,方便查找: 一.浏览器默认样式 1.浏览器处理css&html a.css作用范围:盒子模式.浮动.定位.背景 ...

  4. mysql优化方案之sql优化

    优化目标 1.减少 IO 次数 IO永远是数据库最容易瓶颈的地方,这是由数据库的职责所决定的,大部分数据库操作中超过90%的时间都是 IO 操作所占用的,减少 IO 次数是 SQL 优化中需要第一优先 ...

  5. C#串口通信遇到的坑

    C#串口通信中有一个DataReceived事件可以委托一个接收函数.此接收函数是运行在辅线程(secondary thread)上的.如果要在这个函数中修改主线程中的一些元素,比如UI界面上的变量的 ...

  6. 18 os/os.path模块中关于文件/目录常用的函数使用方法 (转)

    os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...

  7. LeetCode #001# Two Sum(js描述)

    索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...

  8. php 使用str_replace替换关键词(兼容字符串,一维数组,多维数组)

    通过递归的方式来实现替换字符串. /* * * 使用str_replace替换关键词(兼容字符串,一维数组,多维数组) * $search 需要查找的内容 * $replace 需要替换的内容 * $ ...

  9. TCP之种种连接异常

    1. connect出错: (1) 若TCP客户端没有收到syn分节的响应,则返回ETIMEOUT错误:调用connect函数时,内核发送一个syn,若无响应则等待6s后再发送一个,若仍然无响应则等待 ...

  10. shell实现自动部署两台tomcat项目Ⅱ

    本次分为3个脚本, scp.sh放进第一台机器(负责传输文件), schenglee.sh放进第一台机器(自动部署), schenglee2.sh放进第二台机器(自动部署) 环境 tomcat1: 1 ...