POJ-1330 Nearest Common Ancestors(lca模板题)

Ubuntu安装ToDesk远程控制软件 前几天有同学问我怎么在 Ubuntu 系统上安装 ToDesk 远程控制软件, 帮他安装完成后就 顺手整理了一下安装过程,所以就顺手有了这篇在 Ubuntu 系统上安装 todesk 的文章。 阅读详情

题意:

给定树形图,给定两点,求两点的最近公共祖先。

模板(理论:图论--最近公共祖先LCA-优快云博客):

#include <algorithm>
#include <string.h>
#include <vector>
#include <cstdio>
#include <cmath> 
using namespace std;
const int maxn = 1e4+5;
int t, n, u, v;
vector<int> g[maxn];
int E[2*maxn], cnt;
int R[maxn], dep[maxn];
int f[2*maxn][20];
int vis[maxn];
void dfs(int u, int fa, int depth)
{
	E[++cnt] = u;
	R[u] = cnt, dep[u] = depth;
	for(int i = 0; i < g[u].size(); ++i)
	{
		int v = g[u][i];
		if(v == fa) continue;
		dfs(v, u, depth+1);
		E[++cnt] = u;
	}
}
void rmq_init(int n)
{
	for(int i = 1; i <= n; ++i) f[i][0] = E[i];
	for(int j = 1; (1<<j) <= n; ++j)
	for(int i = 1; i+(1<<j)-1 <= n; ++i)
	{
		int a = f[i][j-1], b = f[i+(1<<(j-1))][j-1];
		f[i][j] = dep[a] < dep[b]? a: b;
	}
}
int rmq(int l, int r)
{
	int k = log2(r-l+1);
	int a = f[l][k], b = f[r-(1<<k)+1][k];
	return dep[a] < dep[b]? a: b;
}
int lca(int u, int v)
{
	int l = R[u], r = R[v];
	if(l > r) swap(l, r);
	return rmq(l, r);
}
void init()
{
	for(int i = 1; i <= n; ++i) g[i].clear();
	cnt = 0;
	memset(vis, 0, sizeof vis);
}
int main()
{
	for(scanf("%d", &t); t--;)
	{
		scanf("%d", &n); init();
		for(int i = 1; i < n; ++i)
		{
			scanf("%d %d", &u, &v);
			g[u].push_back(v);
			vis[v] = 1;
		}
		scanf("%d %d", &u, &v);
		for(int i = 1; i <= n; ++i)
		if(!vis[i])
		{
			dfs(i, i, 1);
			break;
		}
		rmq_init(cnt);
		printf("%d\n", lca(u, v));
	}
	return 0;
}

继续加油~

MAX30102传感器实战:从寄存器配置到Arduino心率血氧监测 本文详细介绍了MAX30102传感器在Arduino平台上的实战应用,从寄存器配置到心率血氧监测的全过程。通过清晰的代码示例和实用技巧,帮助开发者快速实现脉搏血氧仪功能,包括I2C通信、FIFO数据处理及算法实现,适用于健康监测和可穿戴设备开发。 阅读详情

相关推荐

Balser相机的硬件触发实战:从连线到信号处理

本文详细介绍了Balser相机硬件触发的实战应用,从硬件接线、信号定义到软件配置(Pylon Viewer及API)提供了完整指南。重点阐述了如何利用外部物理电信号实现微秒级精准触发,解决工业场景下的同步与延迟问题,并分享了信号调理、抗干扰等高级实战经验。

y9z0a1b的博客 338

POJ 1330 Nearest Common Ancestors (LCA)

Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19435 Accepted: 10294 Description A rooted tree is a well-known data structure i

Tc的专栏 901

沈阳计算机研究所复试,沈阳计算所复试试题附答案.doc

沈阳计算所复试试题附答案目录数据结构1200612007220085200914操作系统16200616200718200818200919组成原理20200620200720200821200921经典算法22数据结构2006数据结构部分(共25分,第1题10分,第2题15分)在(下图)所示的有向图中:v1 v2v4 ...

