当前位置:网站首页>P4281 [AHOI2008]紧急集合 / 聚会(LCA)
P4281 [AHOI2008]紧急集合 / 聚会(LCA)
2022-07-05 00:02:00 【eva_can(not)survive】
[AHOI2008]紧急集合 / 聚会 - 洛谷https://www.luogu.com.cn/problem/P4281这题很明显要用LCA写,求3个点两两LCA然后判断,如果这3个LCA相等那么他们就在该点汇合,若有一个不一样则,在那个不一样的汇合,不可能出现三个都不一样的情况。
正确性画个图就可以判断出来。
#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;
}
边栏推荐
- ECCV 2022 | Tencent Youtu proposed disco: the effect of saving small models in self supervised learning
- JS convert pseudo array to array
- 业务实现-日志写到同一个行数据里面
- Cross domain request
- He worked as a foreign lead and paid off all the housing loans in a year
- 微软禁用IE浏览器后,打开IE浏览器闪退解决办法
- Expand your kubecl function
- Using fast parsing intranet penetration to realize zero cost self built website
- 积分商城游戏设置的基本要点
- js如何实现数组转树
猜你喜欢
Consolidated expression C case simple variable operation
French scholars: the explicability of counter attack under optimal transmission theory
The caching feature of docker image and dockerfile
Microservice
圖解網絡:什麼是網關負載均衡協議GLBP?
用快解析内网穿透实现零成本自建网站
PMP certificate renewal process
同事的接口文档我每次看着就头大,毛病多多。。。
Skills in analyzing the trend chart of London Silver
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
随机推荐
Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
高配笔记本使用CAD搬砖时卡死解决记录
Acrel-EMS综合能效平台在校园建设的意义
PMP证书续证流程
The pit of sizeof operator in C language
How to avoid arc generation—— Aafd fault arc detector solves the problem for you
[paper reading] cavemix: a simple data augmentation method for brain vision segmentation
The caching feature of docker image and dockerfile
22-07-02周总结
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
Summary of week 22-07-02
How many triangles are there in the golden K-line diagram?
js正则表达式之中文验证(转)
股票账户佣金怎么调低,炒股佣金怎么调低网上开户安全吗
华泰证券低佣金的开户链接安全吗?
What is the difference between port mapping and port forwarding
Instructions for go defer
挖财学院开户安全的吗?开户怎么开?
他做国外LEAD,用了一年时间,把所有房贷都还清了