当前位置:网站首页>P3379 【模板】最近公共祖先(LCA)
P3379 【模板】最近公共祖先(LCA)
2022-06-10 13:31:00 【surocco】
题目描述
如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先。
输入格式
第一行包含三个正整数 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
注释都写在代码里啦
AC代码:
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <set>
#define bug(x) cout<<#x<<"=="<<x<<endl;
#define lowbit(x) x&(-x)
#define INF 0x7fffffff
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
#define int long long
const int maxn = 500010;
VI son[maxn];
int n, m, k, depth[maxn], fa[maxn][30], a, b, maxd;
void dfs(int x, int pre)//x表示当前dfs节点,pre为其父节点
{
depth[x] = depth[pre] + 1;//计算x节点深度
//maxd = max(maxd, depth[x]);
fa[x][0] = pre;//向上走一步即为其父节点
for (int i = 1; (1 << i) <= depth[x]; ++i)
fa[x][i] = fa[fa[x][i - 1]][i - 1];//x向上走2^i步等同于x向上走2^i-1,再走2^i-1;
for (int i = 0; i < son[x].size(); ++i) {
if (son[x][i] != pre) dfs(son[x][i], x);//递归处理
}
}
int up(int x, int d)//计算x向上走d步的节点
{
int ret = x;//初始化
for (int i = 26; i >= 0; --i)//考虑d的二进制表示为1的位置,用之前的处理向上跳跃
if ((1 << i) & d)
ret = fa[ret][i];
return ret;
}
int lca(int x, int y) {//计算两个节点之间的lca
if (depth[x] < depth[y])
swap(x, y);//保证x深度较大
x = up(x, depth[x] - depth[y]);
if (x == y)
return x;
for (int i = 26; i >= 0; --i)
if (fa[x][i] != fa[y][i])
x = fa[x][i], y = fa[y][i];//如果跳2^i后不一样就跳上去
return fa[x][0];
}
signed main()
{
cin >> n >> m >> k;
for (int i = 1; i < n; i++) {
cin >> a >> b;
son[a].push_back(b);
son[b].push_back(a);
}
depth[0] = 0;
dfs(k, 0);//k是根节点
for (int i = 1; i <= m; ++i) {
cin >> a >> b;
cout << lca(a, b) << endl;
}
return 0;
}边栏推荐
- 32、树莓派的简单测试串口通信和超声波模块测距
- 「大模型」之所短,「知识图谱」之所长
- 小笔记-简单但够用系列_yapi 返回参数 data 应当是 object 类型问题解决记录
- From "chemist" to developer, from Oracle to tdengine, two important choices in my life
- im即时通讯开发:进程被杀底层原理、APP应对被杀技巧
- Qt: accessing controls in other forms
- Lecture on linear dynamic programming
- 32. Simple test of raspberry pie serial port communication and ultrasonic module ranging
- Google Earth Engine(GEE)——基于s2影像的实时全球10米土地利用/土地覆盖(LULC)数据集
- #yyds干货盘点# 解决剑指offer:跳台阶扩展问题
猜你喜欢

Google Earth Engine(GEE)——基于s2影像的实时全球10米土地利用/土地覆盖(LULC)数据集

Ekuiper newsletter 2022-05 protobuf codec support, visual drag and drop writing rules

苹果生产线迁离,说明5G工业互联、智能制造对中国制造帮助有限

如果再写for循环,我就锤自己了
![buuctf [PHP]XDebug RCE](/img/e2/bcae10e2051b7e9dce918bf87fdc05.png)
buuctf [PHP]XDebug RCE

【无标题】

WT2003H4-16S 语音芯片按键录音及播放应用解析

3. 网页开发工具 VS Code
![[Netease Yunxin] in depth analysis of the design of](/img/17/7c0b7db01310f8dd31e9021f8d5516.png)
[Netease Yunxin] in depth analysis of the design of "circle group" relationship system | series of articles on "circle group" technology

将anaconda的bin目录加入PATH
随机推荐
About native SQL and database methods in PHP framework
Yyds dry goods inventory # solve the problem of sword finger offer: jump step expansion
如果再写for循环,我就锤自己了
Use and inspection of safety tools and instruments
Pycharm安装详细教程
How about the one-stop machine learning opening platform mlflow?
What is the p value of a gene?
Let resources flow freely in the cloud and locally
[Huang ah code] Why is php7 twice as fast as PHP5?
eKuiper Newsletter 2022-05|protobuf 编解码支持、可视化拖拽编写规则
How does the API detect security configuration errors?
TextInputLayout使用详解
10 competitive airpods Pro products worth your choice
Final exam - Principles of compilation
[FAQ] résumé des problèmes courants et des solutions lors de l'utilisation de l'interface API rest du Service de santé sportive
Ten easy-to-use cross browser testing tools to share, good things worth collecting
Lecture on linear dynamic programming
[Netease Yunxin] in depth analysis of the design of "circle group" message system | series of articles on "circle group" technology
Multithreading killer ---countdownlatch & cyclicbarrier
解决win10虚拟机和主机不能互相粘贴复制的问题