weixin_42511053的博客 863

POJ 1330 Nearest Common Ancestors

题目: http://poj.org/problem?id=1330 题意: 给出一棵树,求其中两个点的最近公共祖先。 思路: 测试模版。 代码: const int MAXN = 101001; const int MAXM = 101001; const int MAXQ = 101001; struct Tree{ int head[MAXN];//前向星存图

MyWorld 404

【Excel VBA 编程】表格内自动生成进度条

在表格中生成进度条,进度条需要自适应单元格的宽度,根据不同进度区间变换颜色,并且按照进度数据大小显示其长短

wei1019的专栏 1519

[Godot][入门] 安装与版本选择:3.x 还是 4.x?(按项目类型一键决策)

摘要: Godot引擎版本选择需根据项目需求权衡3.x与4.x。Godot4.x(如4.2)适合3D开发和新项目,支持Vulkan和GDScript2.0,但生态尚不成熟;Godot3.x(如3.5.3)则更适合稳定2D开发、Web导出及依赖现有插件/教程的场景。安装方式极简,官网下载解压即可运行。新手建议:短期高效选3.x,长期探索选4.x,旧项目勿盲目迁移。附版本对比表及决策树,助开发者快速匹配需求。

技术小甜甜:传播可落地技术,帮助个人和企业降低解决真实场景问题的成本 912

POJ1330(倍增LCA解法)

传送门 题意:给你一颗树,让你找出两点的最近公共祖先。 题解:LCA模板题,这里我使用倍增的方法解决 我使用倍增来在线求 LCA ,时间和空间复杂度分别是 O((n+q)logn) 和 O(nlogn) 。q是查询次数。 算法基本思想:     ①如果 a 和 b 深度不同,先把深度调浅,使他变得和浅的那个一样     ②现在已经保证了 a 和 b 的深度一样,所以我们只要把两个一起一步一步往上移...

weixin_43843730的博客 807

uniapp(安卓苹果APP端):解决video组件播放视频卡顿,视频一直加载非常缓慢或加载不出来解决方案,解决多个video组件视频播放很卡一直加载非常慢怎么办(无论真机调试还是本地测试都完美搞定)

uni-app APP端video组件播放视频卡顿,uniapp video app 加载不出来解决方案,uniapp app使用video视频播放卡顿,uni-app APP端video组件播放视频卡顿_uniapp video老是加载,uniapp开发app视频在真机上卡顿,安卓app播放视频很卡,苹果ios真机运行播放视频加载很慢怎么办,解决UniApp中MP4视频播放卡顿问题,解决m3u8视频播放很卡的问题,uniapp 使用video组件,部分视频加载非常慢,uniapp视频播放卡顿,安卓视频播放

✅ 精选推荐文章 7916

POJ1330(Nearest Common Ancestors)

Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4129 A...

weixin_30470857的博客 216

《QEMU/KVM源码分析与应用》读书笔记4 —— 第2章 QEMU基本组件(1)

《QEMU/KVM源码分析与应用》读书笔记4 —— 第2章 QEMU基本组件(1)

phmatthaus的专栏 1475

面向干净核心的并行扩展:SAP BTP 上的 side-by-side extensions 全面解读

