ProgramAlgTrain/20240919/CSP常考算法模板/P3379【模板】LCA_倍增 -邻接表_todo.cpp

54 lines
857 B
C++
Raw Normal View History

2024-09-19 02:22:41 +00:00
#include <bits/stdc++.h>
using namespace std;
const int N = 500001;
int n,m,s;
int depth[N], Log[N];
2024-09-19 03:11:59 +00:00
int dbl[N][20]; //倍增数组
2024-09-19 02:22:41 +00:00
int tot;
vector<int> graph[N];
void dfs(int cur, int fa) {
//todo
}
int lca(int x,int y) {
2024-09-19 03:11:59 +00:00
// 把两个点升至同一高度,再一起跳
2024-09-19 02:22:41 +00:00
// TODO
if(x==y)
return x;
2024-09-19 03:11:59 +00:00
// 两个点同时往上跳跳到LCA的下一层为止
2024-09-19 02:22:41 +00:00
// TODO
return dbl[x][0];
}
/*
2024-09-19 03:11:59 +00:00
O(nlogn)
2024-09-19 02:22:41 +00:00
*/
int main() {
scanf("%d%d%d",&n,&m,&s);
for(int i=1; i<=n-1; i++) {
int x,y;
scanf("%d%d",&x,&y);
//todo
}
2024-09-19 03:11:59 +00:00
dfs(s,0); // 建树
2024-09-19 02:22:41 +00:00
2024-09-19 03:11:59 +00:00
// 预处理,常数优化
2024-09-19 02:22:41 +00:00
for(int i=2; i<=n; i++) {
//todo
}
for(int i=1; i<=m; i++) {
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",lca(x,y));
}
return 0;
}