当前位置:网站首页>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;
}
边栏推荐
- 深入掌握Service
- 中创算力荣获「2022年科技型中小企业」认定
- If you don't know these 20 classic redis interview questions, don't go to the interview!
- [internship] processing time
- C# 之 FileStream类介绍
- Database design process
- MCU experiment record
- How to ensure data consistency between MySQL and redis?
- 暗黑王者|ZEGO 低照度图像增强技术解析
- Web: compiling big refactoring from 10 to 1
猜你喜欢

Pychart configuration pyqt5

Implementation of recommendation system collaborative filtering in spark

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

It we media shows off its wealth in a high profile, and is targeted by hacker organizations. It is bound to be imprisoned

Getting started with scratch

How to ensure data consistency between MySQL and redis?

推荐系统-协同过滤在Spark中的实现

Leetcode55. Jumping game

Unity 之 UPR优化建议汇总

实战|记一次攻防演练打点
随机推荐
01 create project warehouse
QT download installation tutorial
Interpretation and download of the report | ink Tianlun July database industry report, be prepared for danger in times of safety, and safety first
Libenent and libev
Construction of Seata multilingual system
How to transfer NFT metadata from IPFs to smart contracts
数据链路层协议 ——— 以太网协议
The market is right
Idea2021 installation
@ResponseBody注解的总结
LVGL 8.2 Textarea
Anaconda installs jupyter
PHP Baidu qianqianhua installment API
一般在进行数仓迁移过程中,是如何进行数据测试的?
In depth understanding of service
The United States has launched 337 investigations on a number of Chinese companies: Bubugao is full of lying guns!
@Summary of ResponseBody annotation
Gradle test and idea test
Json.tojsonstring cannot pass Boolean
Interviewer: explain the core principle of ThreadLocal