Codeforces Round #620 (Div. 2)E LCA
题:https://codeforces.com/contest/1304/problem/E
题意:给定一颗树,边权为1,m次询问,每次询问给定x,y,a,b,k,问能否在原树上添加x到y的边,a到b的路径长度等于k,注意这里的点和边都是可以重复走的;
分析:注意到点边可以重复走,我们就可以推出一个条件就是a、b之间的长度len要满足>=k;
其次当路长大于k时,我们就要判断len和k是否同奇偶,因为要到达长度len要被分为:len=k+2*i(i>=0),否则无法构造出这么一条路径
然后添加x-y造成的路径选择,有分为最简单的3种
1、直接走a,b;
2、走a-x-y-b;
3、走a-y-x-b;
然后树上路径就用倍增lca模板即可求俩点间距离
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
#define pb push_back
const int M=2e5+;
const int N=;
struct node{
int v,w;
node(int vv=,int ww=):v(vv),w(ww){}
};
vector<node>e[M];
int s,grand[M][N],dis[M][N],deep[M],root,n;
void dfs(int u){
for(int i=;i<=s;i++){
grand[u][i]=grand[grand[u][i-]][i-];
dis[u][i]=dis[u][i-]+dis[grand[u][i-]][i-];
if(!grand[u][i])
break;
}
for(int i=;i<e[u].size();i++){
int v=e[u][i].v;
if(v!=grand[u][]){
grand[v][]=u;
deep[v]=deep[u]+;
dis[v][]=e[u][i].w;
dfs(v);
} // cout<<"!!"<<endl;
}
}
void init(){
s=floor(log(1.0*n)/log(2.0));
deep[]=-;
dfs(root);
}
int LCA(int a,int b){
if(deep[a]>deep[b])
swap(a,b);
int ans=;
for(int i=s;i>=;i--){
if(deep[b]>deep[a]&&deep[a]<=deep[grand[b][i]])
ans+=dis[b][i],b=grand[b][i];
}
for(int i=s;i>=;i--)
if(grand[a][i]!=grand[b][i])
ans+=dis[a][i]+dis[b][i],b=grand[b][i],a=grand[a][i];
if(a!=b)
ans+=dis[a][]+dis[b][];
return ans;
}
bool check(int a,int b){
return b>=a&&(a%)==(b%);
}
int main(){ scanf("%d",&n);
for(int v,u,i=;i<n;i++){
scanf("%d%d",&u,&v);
e[u].pb(node(v,));
e[v].pb(node(u,));
}
root=;
init();
int m;
scanf("%d",&m);
while(m--){
int x,y,a,b,k;
scanf("%d%d%d%d%d",&x,&y,&a,&b,&k);
/// cout<<LCA(a,b)<<endl;
if(check(LCA(a,b),k)||check(LCA(a,x)+1+LCA(y,b),k)||check(LCA(a,y)+1+LCA(x,b),k))
puts("YES");
else
puts("NO");
}
return ;
}
Codeforces Round #620 (Div. 2)E LCA的更多相关文章
- Codeforces Round #620 (Div. 2)
Codeforces Round #620 (Div. 2) A. Two Rabbits 题意 两只兔子相向而跳,一只一次跳距离a,另一只一次跳距离b,每次同时跳,问是否可能到同一位置 题解 每次跳 ...
- 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 ...
- 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) E
LCA的倍增 模板: ], depth[maxn]; int dist[maxn],head[maxn]; void add(int u,int v,int dist0){ a[tot].next=h ...
- 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) 题解
A. Two Rabbits 思路: 很明显,如果(y-x)%(a+b)==0的话ans=(y-x)/(a+b),否则就为-1 #include<iostream> #include< ...
- Codeforces Round #620 (Div. 2)D dilworld定理
题:https://codeforces.com/contest/1304/problem/D 题意:给定长度为n-1的只含’>'和‘<’的字符串,让你构造出俩个排列,俩个排列相邻的数字之 ...
- Codeforces Round #620 (Div. 2) D
构造一个排列,要求相邻之间的数满足给定的大小关系,然后构造出两个序列,一个序列是所有可能的序列中LIS最长的,一个所有可能的序列中LIS最短的 最短的构造方法:我们考虑所有单调递增的部分,可以发现要让 ...
- Codeforces Round #620 (Div. 2)D(LIS,构造)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ io ...
随机推荐
- css中background合写样式
body { background: url("img_tree.png") no-repeat fixed 50% 50%/cover #ffffff ; } 等价于 body ...
- (四)Flex 布局教程和例子
布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. 1.flex-direction ...
- P1083 是否存在相等的差
P1083 是否存在相等的差 转跳点:
- REST接口
全名是Representational State Transfer REST是设计风格而不是标准 建议将JSON格式作为标准响应格式 -------------------------------- ...
- BZOJ 2744
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #incl ...
- Gauss列主消元
问题:1.列主消元为什么精度高? 2.fabs函数精确度 #include<iostream> #include<cstdio> #include<cstring> ...
- 实验吧-隐写术-黑与白(二)(反转+五笔+Image steganography)
反转有二:颜色反转.文件名反转 文件名这么乱,毫无规律,好奇怪,进行反转后发现是:steganography(就是隐写术的意思),这还是个图片文件,有一款工具正好叫Image steganograph ...
- Mapper method 'com.xxxx.other.dao.MyDao.getNo attempted to return null from a method with a primitive return type (int)
使用myBatis调用存储过程的返回值,提示错误信息: org.apache.ibatis.binding.BindingException: Mapper method 'com.xxxx.othe ...
- 15 ~ express ~ 用户数据分页原理和实现
一,在后台路由 /router/admin.js 中 1,限制获取的数据条数 : User.find().limit(Number) 2,忽略数据的前(Number)条数据 : skip(Number ...
- 在Mac上使用docker+sql server+Navicat
1. 版本: 2. 安装Kubernetes(并不知道安装这个有什么用) git clone https://github.com/maguowei/k8s-docker-desktop-for-m ...