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 ...
随机推荐
- linq和初始化器
对象初始化器 在没有初始化器之前,我们初始化一个对象可能需要在构造函数内对属性赋值,或者先实例化一个对象,在一个一个的对其属性赋值. 使用初始化器: 已知类型:var query = new Pers ...
- Netcat工具
一般Netcat有两个版本,一个版本是不提供反向连接的版本,一个是全功能版本.这两者的区别就是是否带-e参数,只有带-e参数的版本才支持反向连接. 参数说明 -c shell commands she ...
- eclipse运用经验
1.eclipse粘贴字符串添加转义符 2.eclipse的jdk版本切换 1.Window—Preferences—Java—Compiler—右侧面板设置为1.6 2.Window—Prefere ...
- php 法大大对接
通过编写扩展实现, 实现两种方式签署 1, 存证签署 存证签署流程 /** *个人 *客户在平台注册(姓名,身份证,手机号)---->三要素验证----->获取客户编号-------> ...
- 第三十九篇 入门机器学习——Numpy.array的基础操作——合并与分割向量和矩阵
No.1. 初始化状态 No.2. 合并多个向量为一个向量 No.3. 合并多个矩阵为一个矩阵 No.4. 借助vstack和hstack实现矩阵与向量的快速合并.或多个矩阵快速合并 No.5. 分割 ...
- Razor视图中的@:和语法
Razor视图中的@:和语法 原创changuncle 最后发布于2016-12-07 17:43:50 阅读数 4456 收藏 展开 在MVC项目中新建视图的时候默认支持ASPX引擎和Razor引 ...
- 题解【洛谷P3884】[JLOI2009]二叉树问题
题面 题解 这道题目可以用很多方法解决,这里我使用的是树链剖分. 关于树链剖分,可以看一下我的树链剖分学习笔记. 大致思路是这样的: 第\(1\)次\(dfs\)记录出每个点的父亲.重儿子.深度.子树 ...
- nodejs npm安装教程
一.使用之前,我们先来掌握3个东西是用来干什么的. npm: Nodejs下的包管理器. webpack: 它主要的用途是通过CommonJS的语法把所有浏览器端需要发布的静态资源做相应的准备,比如资 ...
- python 字符串是否包含某个子字符串
方法如下:以后再整理 if str1 in str2: 包含的话,True if str1.find(str2)>=0: 包含的话,返回第一次出现的位置,没有的话为负数 https://www. ...
- [MongoDB]MongoDB分页显示
MongoDB Limit与Skip方法配合进行分页MongoDB Limit() 方法如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接 ...