Codeforces 1304E. 1-Trees and Queries 代码(LCA 树上两点距离判奇偶)
https://codeforces.com/contest/1304/problem/E
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxbit = ;
const int maxn = 1e5+;
vector<int> G[maxn];
int depth[maxn];
int fa[maxn][maxbit];
int Log[maxn];
int N;
void pre(){
Log[] = -;
Log[] = ,Log[] = ;
for(int i = ;i<maxn;i++) Log[i] = Log[i/] + ;
}
void dfs(int cur,int father){//dfs预处理
depth[cur] = depth[father] + ;//当前结点的深度为父亲结点+1
fa[cur][] = father;//更新当前结点的父亲结点
for(int j = ;(<<j)<=N;j++){//倍增更新当前结点的祖先
fa[cur][j] = fa[fa[cur][j-]][j-];
}
for(int i = ;i<G[cur].size() ;i++){
if(G[cur][i] != father) {//dfs遍历
dfs(G[cur][i],cur);
}
}
}
int LCA(int u,int v){
if(depth[u]<depth[v]) swap(u,v);
int dist = depth[u] - depth[v];//深度差
while(depth[u]!=depth[v]){//把较深的结点u倍增到与v高度相等
u = fa[u][Log[depth[u]-depth[v]]];
}
if(u == v) return u;//如果u倍增到v,说明v是u的LCA
for(int i = Log[depth[u]];i>=;i--){//否则两者同时向上倍增
if(fa[u][i]!=fa[v][i]){//如果向上倍增的祖先不同,说明是可以继续倍增
u = fa[u][i];//替换两个结点
v = fa[v][i];
}
}
return fa[u][];//最终结果为u v向上一层就是LCA
}
int main()
{
pre();
cin>>N;
for(int i = ;i<N;i++){
int u,v;
cin>>u>>v;
G[u].push_back(v),G[v].push_back(u);
}
dfs(,);
int q;cin>>q;
while(q--){
int x,y,a,b,k;
cin>>x>>y>>a>>b>>k;
int t1 = LCA(a,b),t2 = LCA(a,x),t3 = LCA(a,y),t4 = LCA(b,x),t5 = LCA(b,y);
int dis = abs(depth[t1]-depth[a])+abs(depth[t1]-depth[b]);
if(dis% == k% && dis <=k ){
// cout<<"De1"<<endl;
cout<<"YES"<<endl;
continue;
}
int dis1 = abs(depth[t2]-depth[a])+abs(depth[t2]-depth[x]);
int dis2 = abs(depth[t5]-depth[b])+abs(depth[t5]-depth[y]);
if( (dis1+dis2+)% == k% && dis1+dis2+ <= k){
// cout<<"de2"<<endl;
cout<<"YES"<<endl;
continue;
}
dis1 = abs(depth[t3]-depth[a])+abs(depth[t3]-depth[y]);
dis2 = abs(depth[t4]-depth[b])+abs(depth[t4]-depth[x]);
if((dis1+dis2+)% == k% && dis1+dis2+ <= k){
// cout<<"de3"<<endl;
cout<<"YES"<<endl;
continue;
}
cout<<"NO"<<endl;
}
return ;
}
Codeforces 1304E. 1-Trees and Queries 代码(LCA 树上两点距离判奇偶)的更多相关文章
- 模板倍增LCA 求树上两点距离 hdu2586
http://acm.hdu.edu.cn/showproblem.php?pid=2586 课上给的ppt里的模板是错的,wa了一下午orz.最近总是被坑啊... 题解:树上两点距离转化为到根的距离 ...
- cogs 2450. 距离 树链剖分求LCA最近公共祖先 快速求树上两点距离 详细讲解 带注释!
2450. 距离 ★★ 输入文件:distance.in 输出文件:distance.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 在一个村子里有N个房子,一 ...
- How far away ? LCA求树上两点距离
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Codeforces 832D: Misha, Grisha and Underground 【LCA模板】
题目链接 模板copy from http://codeforces.com/contest/832/submission/28835143 题意,给出一棵有n个结点的树,再给出其中的三个结点 s,t ...
- 洛谷P3379lca,HDU2586,洛谷P1967货车运输,倍增lca,树上倍增
倍增lca板子洛谷P3379 #include<cstdio> struct E { int to,next; }e[]; ],anc[][],log2n,deep[],n,m,s,ne; ...
- Codechef Dynamic Trees and Queries
Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...
- 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 ...
- UESTC 912 树上的距离 --LCA+RMQ+树状数组
1.易知,树上两点的距离dis[u][v] = D[u]+D[v]-2*D[lca(u,v)] (D为节点到根节点的距离) 2.某条边<u,v>权值一旦改变,将会影响所有以v为根的子树上的 ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 D.寻找-树上LCA(树上a到b的路径上离c最近的点)
链接:https://ac.nowcoder.com/acm/contest/558/D来源:牛客网 寻找 小猫在研究树. 小猫在研究树上的距离. 给定一棵N个点的树,每条边边权为1. Q次询问,每次 ...
随机推荐
- pinpoint安装(docker)
安装docker docker-compose yum update -y yum install docker epel-release python-pip -y pip install --up ...
- 网站windows可以访问mac和linux无法访问【MTU MSS问题】
环境: 阿里云LB 内网地址类型,代理后面的k8s上的服务 公司和阿里云之间vpn打通 在windows上进行访问一切正常,在相同的办公局域网linux主机内访问不通,mac笔记本访问同样不通,tel ...
- aliyun---经过LB到后端k8s压测超时的问题
环境:阿里云 压测主机:阿里云ECS(非LB后的主机) 压测目标:阿里云k8s自己的某个服务 k8s配置在kube-system 按照之前的ingress-nginx 配置了一个内网的ingress- ...
- [RHEL8]安装Docker Problem: package docker-ce-3:19.03.6-3.el7.x86_64 requires containerd.io
系统环境 # cat /etc/redhat-release Red Hat Enterprise Linux release 8.0 (Ootpa) 安装依赖 # yum install -y yu ...
- Vue子组件和根组件的关系
代码: <script type="text/javascript"> const Foo = Vue.extend({ template: `<div id=& ...
- 计算机网络 From Mr.Liu
引言 本博客摘自Mr.Liu,原帖请点击这里. 感谢Mr.Liu,这个文章很充分的描述了计算机网络的核心知识点. 我还在学习中,所以没有进行自己的转述.图片因为是copy代码而没有获得,想看更详尽的, ...
- 工作五年的.neter的一些经历感想和对未来的一些疑惑
本次疫情在家办公快一个月了,节省了上下班的时间,外出活动时间,感觉有好多时间可以利用.人一闲下来就容易想事情,很多事情想不通心里堵的厉害,做事都提不起兴趣.至于想些什么呢,我给大家摆一下. 我的经历 ...
- codewars--js--Large Factorials--阶乘+大数阶乘
问题描述: In mathematics, the factorial of integer n is written as n!. It is equal to the product of n a ...
- 关于存储最近N次数据的问题的实现
需求描述: 需要实现采集中,始终保持最近10次的数据 描述分析: 当采集第一次数据的存储的时候,开辟一个长度为11的list,和一个标记为来记录当前的采集轮询次数(记录1-10,第11次数值归1) 数 ...
- Android Intent用法总结
Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 ...