BZOJ1787/Luogu4281: [Ahoi2008]Meet 紧急集合
画画图可知,三点\(lca\)必有两相同,\(a,b,c\)距离为\(dis_a + dis_b + dis_c - dis_{lca(a,b)} - dis_{lca(b,c)} - dis_{lca(a,c)}\)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#else
#define D_e_Line ;
#endif
struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;
const int N = 500007;
struct Edge{
	int nxt, pre, w;
}e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v, int w){
	e[++cntEdge] = (Edge){head[u], v, w}, head[u] = cntEdge;
}
int dis[N];
int fa[N], son[N], siz[N], dep[N];
inline void DFS_First(int u, int father){
	fa[u] = father, dep[u] = dep[father] + 1, siz[u] = 1;
	for(register int i = head[u]; i; i = e[i].nxt){
		int v = e[i].pre;
		if(v == father) continue;
		dis[v] = dis[u] + e[i].w;
		DFS_First(v, u);
		siz[u] += siz[v];
		if(!son[u] || siz[v] > siz[son[u]]){
			son[u] = v;
		}
	}
}
int dfn[N], dfnIdx, top[N];
inline void DFS_Second(int u, int ancester){
	top[u] = ancester, dfn[u] = ++dfnIdx;
	if(!son[u]) return;
	DFS_Second(son[u], ancester);
	for(register int i = head[u]; i; i = e[i].nxt){
		int v = e[i].pre;
		if(v != son[u] && v != fa[u]){
			DFS_Second(v, v);
		}
	}
}
inline int LCA(int x, int y){
	while(top[x] != top[y]){
		if(dep[top[x]] < dep[top[y]]) Swap(x, y);
		x = fa[top[x]];
	}
	return dep[x] < dep[y] ? x : y;
}
int main(){
	int n, Ques;
	io >> n >> Ques;
	R(i,2,n){
		int u, v;
		io >> u >> v;
		add(u, v, 1);
		add(v, u, 1);
	}
	DFS_First(1, 0);
	DFS_Second(1, 1);
	while(Ques--){
		int a, b, c;
		io >> a >> b >> c;
		int A = LCA(a, b), B = LCA(b, c), C = LCA(a, c);
//		int disA = dis[a] + dis[b] - (dis[A] << 1);
//		int disB = dis[b] + dis[c] - (dis[B] << 1);
//		int disC = dis[a] + dis[c] - (dis[C] << 1);
		int pos;
		if(A == B) pos = C;
		else if(B == C) pos = A;
		else pos = B;
		printf("%d %d\n", pos, dis[a] + dis[b] + dis[c] - dis[A] - dis[B] - dis[C]);
	}
	return 0;
}

BZOJ1787/Luogu4281: [Ahoi2008]Meet 紧急集合的更多相关文章
- 【BZOJ1787】[Ahoi2008]Meet 紧急集合 LCA
		[BZOJ1787][Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 ... 
- LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合
		LCA [bzoj1787][Ahoi2008]Meet 紧急集合 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1787 注意到边权为一 ... 
- 【bzoj1787】[Ahoi2008]Meet 紧急集合
		1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 2466 Solved: 1117[Submit] ... 
- 【bzoj1787】[Ahoi2008]Meet 紧急集合  倍增LCA
		题目描述 输入 输出 样例输入 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 2 4 4 6 6 6 样例输出 5 2 2 5 4 1 6 0 题解 倍增LCA 首先有集合点 ... 
- bzoj1787[Ahoi2008]Meet 紧急集合&bzoj1832[AHOI2008]聚会
		bzoj1787[Ahoi2008]Meet 紧急集合 bzoj1832[AHOI2008]聚会 题意: 给个树,每次给三个点,求与这三个点距离最小的点. 题解: 倍增求出两两之间的LCA后,比较容易 ... 
- bzoj1787  [Ahoi2008]Meet 紧急集合
		1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 2272 Solved: 1029 [Submi ... 
- BZOJ1787 [Ahoi2008]Meet 紧急集合  【LCA】
		1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3578 Solved: 1635 [Submi ... 
- 【BZOJ-1787&1832】Meet紧急集合&聚会     倍增LCA
		1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 2259 Solved: 1023[Submit] ... 
- bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)
		1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1841 Solved: 857[Submit][ ... 
随机推荐
- 第06组Alpha冲刺(6/6)
			目录 1.1 基本情况 1.2 冲刺概况汇报 1.郝雷明 2.曹兰英 3. 方梓涵 4.曾丽莉 5.鲍凌函 6.杜筱 7.黄少丹 8.詹鑫冰 9.董翔云 10.吴沅静 1.3 冲刺成果展示 1.1 基 ... 
- React设置proxy后依旧报CROS错误
			1.判断表单数据是否为后端接收的类型 POST GET2.axios自动转换问题 手动添加标头这份表单数据包括了files (二进制数据)而标头显示是JSON格式 不符 所以报CROS 更多文章请移步 ... 
- 深度学习与CV教程(12) | 目标检测 (两阶段,R-CNN系列)
			作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/37 本文地址:http://www.showmeai.tech/article-det ... 
- Linux安装netstat命令
			Linux安装netstat命令 1.查找netstat命令所属的依赖包 [root@localhost ~]# yum provides netstat netstat命令的安装包为net-tool ... 
- BUUCTF-ningen
			ningen 从16进制看可以发现其中有压缩包,存在着504b0304,使用binwalk分离即可 压缩包带密码,根据提示是四位纯数字 使用ARCHPR破解即可 
- webpack中文api
			1. 简介 1.Plugins://插件 webpack has a rich plugin interface.Most of the features are internal plug ... 
- Leetcode--Last Stone Weight II
			Last Stone Weight II 欢迎关注H寻梦人公众号 You are given an array of integers stones where stones[i] is the we ... 
- Redis布隆过滤器和布谷鸟过滤器
			一.过滤器使用场景:比如有如下几个需求:1.原本有10亿个号码,现在又来了10万个号码,要快速准确判断这10万个号码是否在10亿个号码库中? 解决办法一:将10亿个号码存入数据库中,进行数据库查询,准 ... 
- Docker Buildx使用教程:使用Buildx构建多平台镜像
			写在前边 记录一下前阵子在X86_64平台使用Docker Buildx构建多平台镜像的办法,包含但不限于构建ARM镜像. 构建环境 软件名 版本 Ubuntu 18.04.2 LTS Docker ... 
- SpringBoot之MongoDB附件操作
			前言 近期自己针对附件上传进一步学习,为了弥足项目中文件上传的漏洞,保证文件上传功能的健壮性和可用性,现在我将自己在这一块的心得总结如下: 一.pom.xml依赖的引入 <dependency& ... 
