当前位置:网站首页>P4281 [ahoi2008] emergency assembly / gathering (LCA)
P4281 [ahoi2008] emergency assembly / gathering (LCA)
2022-07-05 00:05:00 【eva_ can(not)survive】
[AHOI2008] Emergency assembly / party - Luogu https://www.luogu.com.cn/problem/P4281 This question obviously needs to be used LCA Write , seek 3 One point, two liang LCA And then determine , If this 3 individual LCA Equal, then they meet at that point , If one is different , At that different meeting , It is impossible to have three different situations .
Correctness can be judged by drawing a picture .
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
#include <bitset>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
using P = pair<int, int>;
int n, m;
int head[MAXN];
int nxt[MAXN];
int ver[MAXN];
int cnt;
int dep[MAXN];
int LOG2[MAXN];
int fafa[MAXN][20];
void add(int x, int y) {
ver[++cnt] = y;
nxt[cnt] = head[x];
head[x] = cnt;
}
void dfs(int p, int fa) {
fafa[p][0] = fa;
for (int i = 1; i <= LOG2[dep[p]]; i++) {
fafa[p][i] = fafa[fafa[p][i - 1]][i - 1];
}
for (int i = head[p]; i; i = nxt[i]) {
int v = ver[i];
if (v == fa)
continue;
dep[v] = dep[p] + 1;
dfs(v, p);
}
}
int lca(int x, int y) {
if (dep[x] < dep[y])
swap(x, y);
while (dep[x] != dep[y]) {
x = fafa[x][LOG2[dep[x] - dep[y]]];
}
if (x == y)
return x;
for (int i = LOG2[dep[x]]; i >= 0; i--) {
if (fafa[x][i] != fafa[y][i])
x = fafa[x][i], y = fafa[y][i];
}
return fafa[x][0];
}
void solve() {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
int tmp1 = lca(x, y);
int tmp2 = lca(y, z);
int tmp3 = lca(x, z);
// printf("====%d %d %d\n", tmp1, tmp2, tmp3);
if (tmp1 == tmp2 && tmp1 == tmp3 && tmp2 == tmp3) {
printf("%d %d\n", tmp1, dep[x] + dep[y] + dep[z] - 3 * dep[tmp1]);
} else if (tmp1 == tmp2 && tmp1 != tmp3) {
printf("%d %d\n", tmp3, dep[x] + dep[z] - 2 * dep[tmp3] + dep[tmp3] + dep[y] - 2 * dep[tmp1]);
} else if (tmp1 == tmp3 && tmp1 != tmp2) {
printf("%d %d\n", tmp2, dep[y] + dep[z] - 2 * dep[tmp2] + dep[tmp2] + dep[x] - 2 * dep[tmp1]);
} else if (tmp2 == tmp3 && tmp1 != tmp2) {
printf("%d %d\n", tmp1, dep[x] + dep[y] - 2 * dep[tmp1] + dep[tmp1] + dep[z] - 2 * dep[tmp2]);
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 2; i <= n; i++) {
LOG2[i] = LOG2[i / 2] + 1;
}
dep[1] = 0;
int x, y;
for (int i = 1; i <= n - 1; i++) {
scanf("%d %d", &x, &y);
add(x, y);
add(y, x);
}
dfs(1, 0);
while (m--)
solve();
return 0;
}边栏推荐
- In the enterprise, win10 turns on BitLocker to lock the disk, how to back up the system, how to recover when the system has problems, and how to recover quickly while taking into account system securi
- Expand your kubecl function
- 业务实现-日志写到同一个行数据里面
- Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record
- Paddleocr tutorial
- 如果炒股开华泰证券的户,在网上开户安全吗?
- MIT-6.824-lab4B-2022(万字思路讲解-代码构建)
- uniapp上传头像
- Acrel-EMS综合能效平台在校园建设的意义
- How to effectively monitor the DC column head cabinet
猜你喜欢

The input of uniapp is invalid except for numbers

Jar batch management gadget

Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...

A new method for analyzing the trend chart of London Silver
![P3304 [SDOI2013]直径(树的直径)](/img/5c/984675bf4517481f80f54657c6c7ad.png)
P3304 [SDOI2013]直径(树的直径)

公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!

【路径规划】RRT增加动力模型进行轨迹规划

Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC

Verilog tutorial (11) initial block in Verilog

如何避免电弧产生?—— AAFD故障电弧探测器为您解决
随机推荐
OpenHarmony资源管理详解
如何将自己的代码作品快速存证,已更好的保护自己劳动成果
JS convert pseudo array to array
Enterprise application business scenarios, function addition and modification of C source code
How to effectively monitor the DC column head cabinet
巩固表达式C# 案例简单变量运算
PMP证书续证流程
Using fast parsing intranet penetration to realize zero cost self built website
Is it safe to open an account in the College of Finance and economics? How to open an account?
PermissionError: [Errno 13] Permission denied: ‘data. csv‘
Pytoch --- use pytoch to realize linknet for semantic segmentation
Jar batch management gadget
[path planning] RRT adds dynamic model for trajectory planning
ECCV 2022 | Tencent Youtu proposed disco: the effect of saving small models in self supervised learning
Cross domain request
Jar批量管理小工具
Fast analysis -- easy to use intranet security software
使用快解析搭建自己的minecraft服务器
Consolidated expression C case simple variable operation
Go pit - no required module provides Package: go. Mod file not found in current directory or any parent