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 ...
随机推荐
- [ZJOI2014] 力 - 多项式乘法 FFT
题意:给定 \({q_i}\),求 \[E_i = \sum_{i<j}{\frac{q_j}{(j-i)^2}} - \sum_{i>j}{\frac{q_j}{(j-i)^2}}\] ...
- C++——简单程序设计
1.一个简单的程序 #include <iostream> //iostream是头文件,用来说明要使用的对象的相关信息. using namespace std; //使用命名空间,解决 ...
- 较完整checkstyle.xml及说明
具体项的具体说明请参考:https://www.cnblogs.com/ziyuyuyu/p/9870717.html 梳理此次完整checkstyle.xml说明,以备以后再此基础上删减 <? ...
- 配置数据库属性validationQuery
配置数据库时,属性validationQuery默认值为“select 1”,对于oracle值应为“select 1 from dual” validationQuery属性:用来验证数据库连接的语 ...
- ASP.NET + MVC5 入门完整教程八 -—-- 一个完整的应用程序(下)
https://blog.csdn.net/qq_21419015/article/details/80802931 SportsStore 1.导航 添加导航控件 如果客户能够通过产品列表进行分类导 ...
- Spark 中 GroupByKey 相对于 combineByKey, reduceByKey, foldByKey 的优缺点
避免使用GroupByKey 我们看一下两种计算word counts 的方法,一个使用reduceByKey,另一个使用 groupByKey: val words = Array("on ...
- java_获取指定ip的定位
因为自己网站后台做了一个进站ip统计,之前只是获取了ip,这次优化了下,把ip的大致区域弄出来了 废话不多说,进正题 首先要用到几个网络大头的api 淘宝API:http://ip.taobao.co ...
- 1167E - Range Deleting 双指针
题意:给出n个数的序列,并给出x,这n个数的范围为[1,x],f(L,R)表示删除序列中取值为[l,r]的数,问有几对L,R使得操作后的序列为非递减序列 思路:若[l,r]成立,那么[l,r+1],. ...
- 励志成为优产的母猪--------猜数游戏 ,历史记录,pickle保存,队列deque
# pickle 可以处理复杂的序列化语法.(例如自定义的类的方法,游戏的存档等),存档以文件的形式保存 参见 https://www.cnblogs.com/abobo/p/8080447.html ...
- AS报错:Class kotlin.reflect.jvm.internal.FunctionCaller$FieldSetter can not access a member of class com.android.build.gradle.tasks.ManifestProcessorTask with modifiers "private"
删除所有.gradle文件夹 失效缓存/重新启动