当前位置:网站首页>POJ 1330 Nearest Common Ancestors (lca)
POJ 1330 Nearest Common Ancestors (lca)
2022-07-28 04:55:00 【gongyuandaye】
The question : Band lca.
Answer key :lca
The degree of penetration is 0 It's the beginning of dfs.
Pay attention to the directed side .
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int MAXN = 1e4 + 5;
int t, n, q;
vector<int> v[MAXN];
int fa[MAXN][31], dep[MAXN], ind[MAXN];
void init() {
for (int i = 1; i <= n; i++) v[i].clear(), ind[i] = 0;
// memset(fa, 0, sizeof(fa));
// memset(cost, 0, sizeof(cost));
// memset(dep, 0, sizeof(dep));
}
void dfs(int root, int fno) {
//1 0
fa[root][0] = fno;
dep[root] = dep[fa[root][0]] + 1;
for (int i = 1; i < 31; ++i) {
fa[root][i] = fa[fa[root][i - 1]][i - 1];
}
int sz = v[root].size();
for (int i = 0; i < sz; ++i) {
if (v[root][i] == fno) continue;
dfs(v[root][i], root);
}
}
int lca(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
int tmp = dep[y] - dep[x];
for (int j = 0; tmp; ++j, tmp >>= 1)
if (tmp & 1) y = fa[y][j];
if (y == x) return x;
for (int j = 30; j >= 0 && y != x; --j) {
if (fa[x][j] != fa[y][j]) {
x = fa[x][j];
y = fa[y][j];
}
}
return fa[x][0];
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
init();
int x, y;
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
v[x].push_back(y);
//v[y].push_back(x);
ind[y]++;
}
for (int i = 1; i <= n; i++) {
if (ind[i] == 0) {
dfs(i, 0);
break;
}
}
scanf("%d%d", &x, &y);
printf("%d\n", lca(x, y));
}
return 0;
}
边栏推荐
- [daily question 1] 735. Planetary collision
- Analyze the emotional elements contained in intelligent sweeping robot
- MySQL partition table transformation
- Visual studio 2019 new OpenGL project does not need to reconfigure the environment
- Angr(十一)——官方文档(Part2)
- [daily one] visual studio2015 installation in ancient times
- FPGA: use PWM wave to control LED brightness
- App test process and test points
- Alibaba interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
- 【CPU占用高】software_reporter_tool.exe
猜你喜欢

Domain name (subdomain name) collection method of Web penetration

With a monthly salary of 15.5K, he failed to start a business and was heavily in debt. How did he reverse the trend through software testing?

UI automation test farewell from now on, manual download browser driver, recommended collection

如何在 FastReport VCL 中通过 Outlook 发送和接收报告?

05.01 string

Jupyter notebook installation code prompt function

Internet of things industrial serial port to WiFi module wireless routing WiFi module selection

猿辅导技术进化论:助力教与学 构想未来学校

Comprehensively analyze the differences between steam and maker Education

Special topic of APP performance design and Optimization - poor implementation affecting performance
随机推荐
Angr(十一)——官方文档(Part2)
你必需要了解的saas架构设计?
The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight
[idea] check out master invalid path problem
数据安全逐步落地,必须紧盯泄露源头
excel实战应用案例100讲(十一)-Excel插入图片小技巧
低代码是开发的未来吗?浅谈低代码平台
Inspire domestic students to learn robot programming education for children
alter和confirm,prompt的区别
[Sylar] framework Chapter 23 summary of module chapter
Performance comparison between set and list
Clickhouse填坑记2:Join条件不支持大于、小于等非等式判断
Depth traversal and breadth traversal of tree structure in JS
UI automation test farewell from now on, manual download browser driver, recommended collection
[II. Mobile web page development] 2D & 3D conversion and animation, mobile terminal layout, responsive layout
[Sylar] practical part - redis based parameter query service
Jupyter notebook installation code prompt function
(克隆虚拟机步骤)
【CPU占用高】software_reporter_tool.exe
[Oracle] 083 wrong question set