摘要: SAP的side-by-side extensions是一种与核心系统解耦的云原生扩展模式,通过API和事件驱动实现与SAP S/4HANA等核心系统的松耦合集成。该模式支持独立升级、弹性扩展和高效创新,是SAP官方推荐的clean core关键实现路径。其典型架构包括接口层(OData/REST API、事件通道)、运行层(Cloud Foundry/Kyma/CAP框架)和体验层(Fiori/UI5)。适用场景包括需快速迭代、复用云服务或服务外部用户的业务扩展。实施时需关注安全治理(如XSUAA

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 306

poj 1330 (LCA模板题)

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9838   Accepted: 5224 Des

志当存高远 1258

使用pyqt5实现可勾选的测试用例界面

使用pyqt5实现可勾选的测试用例界面

天赐好车的博客 629

POJ 1330 Nearest Common Ancestors (LCA模板题)

题目 最裸的LCA模板题 分析 可以做的方式很多 离线 Tarjian算法 在线 LCA倍增法 在线 dfs + ST表算法 下面给出 ①:Tarjian + 邻接表, (POJ不支持万能头文件,和c++11,所以下面代码要修改才能提交) #include <bits/stdc++.h> #pragma GCC diagnostic error "-std=c++11" #defi...

Stormjing的博客 294

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

POJ 1330 Nearest Common Ancestors【tarjan】【简单LCA】【模板题】【并查集】

题目链接:http://poj.org/problem?id=1330 题目大意:给出n个父子关系,不会出现回路,求最原始祖先(根节点)。 照大佬的板子自己手码了一遍,还挺长,,, AC代码: #include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include&...

weixin_43499182的博客 208

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

【LCA模板题】poj 1330 Nearest Common Ancestors

LCA模板题

Gzbgreat的博客 399

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

poj1330 Nearest Common Ancestors(lca模板题)

题目 T组样例,N(2<=N<=1e4)个节点, 给出N-1条有向边u->v后,询问一组u和v的lca 思路来源 《挑战程序竞赛》P330-331 题解 lca裸题,用RMQ+ST就好了,其实倍增也是应该掌握的 由于只有一组询问,所以预处理欧拉序dfn id用来记录dfs序中第一次访问的时间戳 不妨id[u]<id[v],那么lca(u,v)=dfn[[i...

小衣同学的博客 300

【POJ - 1330】Nearest Common Ancestors(lca,模板题)

题干: A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is th...

xuanweiace的博客 391

POJ 1330 Nearest Common Ancestors 【LCA模板题】

任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000K Total Submissions:34942 Accepted:17695 Description A rooted tree is a well-know...

Mod_13的博客 109

POJ 1330(Nearest Common Ancestors-Lca模板题)

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13559   Accepted: 7236 Description A rooted tree is a well-known data structure in c

nike0good |Oier&ACMer | 熟能生巧 1506

POJ 1330 Nearest Common Ancestors (LCA模板题)

题目链接:http://poj.org/problem?id=1330 #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;algorithm&gt; using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)&l...

满月照亮的路 246

poj-1330 Nearest Common Ancestors(LCA模板题)

题目链接:  http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23822   Accepted: 12405 Description

TUT我好菜啊 398

POJ-1330-Nearest Common Ancestors(LCA+倍增模板题)

题目链接:http://poj.org/problem?id=1330 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:   In the figure, each node is labeled wit...

永远鲜红の幼月的博客 168

F - Nearest Common Ancestors POJ - 1330 (LCA模板题)

A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:   In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the roo...

立志变成java巨头 217

POJ 1330 Nearest Common Ancestors 倍增算法的LCA

POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义。f[i][j]表示i节点的第2j个父亲是多少      这个代码不是我的,转自 邝斌博客 1 /* *********************************************** 2

Macan - 生活点滴 533

POJ 1330:Nearest Common Ancestors ← 暴力法(同步前进法‌)+ LCA

“同步前进法‌”是一种求 LCA 的暴力法,即将深度较深的节点提升至与另一节点相同深度后,两节点同步向上移动,直到相遇的节点即为 LCA‌。例如,若节点 x 深度大于 y 的深度,则先将 x 提升至与 y 同深度,再共同向上搜索‌。

hnjzsyjyj的专栏 1139
上一篇: HDU-5459 Jesus Is Here(思维递推)
下一篇: POJ-3264 Balanced Lineup(rmq模板题)
薄层
博客等级 码龄10年 242粉丝 289原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值