当前位置:网站首页>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;
}边栏推荐
- Cardview usage and properties
- 3. 网页开发工具 VS Code
- About native SQL and database methods in PHP framework
- Can qiniu open an account? Can the security of securities companies be directly opened on the app
- 多云管理平台cmp是什么意思?谁能清楚解释一下
- 将anaconda的bin目录加入PATH
- CardView使用及属性
- Use and inspection of safety tools and instruments
- Ten easy-to-use cross browser testing tools to share, good things worth collecting
- [Huang ah code] Why is php7 twice as fast as PHP5?
猜你喜欢

The relocation of Apple's production line shows that 5g industrial interconnection and intelligent manufacturing have limited help for manufacturing in China

How to locate the hot problem of the game

Z-Wave ecosystem status report in 2022

Student status management system

如果再写for循环,我就锤自己了

The essence of linear algebra 6 inverse matrix, column space and zero space

Simple integration of client go gin six list watch two (about the improvement of RS, pod and deployment)

高性能实战Alibaba Sentinel笔记,深度还原阿里微服务高并发方案

Qt: 访问其他窗体中的控件

The shortcomings of the "big model" and the strengths of the "knowledge map"
随机推荐
win10虚拟机下载安装流程
What are the common automated test frameworks? Shanghai software testing company Amway
618. How to prepare for the great promotion
将anaconda的bin目录加入PATH
buuctf [Jupyter]notebook-rce
[raise bar C #] how to call the base of the interface
解决VMWareStation安装 tools 时 D:\setup.exe 找不到的问题
CardView使用及属性
buuctf [PHP]CVE-2019-11043
Nanomq newsletter 2022-05 | release of V0.8.0, new webhook extension interface and connection authentication API
Cardview usage and properties
buuctf [PHP]XDebug RCE
多云管理平台cmp是什么意思?谁能清楚解释一下
小笔记-简单但够用系列_yapi 返回参数 data 应当是 object 类型问题解决记录
10 competitive airpods Pro products worth your choice
buuctf [Jupyter]notebook-rce
【Golang】创建有配置参数的结构体时,可选参数应该怎么传?
Application analysis of key recording and playing of wt2003h4-16s voice chip
Snackbar使用详解
Tablayout usage details (modify text size, underline style, etc.)