当前位置: 首页 > news >正文

【模板】最近公共祖先(LCA)

题目描述

如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先。

输入输出格式

输入格式:

第一行包含三个正整数N、M、S,分别表示树的结点个数、询问的个数和树根结点的序号。

接下来N-1行每行包含两个正整数x、y,表示x结点和y结点之间有一条直接连接的边(数据保证可以构成树)。

接下来M行每行包含两个正整数a、b,表示询问a结点和b结点的最近公共祖先。

输出格式:

输出包含M行,每行包含一个正整数,依次为每一个询问的结果。

输入输出样例

输入样例#1: 复制
5 5 4
3 1
2 4
5 1
1 4
2 4
3 2
3 5
1 2
4 5
输出样例#1: 复制
4
4
1
4
4

说明

时空限制:1000ms,128M

数据规模:

对于30%的数据:N<=10,M<=10

对于70%的数据:N<=10000,M<=10000

对于100%的数据:N<=500000,M<=500000

样例说明:

该树结构如下:

第一次询问:2、4的最近公共祖先,故为4。

第二次询问:3、2的最近公共祖先,故为4。

第三次询问:3、5的最近公共祖先,故为1。

第四次询问:1、2的最近公共祖先,故为4。

第五次询问:4、5的最近公共祖先,故为4。

故输出依次为4、4、1、4、4。

 

倍增就行了;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 500005<<1
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/



ll qpow(ll a, ll b, ll c) {
	ll ans = 1;
	a = a % c;
	while (b) {
		if (b % 2)ans = ans * a%c;
		b /= 2; a = a * a%c;
	}
	return ans;
}

int head[maxn], nxt[maxn], ver[maxn];
int cnt;
int n, m, N;

void addedge(int x, int y) {
	ver[++cnt] = y; nxt[cnt] = head[x]; head[x] = cnt;
}
int grand[maxn][20];
int dep[maxn];
int S;

void init() {
	ms(head);
	N = (int)(log(n) / log(2)) + 1;
	dep[S] = 1; cnt = 0;
}

void dfs(int  rt) {
	for (int i = 1; i <= N; i++)grand[rt][i] = grand[grand[rt][i - 1]][i - 1];
	for (int i = head[rt]; i; i = nxt[i]) {
		int v = ver[i];
		if (v == grand[rt][0])continue;
		grand[v][0] = rt; dep[v] = dep[rt] + 1; dfs(v);
	}
}

int lca(int x, int y) {
	if (dep[x] > dep[y])swap(x, y);
	for (int i = N; i >= 0; i--) {
		if (dep[grand[y][i]] >= dep[x]) {
			y = grand[y][i];
		}
	}
	if (x == y)return x;
	for (int i = N; i >= 0; i--) {
		if (grand[x][i] != grand[y][i]) {
			x = grand[x][i]; y = grand[y][i];
		}
	}
	return grand[x][0];
}

int main()
{
	//ios::sync_with_stdio(0);
	rdint(n); rdint(m); rdint(S); init();
	for (int i = 1; i < n; i++) {
		int x, y; rdint(x); rdint(y); addedge(x, y); addedge(y, x);
	}
	dfs(S);
	while (m--) {
		int a, b; rdint(a); rdint(b); //cout << lca(a, b) << endl;
		printf("%d\n", lca(a, b));
	}
    return 0;
}

 

转载于:https://www.cnblogs.com/zxyqzy/p/10003563.html

相关文章:

  • 端口的作用
  • Scrum立会报告+燃尽图(十一月二十二日总第三十次):加强回归测试
  • Java开发者福音 阿里巴巴宣布连任Java全球管理组织席位
  • FFmpeg(七)音频的播放
  • AI创业公司“一览群智”完成1.5亿元融资,经纬中国、策源资本投资
  • HTTP API 自动化测试从手工测试到平台的演变
  • JS 中的深拷贝与浅拷贝
  • 理解React Hooks
  • Django的Rbac介绍3
  • 毫秒级从百亿大表任意维度筛选数据,是怎么做到的...
  • 网上流传的那些关于链表的面试问题
  • JavaScript的使用你知道几种?(上)
  • 根据开始日期和当前日期,获取当前是第几周
  • 服务发现全量配置整理(更新中)
  • MySql版本查看
  • #Java异常处理
  • AngularJS指令开发(1)——参数详解
  • CSS 专业技巧
  • IDEA 插件开发入门教程
  • iOS 系统授权开发
  • JavaScript创建对象的四种方式
  • k8s 面向应用开发者的基础命令
  • laravel with 查询列表限制条数
  • miaov-React 最佳入门
  • SQLServer之索引简介
  • TypeScript迭代器
  • Vue2 SSR 的优化之旅
  • 百度地图API标注+时间轴组件
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 机器人定位导航技术 激光SLAM与视觉SLAM谁更胜一筹?
  • 马上搞懂 GeoJSON
  • 如何设计一个比特币钱包服务
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 实现菜单下拉伸展折叠效果demo
  • 使用Swoole加速Laravel(正式环境中)
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 原生 js 实现移动端 Touch 滑动反弹
  • #pragma data_seg 共享数据区(转)
  • (4)(4.6) Triducer
  • (java)关于Thread的挂起和恢复
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (LNMP) How To Install Linux, nginx, MySQL, PHP
  • (SpringBoot)第二章:Spring创建和使用
  • (vue)页面文件上传获取:action地址
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (十六)Flask之蓝图
  • (转载)利用webkit抓取动态网页和链接
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • .Net Core与存储过程(一)
  • .NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)...
  • .net6 webapi log4net完整配置使用流程
  • .NET连接MongoDB数据库实例教程
  • @hook扩展分析
  • @JsonSerialize注解的使用
  • @kafkalistener消费不到消息_消息队列对战之RabbitMq 大战 kafka