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的只含’>'和‘<’的字符串,让你构造出俩个排列,俩个排列相邻的数字之 ...
随机推荐
- oracle Insert 一次插入多条记录
oracle Insert 一次插入多条记录有两种方法: 1)Insert All Into table_name values ... insert all into table_name valu ...
- axios 跨域请求允许带cookie,则服务器Access-Control-Allow-Origin应设置为具体域名,否则请求无法获得返回数据
1.通过允许跨域访问实现了跨域请求,但为了使每个请求带上session信息,我设置了withCredentials ,即: axios.defaults.withCredentials = true ...
- python3练习100题——051
题目:学习使用按位与 & . 不会的知识点,查了一下按位运算. 按位运算符是把数字看作二进制来进行计算的. 运算符 描述 实例 & 按位与运算符:参与运算的两个值,如果两个相应位都为1 ...
- python爬虫----爬取阿里数据银行websocket接口
业务需求:爬取阿里品牌数据银行的自定义模块==>>>人群透视==>>>查看报告==>>数据 发现:数据通过websocket接口传递,此类型接口的详细理 ...
- Deep Clustering Algorithms
Deep Clustering Algorithms 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 本文研究路线:深度自编码器(Deep Autoen ...
- 15. 3Sum、16. 3Sum Closest和18. 4Sum
15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...
- 在vue项目中播放m3u8格式视频
前言:最近公司在做一个线上会议的项目,要求后台网站播放m3u8格式的视频,查找部分资料,总结一下,方便后边查阅 1.在vue工程中安装以下依赖: cnpm install video.js --sa ...
- 22.01.Cluster
1. 클러스터링 iris 데이터셋 확인¶ In [2]: from sklearn import cluster from sklearn import datasets iris = dat ...
- wso2 linux上部署说明
1.启动wso2
- django学习,captcha图形验证码的使用
很多网站在登录或者注册的时候都有验证码,让你去输入. 刚好有这么一款插件,可以满足这个功能 首先,先pip install django-simple-captcha 然后再setting里添加,如 ...