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. css代码实现switch开关滑动

    效果预览: 代码如下: <style> #toggle-button{ display: none; } .button-label{ position: relative; displa ...

  2. SDN的深入思考(1):SDN的核心本质到底是什么?

    原文链接:https://blog.csdn.net/maijian/article/details/41744535 SDN的概念从提出到现在已经过了4年多了,但是关于SDN最基本的问题,“什么是S ...

  3. Life Forms[poj3294]题解

    Life Forms Description - You may have wondered why most extraterrestrial life forms resemble humans, ...

  4. AC认证技术

    一.认证方式 Dkey认证(数字密钥认证) 1)免认证key,形同usb,插入即通过认证 2)免审计key,也是上网不被记录审计. 单点登录 登录了某点,其他点都能访问:例如登录了支付宝淘宝就不用登录 ...

  5. Treats for the Cows POJ - 3186 dp 区间dp

    //dp[i][j]表示第i次从左边取,第j次从右边取的价值,所以我们可以得到状态方程 //dp[i][j]=max(dp[i-1][j]+(i+j)*a[i],dp[i][j-1]+(i+j)*a[ ...

  6. prach定点化处理

  7. vue加载单文件使用vue-loader报错

    报错信息如下:ERROR in ./src/login.vue Module Error (from ./node_modules/vue-loader/lib/index.js): vue-load ...

  8. VUE 路由参数改变重新刷新数据

    VUE 路由参数改变重新刷新数据 App.vue 里面使用路由,然后通过App.vue文件中的搜索功能搜索刷新路由文件中的列表. 修改 index.js 文件 首先在路由文件 index.js 文件中 ...

  9. centos7重启Mysql命令

    执行命令/etc/init.d/mysql restart重启Mysql服务器

  10. Wannafly Camp 2020 Day 5A Alternative Accounts

    There are n different accounts on the website, and some of them competed in the recent k contests. H ...