当前位置:网站首页>Logu p3398 hamsters find sugar solution
Logu p3398 hamsters find sugar solution
2022-07-25 04:50:00 【q779】
Luogu P3398 Hamsters look for sugar Answer key
Topic link :P3398 Hamsters look for sugar
The question :
Hamster's and his base (mei) friend (zi)sugar Living in underground caves , The number of each node is 1~n. The underground cave is a tree structure . On this day, hamster plans to go from his bedroom (a) To the restaurant (b), And his base friend is going to be in his bedroom at the same time (c) To the library (d). They all take the shortest path . Now little hamster wants to know , Is it possible to be somewhere , You can meet his friends ?
Hamsters are so weak , And every day zzq Uncle abuse , Please come and save him !
n ≤ 1 0 5 , q ≤ 1 0 5 n \le 10^5,q \le 10^5 n≤105,q≤105
Simplify the meaning of the question , It is to judge whether there is intersection given the path on two trees
set up a , b a,b a,b Of LCA by x x x , c , d c,d c,d Of LCA by y y y
It is not difficult to find that if two trees intersect , That must meet the following requirements At least one condition
- x x x stay c , d c,d c,d On the way
- y y y stay a , b a,b a,b On the way
Consider how to determine whether it is on the path
It's very simple , as long as x x x To c , d c,d c,d The sum of distances equals c c c To d d d Distance of , be x x x It's just c , d c,d c,d On the way
Empathy , as long as y y y To a , b a,b a,b The sum of distances equals a a a To b b b Distance of , be y y y It's just a , b a,b a,b On the way
Then write a multiplier LCA It's over
Time complexity O ( m log n ) O(m \log n) O(mlogn)
Code :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <random>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(1e5+15)
int n,Q,pos=1,head[N],dep[N],lg[N],fa[N][20];
struct Edge{
int u,v,next;}e[N<<1];
void addEdge(int u,int v)
{
e[++pos]={
u,v,head[u]};
head[u]=pos;
}
void dfs(int u,int f)
{
fa[u][0]=f;
dep[u]=dep[f]+1;
for(int i=1; i<=lg[dep[u]]; i++)
fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i=head[u]; i; i=e[i].next)
if(e[i].v!=f)dfs(e[i].v,u);
}
int lca(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
while(dep[x]>dep[y]) x=fa[x][lg[dep[x]-dep[y]]-1];
if(x==y) return x;
for(int i=lg[dep[x]]; i>=0; i--)
if(fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
int dis(int x,int y)
{
int d=lca(x,y);
return abs(dep[x]-dep[d])+abs(dep[y]-dep[d]);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
cin >> n >> Q;
for(int i=1,u,v; i<n; i++)
{
cin >> u >> v;
addEdge(u,v);addEdge(v,u);
}
for(int i=1; i<=n; i++)
lg[i]=lg[i-1]+(1<<lg[i-1]==i);
dfs(1,1);
for(int a,b,c,d; Q--;)
{
cin >> a >> b >> c >> d;
int d1=lca(a,b),d2=lca(c,d);
if(dis(a,d2)+dis(d2,b)==dis(a,b)||dis(c,d1)+dis(d1,d)==dis(c,d))
cout << "Y\n";
else cout << "N\n";
}
return 0;
}
边栏推荐
- 市场的调节
- Paper:《Peeking Inside the Black Box: Visualizing Statistical Learning with Plots of Individual Condi
- Interpretation and download of the report | ink Tianlun July database industry report, be prepared for danger in times of safety, and safety first
- Actual combat | record an attack and defense drill management
- Analysis of lottery winning numbers in history
- Best practice cases of data security in the medical industry (desensitization version)
- Implementation of recommendation system collaborative filtering in spark
- Most of the time, it's probability
- @ResponseBody注解的总结
- 956. Highest billboard pressure DP
猜你喜欢
![[analysis of GPIO register (crl/crh) configuration of STM32]](/img/63/a7b262e95f1dc74201ace9d411b46f.png)
[analysis of GPIO register (crl/crh) configuration of STM32]

Ffmpeg download and installation

Ora-01460: conversion request cannot be implemented or unreasonable

ES6 -- Methods and extensions of array objects, traversal of arrays, and extension methods of strings
![[detailed tutorial] a thorough article on mongodb aggregation query](/img/81/1ac7afa778849b8a4b103107fd9cb6.png)
[detailed tutorial] a thorough article on mongodb aggregation query

QT download installation tutorial

Cannot make qopenglcontext current in a different thread: the solution to pyqt multithread crash

Wechat official account all article download links to get
![Introduction to fundamentals of operations research [1]](/img/79/b613bff74a78ad63f202b9e652220d.png)
Introduction to fundamentals of operations research [1]

5年经验的大厂测试/开发程序员,怎样突破技术瓶颈?大厂通病......
随机推荐
[wechat applet] picker scroll selector (85/100)
etcd学习
Leetcode55. Jumping game
Json.tojsonstring cannot pass Boolean
Detailed explanation of security authentication of mongodb
[cloud picture theory] 247 first introduction to Huawei cloud analysis service
@ResponseBody注解的总结
【基于stm32f103的SHT30温湿度显示】
Dig deep into data dividends, Intel and industry accelerate the implementation of digital economy
5年经验的大厂测试/开发程序员,怎样突破技术瓶颈?大厂通病......
Open source summer interview | "after 00" PMC member Bai Zeping
Geely and Daimler set up a joint venture to produce pure electric smart in China!
深入掌握Pod
Sony announced the closure of Beijing mobile phone factory! The production line will be moved to Thailand, and the cost can be reduced by half!
How to transfer NFT metadata from IPFs to smart contracts
如何取得数据库创建时间?
Database design process
Thinking from the perspective of Aya
Very clear organization
Get the parameters of the browser address bar