当前位置:网站首页>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;
}
边栏推荐
- Data link layer protocol -- Ethernet protocol
- The market is right
- AUTOSAR from getting started to mastering 100 lectures (105) - protection mechanism of AUTOSAR timing for functional safety
- How can test / development programmers with 5 years of experience break through the technical bottleneck? Common problems in big factories
- ESWC 2018 | R-GCN:基于图卷积网络的关系数据建模
- Summary of UPR optimization suggestions of unity
- Only list the data of the specified field GetData ($table, '*', $where, $order)
- Sudden! Britain accuses Huawei of major defects in its equipment (with report)
- MySQL 中RDS 链接数突然上涨怎么查?
- Pychart configuration pyqt5
猜你喜欢

The strongest JVM in the whole network is coming!

5年经验的大厂测试/开发程序员,怎样突破技术瓶颈?大厂通病......

Idea2021 installation

Salt and ice particles cannot be distinguished

QT download installation tutorial

【基于stm32f103的SHT30温湿度显示】

ThreadLocal Kills 11 consecutive questions

Gradle test and idea test

Web: compiling big refactoring from 10 to 1

1. If function of Excel
随机推荐
[internship] processing time
深入掌握Service
mitt.js:小型事件发布订阅库
Most of the time, it's probability
01 create project warehouse
In the Internet of things market, Bosch sensor has launched a number of new solutions
LVGL 8.2 Textarea
Source code | opencv DNN + yolov7 target detection
PyG搭建GCN实现链接预测
Swagger simple quick start tutorial
Paper:《Peeking Inside the Black Box: Visualizing Statistical Learning with Plots of Individual Condi
在开发或调试IP直接方案时需要注意Host的值跟直接的IP要一致
The interviewer asked MySQL transactions, locks and mvcc at one go. I
Ora-01460: conversion request cannot be implemented or unreasonable
Millet 100W fast charging, 50W wireless charging technology exposure! Oppo Shen Yiren responded: boring!
Interpretation and download of the report | ink Tianlun July database industry report, be prepared for danger in times of safety, and safety first
Only list the data of the specified field GetData ($table, '*', $where, $order)
ThreadLocal Kills 11 consecutive questions
GBase JDBC 连接数据库异常
Information System Project Manager --- Chapter IX examination questions of project human resource management over the years