Codeforces Round #620 (Div. 2) E
LCA的倍增
模板:
int fath[maxn][], depth[maxn];
int dist[maxn],head[maxn];
void add(int u,int v,int dist0){
a[tot].next=head[u];
a[tot].dist=dist0;
a[tot].v=v;
head[u]=tot++;
}
void dfs(int u,int fa,int d) {
fath[u][]=fa; depth[u]=d;
for(int i=;i<;i++) fath[u][i]=fath[fath[u][i-]][i-];
for (int i=head[u];~i;i=a[i].next){
int v=a[i].v;if(v==fa)continue;
dist[v]=dist[u]+a[i].dist;
dfs(v,u,d+);
}
}
void init(int n){
for(int i=;i<=n;i++)fath[i][]=,dist[i]=,head[i]=-,depth[i]=;
tot=;
}
inline int lca(int x,int y){
if(depth[x]<depth[y])swap(x,y);
int h=depth[x]-depth[y];
for(it i=;h>;i++){
if(h&){
x=fath[x][i];
}
h>>=;
}
if(x==y)return x;
for(it i=;i>=;i--){
if(fath[x][i]!=fath[y][i]){
x=fath[x][i];
y=fath[y][i];
}
}
return fath[x][];
}
inline int dis(int u,int v){
int d=lca(u,v);
return dist[u]+dist[v]-*dist[d];
}
题意:
给一个n点的数,以1为根的树,
询问m个x,y,a,b,k
问x,y两点暂时联通,a点到b点能不能刚好满足过k条线
能输出yes,不能输出no
思路:
树上两点之间的距离用lca算出来,因为可以反复横跳,所以只要两点之间的距离小于等于k,然后与k的奇偶性一样就是yes
一共有三条路
dis(a,b)
dis(a,x)+dis(y,b)+1
dis(a,y)+dis(x,b)+1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define il inline
#define it register int
#define inf 0x3f3f3f3f
#define lowbit(x) (x)&(-x)
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 998244353
const int maxn=1e5+;
struct node{
int next,v;
}a[maxn<<];
int t,m,n,head[maxn],tot,x,y,a1,b,k,f,depth[maxn],fa[maxn][];
void add(int u,int v){
a[tot].next=head[u];
a[tot].v=v;head[u]=tot++;
}
void build(int uu){
queue<int>q;
depth[uu]=;
q.push();
while(!q.empty()){
int u=q.front();q.pop();
for(it i=head[u];~i;i=a[i].next){
int v=a[i].v;
if(depth[v]==-){
depth[v]=depth[u]+;q.push(v);
fa[v][]=u;
for(it j=;j<=;j++){
int arc=fa[v][j-];
fa[v][j]=fa[arc][j-];
}
}
}
}
}
int lca(int x,int y){
if(depth[x]<depth[y])swap(x,y);
for(it i=;i>=;i--){
if(depth[fa[x][i]]>=depth[y]){
x=fa[x][i];
}
}
if(x==y)return x;
for(it i=;i>=;i--){
if(fa[x][i]!=fa[y][i]){
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][];
}
int dis(int u,int v){
int d=lca(u,v);
return depth[u]+depth[v]-*depth[d];
}
int main(){
mem(head,-);mem(depth,-);tot=;
scanf("%d",&n);
for(it i=;i<n-;i++){
int u,v;
scanf("%d%d",&u,&v);add(u,v);add(v,u);
}
depth[]=;build();
scanf("%d",&m);
while(m--){
scanf("%d%d%d%d%d",&x,&y,&a1,&b,&k);
int kk=k;k%=;
int d1=dis(a1,b),d2=dis(a1,x)+dis(y,b)+,d3=dis(a1,y)+dis(x,b)+;
if((d1%==k && kk>=d1) || (d2%==k && kk>=d2)||(d3%==k && kk>=d3)){printf("YES\n");}
else{printf("NO\n");}
}
return ;
}
/*
5
1 2
2 3
3 4
4 5
5
1 3 1 2 2
1 4 1 3 2
1 4 1 3 3
4 2 3 3 9
5 2 3 3 9
YES
YES
NO
YES
NO
*/

Codeforces Round #620 (Div. 2) E的更多相关文章
- Codeforces Round #620 (Div. 2)
Codeforces Round #620 (Div. 2) A. Two Rabbits 题意 两只兔子相向而跳,一只一次跳距离a,另一只一次跳距离b,每次同时跳,问是否可能到同一位置 题解 每次跳 ...
- 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)E LCA
题:https://codeforces.com/contest/1304/problem/E 题意:给定一颗树,边权为1,m次询问,每次询问给定x,y,a,b,k,问能否在原树上添加x到y的边,a到 ...
- 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) 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求树上两点最短距离)
LCA求树上两点最短距离,如果a,b之间距离小于等于k并且奇偶性与k相同显然YES:或者可以从a先走到x再走到y再走到b,并且a,x之间距离加b,y之间距离+1小于等于k并且奇偶性与k相同也输出YES ...
- Codeforces Round #620 (Div. 2)D(LIS,构造)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ io ...
- Codeforces Round #620 (Div. 2) C. Air Conditioner
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to ma ...
随机推荐
- DeepLearningDTU: Building a RNN step by step
exercise 5: Week 5 - Recurrent Neural Networks Building your Recurrent Neural Network - Step by Step
- 【转载】Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
转自:http://www.linuxidc.com/Linux/2014-03/98978.htm Pig 一种操作hadoop的轻量级脚本语言,最初又雅虎公司推出,不过现在正在走下坡路了.当初雅虎 ...
- 【STM32H7教程】第60章 STM32H7的DAC应用之定时器触发实现DMA方式双通道波形
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第60章 STM32H7的DAC应用之定时器触发实 ...
- codeforce D. White Lines
二维前缀和 给你一个n*n的矩阵,里面有两种字符,‘W’和‘B’,代表black 和white .其实这个矩阵就是一个方形画板,你有一个k*k的橡皮只能用一次,使k*k的矩阵里的B变成W,问完全空白的 ...
- 每天进步一点点------Allegro中Autosilk top, Silkscreen top 和Assembly top三个什么区别
Autosilk top:最后出gerber的时候,自动生成的丝印层.会自动调整丝印位置,以及碰到阻焊开窗的地方,丝印会自动消失,避免露锡的地方涂上丝印(一般画丝印层的时候,焊盘上不会画上丝印,所以过 ...
- jenkins新建任务详解
一.新建任务 点击左侧新建任务 输入任务名称-->选择构建一个自由风格的软件项目-->确认 进入任务配置页面 二.基本配置 General 配置构建保留天数,保留个数,必要时并发构建 源码 ...
- Navicat Preminm for Linx
1. 准备工作 首先你要有能在Windows下安装破解Navicat Preminm的能力 去"官网"下载你所需要的"Navicat Preminm for Linx&q ...
- zlt项目实践
nacos gateWay fronted oath2 codeGenerate log-app monitor-app search-app
- CSS-透明背景色兼容
IE 不支持透明背景色 使用fileter div{ backgournd: #666; filter:alpha(opacity=50); -moz-opacity:0.5; opacity:0.5 ...
- shell脚本编程学习笔记(二)linux服务器启动流程
一.linux服务器启动流程 1.bios找到磁盘上的mbr主引导扇区 2.进入grub洁面选择相应的启动内核 3.读取kernel内核文件-/boot/vmlinuz-* 4.读取init的镜像文件 ...