Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)
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求树上两点最短距离)的更多相关文章
- Codeforces Round #620 (Div. 2)E LCA
题:https://codeforces.com/contest/1304/problem/E 题意:给定一颗树,边权为1,m次询问,每次询问给定x,y,a,b,k,问能否在原树上添加x到y的边,a到 ...
- Codeforces Round #620 (Div. 2)
Codeforces Round #620 (Div. 2) A. Two Rabbits 题意 两只兔子相向而跳,一只一次跳距离a,另一只一次跳距离b,每次同时跳,问是否可能到同一位置 题解 每次跳 ...
- 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 ...
- 模板倍增LCA 求树上两点距离 hdu2586
http://acm.hdu.edu.cn/showproblem.php?pid=2586 课上给的ppt里的模板是错的,wa了一下午orz.最近总是被坑啊... 题解:树上两点距离转化为到根的距离 ...
- Codeforces Round #538 (Div. 2) C 数论 + 求b进制后缀零
https://codeforces.com/contest/1114/problem/C 题意 给你一个数n(<=1e8),要你求出n!在b进制下的后缀零个数(b<=1e12) 题解 a ...
- Codeforces Round #620 (Div. 2) 题解
A. Two Rabbits 思路: 很明显,如果(y-x)%(a+b)==0的话ans=(y-x)/(a+b),否则就为-1 #include<iostream> #include< ...
- Codeforces Round #620 (Div. 2) A-F代码 (暂无记录题解)
A. Two Rabbits (手速题) #include<bits/stdc++.h> using namespace std; typedef long long ll; int ma ...
- 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 ...
- Codeforces Round #620 (Div. 2)D dilworld定理
题:https://codeforces.com/contest/1304/problem/D 题意:给定长度为n-1的只含’>'和‘<’的字符串,让你构造出俩个排列,俩个排列相邻的数字之 ...
随机推荐
- XMind快捷键汇总
在 XMind: ZEN 中,快捷键是可以大大提高绘图效率的存在.掌握常用的快捷键组合,就可以在键盘上运指如飞,快速地进行思维导图的绘制.还在等什么?感兴趣的朋友,下面就和小编一起来看看吧! XMin ...
- 使用Python爬虫整理小说网资源-自学
第一次接触python,原本C语言的习惯使得我还不是很适应python的语法风格.希望读者能够给出建议. 相关的入门指导来自以下的网址:https://blog.csdn.net/c406495762 ...
- 使用IntersectionObserver制作滚动动画以及其他记录
前言 最近在重做公司项目的主页,正好新来了个UI,整个都重新设计了一下,动画还挺多的.我之前没有怎么玩过这些,踩了挺多坑,最后找到了目前而言最合适的方法,现在做一个记录. 需要把原来的主页从项目中抽出 ...
- Eigen库学习---Map类
Eigen中定义了一系列的vector和matrix,相比copy数据,更一般的方式是复用数据的内存,将它们转变为Eigen类型.Map类很好地实现了这个功能. Map定义 Map(PointerAr ...
- vue自学入门-8(vue slot-scope)
vue自学入门-1(Windows下搭建vue环境) vue自学入门-2(vue创建项目) vue自学入门-3(vue第一个例子) vue自学入门-4(vue slot) vue自学入门-5(vuex ...
- flex布局 居中
display:flex;justify-content: center;检查侧轴是否居中,比如古代竖着写字,检查字是否在每条竹简的中央. display:flex;align-items: cent ...
- php 时间 日期
获取月初与月末 /** * 获取当前月初与月末时间 * * */ $month =8; $year = 2019; $startDay = $year . '-' . $month . '-1'; $ ...
- UVA122 二叉树的层次遍历
题目链接 https://vjudge.net/problem/UVA-122 #include<bits/stdc++.h> using namespace std; #define l ...
- shell循环结构解析:for/while/case
1.for循环结构 for var in item1 item2 ... itemN do command1 command2 ... commandN done 例如,顺序输出当前列表中的数字: # ...
- dubbox生产者与消费者案例
一.首先要将dubbox添加到本地maven仓库 参考: https://blog.csdn.net/try_and_do/article/details/83383861 二.目录结 ...