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的只含’>'和‘<’的字符串,让你构造出俩个排列,俩个排列相邻的数字之 ...
随机推荐
- .net mvc 多文件上传
1.input文件上传设置允许选择多个文件,设置属性 multiple即可 <input type="file" multiple="multiple" ...
- CTF长久练习平台
0x01 XCTF(攻防世界) 攻防世界是ctf爱好者很喜欢的一个平台,不仅是界面风格像大型游戏闯关,里面的各类题目涵盖的ctf题型很广,还分为新手区和进阶区两块: 并且可以在里面组队,做一道题还有相 ...
- DFS-C - N皇后问题
C - N皇后问题 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N,求出有多少种合法的放 ...
- 【巨杉数据库SequoiaDB】巨杉数据库荣获《金融电子化》“金融科技创新奖”
巨杉助力金融科技创新 2019年12月19日,由<金融电子化>杂志社主办.北京金融科技产业联盟协办的“2019中国金融科技年会暨第十届金融科技及服务优秀创新奖颁奖典礼”在京成功召开.来自金 ...
- 在vue项目中使用MD5.js
1.安装 npm install --save js-md5 2.引入 import md5 from 'js-md5'; 3.使用 md5('holle') // bcecb35d0a12baad4 ...
- 跨AppDomain通信
public class AppDomainTest : MarshalByRefObject { public string TestMethodStr(string srcAppDomain) { ...
- swagger2使用
添加依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swag ...
- java的开发步骤----
首先老师给了一个 视频给我:叫我理解java的简单开发步骤: 上两张图: 然后我就理解成了这样::: 简述java开发步骤:第一步: 编写源代码,把源代码写好 :第二部:源代码通过编译器编译成字节码c ...
- many connection errors,更改max_connection_errors的值
https://www.cnblogs.com/tonyccc/p/11496101.html https://blog.csdn.net/li_li_lin/article/details/7276 ...
- Eclipse的Errors in required projec(s)问题
在Eclipse中运行代码时出现Errors exist in required project(s)弹窗提示,但是当前类并无错误,点击Proceed当前类仍然可以运行 错误展示: Errors ex ...