当前位置:网站首页>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;
}
边栏推荐
- Take out system file upload
- (克隆虚拟机步骤)
- Can plastics comply with gb/t 2408 - Determination of flammability
- 提升学生群体中的STEAM教育核心素养
- Leetcode 18. sum of four numbers
- Leetcode 15. sum of three numbers
- Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)
- Geely AI interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
- 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?
- Is low code the future of development? On low code platform
猜你喜欢

Redis类型

App test process and test points

Tiantian AMADA CNC bending machine touch screen maintenance rgm21003 host circuit board maintenance

解析智能扫地机器人中蕴含的情感元素

How to quickly locate bugs? How to write test cases?

Use animatedbuilder to separate components and animation, and realize dynamic reuse
![[idea] check out master invalid path problem](/img/83/d36362ba314177cd6f1f74f3e922cd.png)
[idea] check out master invalid path problem

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?

低代码是开发的未来吗?浅谈低代码平台

What should testers know about login security?
随机推荐
np. The data returned from delete details is the data after deleting the specified dimension
Take out system file upload
Visual studio 2019 new OpenGL project does not need to reconfigure the environment
Test report don't step on the pit
欧拉路/欧拉回路
Installing MySQL under Linux
Chuangyuan will join hands with 50+ cloud native enterprises to explore new models to cross the digital divide
Gerrit operation - rollback a patch_ set
05.01 string
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?
What SaaS architecture design do you need to know?
X Book Keyword Search
Interview fraud: there are companies that make money from interviews
Jupyter notebook installation code prompt function
【CPU占用高】software_reporter_tool.exe
[Sylar] framework -chapter14 tcpserver module
Can plastics comply with gb/t 2408 - Determination of flammability
The go zero singleton service uses generics to simplify the registration of handler routes
Redux basic syntax
[daily one] visual studio2015 installation in ancient times