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 / ...
随机推荐
- Tomcat出现The origin server did not find a current representation for the target resourc...
访问页面出现404 解决方法: https://blog.csdn.net/dbc_121/article/details/79204340 我的问题主要还是在tomcat调整上, 对了,关于loca ...
- python_WSGI接口
WSGI:Web Server Gateway Interface WSGI接口定义非常简单,它只要求Web开发者实现一个函数,就可以响应HTTP请求.我们来看一个最简单的Web版本的“Hello, ...
- web前端学习历程--排序
一.js排序方法 1.按字母顺序排列: arr.sort() 2.按数值从小到大: function sortNumber(a,b)//排序函数 { return a - b } var arr = ...
- linux下视频转gif
title: linux下视频转gif date: 2017-11-23 16:55:26 tags: linux categories: linux 安装ffmpeg ffmpeg是一套非常强大的音 ...
- U盘安装各种系统方法总结
一.软通牒(UltraISO) 1.安装系统CentOS-5.11-x86_64-bin-1to9 使用软通牒(UltraISO)打开CentOS-5.11-x86_64-bin-1of9.iso,启 ...
- sitecore开发入门之Sitecore字典结构最佳实践
使用Sitecore时,一个重要的主题是如何为您的网站处理不同的语言和区域.Sitecore对此的回答是使用字典项,它基本上只代表键/值定义.但是,这个字典项可以设置为具有不同的语言版本,这几乎允许您 ...
- Lombok 在继承类上面的注意
找不到符号异常 在控制台项目中配置Lombok 编译会报错 找不到符号 解决 方法: 父类字段ToString不了 @Data @NoArgsConstructor @AllArgsConstruct ...
- Jenkins介绍和安装及配合GitLab代码自动部署
Jenkins是什么? 基于JAVA的开源的自动化系统平台 加速自动化CI,CD任务及流水线,所有类型的任务:构建,测试,部署等 丰富的插件生态系统支持功能扩展,1400+插件和SCM,测试,通知,报 ...
- Machine Learning--week3 逻辑回归函数(分类)、决策边界、逻辑回归代价函数、多分类与(逻辑回归和线性回归的)正则化
Classification It's not a good idea to use linear regression for classification problem. We can use ...
- 【C#数据结构系列】图
一:图 图状结构简称图,是另一种非线性结构,它比树形结构更复杂.树形结构中的结点是一对多的关系,结点间具有明显的层次和分支关系.每一层的结点可以和下一层的多个结点相关,但只能和上一层的一个结点相关.而 ...