LCA求树上两点最短距离,如果a,b之间距离小于等于k并且奇偶性与k相同显然YES;或者可以从a先走到x再走到y再走到b,并且a,x之间距离加b,y之间距离+1小于等于k并且奇偶性与k相同也输出YES;或者从a走到y再走到x再走到b,并且总距离+1小于等于k并且奇偶性与k相同同样输出YES;否则输出NO。

 #define HAVE_STRUCT_TIMESPEC
#include <bits/stdc++.h>
using namespace std;
int dis[];
int cnt;
struct edge{int to,nxt;
//int v;
};
edge e[<<];
int tot,head[];
void adde(int u,int v){
e[++tot].to=v;
//e[tot].v=w;
e[tot].nxt=head[u];
head[u]=tot;
}
int fa[][];
int dep[];
void bfs(int rt){
queue<int> q;
dep[rt]=;
fa[rt][]=rt;
q.push(rt);
while(!q.empty()){
int tmp=q.front();q.pop();
for(int i=;i<;i++)
fa[tmp][i]=fa[fa[tmp][i-]][i-];
for(int i=head[tmp];i;i=e[i].nxt){
int v=e[i].to;
if(v==fa[tmp][])continue;
dep[v]=dep[tmp]+;
fa[v][]=tmp;
q.push(v);
}
}
}
int query_lca(int u,int v){
if(dep[u]>dep[v])swap(u,v);
int hu=dep[u],hv=dep[v];
int tu=u,tv=v;
for(int det=hv-hu,i=;det;det>>=,i++)
if(det&)tv=fa[tv][i];
if(tu==tv)return tu;
for(int i=;i>=;i--){
if(fa[tu][i]==fa[tv][i])continue;
tu=fa[tu][i];tv=fa[tv][i];
}return fa[tu][];
}
int query_kth(int u,int k){
for(int det=k,i=;det;det>>=,i++)if(det&)u=fa[u][i];return u;
}
int query_dist(int a,int b){
int lca=query_lca(a,b);
return dep[a]+dep[b]-*dep[lca];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<n;++i){
int u,v;
cin>>u>>v;
adde(u,v);
adde(v,u);
}
bfs();
int q;
cin>>q;
while(q--){
int x,y,a,b,k;
cin>>x>>y>>a>>b>>k;
int dist=query_dist(a,b);
if(dist<=k&&dist%==k%){
cout<<"YES\n";
continue;
}
int dist2=query_dist(a,x);
int dist3=query_dist(b,y);
if(dist2+dist3+<=k&&(dist2+dist3+)%==k%){
cout<<"YES\n";
continue;
}
int dist4=query_dist(a,y);
int dist5=query_dist(b,x);
if(dist4+dist5+<=k&&(dist4+dist5+)%==k%){
cout<<"YES\n";
continue;
}
cout<<"NO\n";
}
return ;
}

Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)的更多相关文章

  1. Codeforces Round #620 (Div. 2)E LCA

    题:https://codeforces.com/contest/1304/problem/E 题意:给定一颗树,边权为1,m次询问,每次询问给定x,y,a,b,k,问能否在原树上添加x到y的边,a到 ...

  2. Codeforces Round #620 (Div. 2)

    Codeforces Round #620 (Div. 2) A. Two Rabbits 题意 两只兔子相向而跳,一只一次跳距离a,另一只一次跳距离b,每次同时跳,问是否可能到同一位置 题解 每次跳 ...

  3. Codeforces Round #620 (Div. 2) E

    LCA的倍增 模板: ], depth[maxn]; int dist[maxn],head[maxn]; void add(int u,int v,int dist0){ a[tot].next=h ...

  4. 模板倍增LCA 求树上两点距离 hdu2586

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 课上给的ppt里的模板是错的,wa了一下午orz.最近总是被坑啊... 题解:树上两点距离转化为到根的距离 ...

  5. Codeforces Round #538 (Div. 2) C 数论 + 求b进制后缀零

    https://codeforces.com/contest/1114/problem/C 题意 给你一个数n(<=1e8),要你求出n!在b进制下的后缀零个数(b<=1e12) 题解 a ...

  6. Codeforces Round #620 (Div. 2) 题解

    A. Two Rabbits 思路: 很明显,如果(y-x)%(a+b)==0的话ans=(y-x)/(a+b),否则就为-1 #include<iostream> #include< ...

  7. Codeforces Round #620 (Div. 2) A-F代码 (暂无记录题解)

    A. Two Rabbits (手速题) #include<bits/stdc++.h> using namespace std; typedef long long ll; int ma ...

  8. Codeforces Round #620 (Div. 2) A. Two Rabbits

    Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a p ...

  9. Codeforces Round #620 (Div. 2)D dilworld定理

    题:https://codeforces.com/contest/1304/problem/D 题意:给定长度为n-1的只含’>'和‘<’的字符串,让你构造出俩个排列,俩个排列相邻的数字之 ...

随机推荐

  1. PAT (Advanced Level) Practice 1054 The Dominant Color (20 分)

    Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of i ...

  2. 0级搭建类010-Oracle Linux 6.x安装(OEL 6.10) 公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  3. 0级搭建类009-Fedora 30 安装(F30) 公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  4. phpstorm安装bootstrap插件

    一个插件可以很好的让我们工作节约时间 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 We ...

  5. Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】

    本文实例讲述了Java实现的上传并压缩图片功能.分享给大家供大家参考,具体如下: 先看效果: 原图:1.33M 处理后:27.4kb 关键代码: package codeGenerate.util; ...

  6. css实现聊天气泡效果

      --------------------------------------- css功能强大,能实现很多炫 酷的效果,今天给大家分享 用css3绘制聊天气泡的方法: -------------- ...

  7. 单调栈(POJ2559)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  8. MySQL的联表查询

    MySQL的联表查询 首选:分析查询的字段来自哪些表 进而:确定交集 然后:确定判断的条件 比如:从student表 和 result表 查学号.考试名称.学时.考试日期.考试成绩 表1: 学号 考试 ...

  9. AcWing 10. 有依赖的背包问题

    #include <cstring> #include <iostream> #include <algorithm> using namespace std; ; ...

  10. Switch The LED Holiday Light To Illuminate The Cheerful Holidays

    I like how LED holiday lights add a little magic to the holidays. Want an easy way to reduce your va ...