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 ...
随机推荐
- [BZOJ4310] 跳蚤 - 后缀数组,二分,ST表
[BZOJ4310] 跳蚤 Description 首先,他会把串分成不超过 \(k\) 个子串,然后对于每个子串 \(S\) ,他会从 \(S\) 的所有子串中选择字典序最大的那一个,并在选出来的 ...
- Error: Unexpected HTTP status 413 'Request Entity Too Large' on
由于nginx的client_max_body_size设置过小,默认上传的文件小于所要上传的文件大小,把这个值调大就可以了,我这里在配置文件的server下更改如下: server { client ...
- word doc转pdf
from win32com.client import constants, gencache # TODO pip install pywin32 -i http://mirrors.aliyun. ...
- OpenCV函数 重映射
重映射是什么意思? 把一个图像中一个位置的像素放置到另一个图片指定位置的过程. 为了完成映射过程, 有必要获得一些插值为非整数像素坐标,因为源图像与目标图像的像素坐标不是一一对应的. 我们通过重映射来 ...
- [ASP.NET]Web网站与Web应用程序区别
[ASP.NET]Web网站与Web应用程序区别 本文链接:https://blog.csdn.net/a954553391/article/details/86403521 前言:在项目开发中, ...
- 【转】git rebase详解
git合并代码方式主要有两种方式,分别为:1.merge处理,这是大家比较能理解的方式.2.rebase处理,中文此处翻译为衍合过程. git rebase操作讲解例子: cd /usr/local/ ...
- 使用QtCreator运行Gstreamer程序
1.创建一个简单的Plain C Application 2.Build System选择qmake 3.Kit Selection默认Desktop Qt x.xx.x GCC 64bit 4..p ...
- Redis非关系型缓存数据库集群部署、参数、命令工具
<关系型数据库与非关系型数据库> 关系数据库:mysql.oracle.DB2.SQL Server非关系数据库:Redis(缓存数据库).MongodDB(处理海量数据).Memcach ...
- poj 3281Dining(网络流 拆点)
题目链接:http://poj.org/problem?id=3281 题目大意:John养了N只奶牛,他为奶牛准备了F个食物和D个饮料,但是每只奶牛只对其中的一些饮料和食物感兴趣,现在请制定一些方案 ...
- GitHub项目简介
为了存放代码新建了一个GitHub账号,存放了一些比较常用的代码块,上面的模块大部分都能找到 index.html 文件直接在浏览器打开. 地址:https://github.com/liuzhou1 ...