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的更多相关文章

  1. Codeforces Round #620 (Div. 2)

    Codeforces Round #620 (Div. 2) A. Two Rabbits 题意 两只兔子相向而跳,一只一次跳距离a,另一只一次跳距离b,每次同时跳,问是否可能到同一位置 题解 每次跳 ...

  2. 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 ...

  3. Codeforces Round #620 (Div. 2)E LCA

    题:https://codeforces.com/contest/1304/problem/E 题意:给定一颗树,边权为1,m次询问,每次询问给定x,y,a,b,k,问能否在原树上添加x到y的边,a到 ...

  4. Codeforces Round #620 (Div. 2)D dilworld定理

    题:https://codeforces.com/contest/1304/problem/D 题意:给定长度为n-1的只含’>'和‘<’的字符串,让你构造出俩个排列,俩个排列相邻的数字之 ...

  5. Codeforces Round #620 (Div. 2) D

    构造一个排列,要求相邻之间的数满足给定的大小关系,然后构造出两个序列,一个序列是所有可能的序列中LIS最长的,一个所有可能的序列中LIS最短的 最短的构造方法:我们考虑所有单调递增的部分,可以发现要让 ...

  6. Codeforces Round #620 (Div. 2) A-F代码 (暂无记录题解)

    A. Two Rabbits (手速题) #include<bits/stdc++.h> using namespace std; typedef long long ll; int ma ...

  7. 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 ...

  8. Codeforces Round #620 (Div. 2)D(LIS,构造)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ io ...

  9. 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 ...

随机推荐

  1. Working copy not locked; this is probably a bug, please report

    问题描述 svn: Unable to lock 'G:\chengXu\2017_Year\JFW\Easy7\src' 解决方案

  2. (转)git学习教程

    转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

  3. JS定义类的六种方式详解

    转载自: http://www.jb51.net/article/84089.htm 在前端开发中,经常需要定义JS类.那么在JavaScript中,定义类的方式有几种,分别是什么呢?本文就JS定义类 ...

  4. 字符串UTF-8和GBK之间的转换以及判定

    一.判定字符串是否是UTF-8的编码 bool is_str_utf8(const char* str) { unsigned ;//UFT8可用1-6个字节编码,ASCII用一个字节 unsigne ...

  5. codeforces 1284C. New Year and Permutation(组合数学)

    链接:https://codeforces.com/problemset/problem/1284/C 题意:定义一个framed segment,在区间[l,r]中,max值-min值 = r - ...

  6. C#中ESRI.ArcGIS.esriSystem的引用问题

    ESRI.ArcGIS.esriSystem,在引用里没有它的同名引用,其实它对应的引用为ESRI.ArcGIS.System,所以添加“ESRI.ArcGIS.System”这个引用即可

  7. springboot13(redis缓存)

    redis做springboot2.x的缓存 1.首先是引入依赖 <dependency> <groupId>org.springframework.boot</grou ...

  8. javascript当中Function用法

    4)Function用法 例 3.4.1 <head>    <meta http-equiv="content-type" content="text ...

  9. Auto.js的初次使用——在VSCode中使用

    最近双十一大家都在集猫币,盖楼,但是每天刷任务太浪费时间了.被推荐了一个脚本可以自动刷任务,很是好奇.于是想要了解一下Auto.js 一.vscode启动Auto.js 1.vscode里安装auto ...

  10. MYSQL之路练习题

    题目换成求平均薪水的等级最高的部门的部门名称