题:https://codeforces.com/contest/1304/problem/E

题意:给定一颗树,边权为1,m次询问,每次询问给定x,y,a,b,k,问能否在原树上添加x到y的边,a到b的路径长度等于k,注意这里的点和边都是可以重复走的;

分析:注意到点边可以重复走,我们就可以推出一个条件就是a、b之间的长度len要满足>=k;

   其次当路长大于k时,我们就要判断len和k是否同奇偶,因为要到达长度len要被分为:len=k+2*i(i>=0),否则无法构造出这么一条路径

   然后添加x-y造成的路径选择,有分为最简单的3种

   1、直接走a,b;

   2、走a-x-y-b;

   3、走a-y-x-b;

   然后树上路径就用倍增lca模板即可求俩点间距离

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
#define pb push_back
const int M=2e5+;
const int N=;
struct node{
int v,w;
node(int vv=,int ww=):v(vv),w(ww){}
};
vector<node>e[M];
int s,grand[M][N],dis[M][N],deep[M],root,n;
void dfs(int u){
for(int i=;i<=s;i++){
grand[u][i]=grand[grand[u][i-]][i-];
dis[u][i]=dis[u][i-]+dis[grand[u][i-]][i-];
if(!grand[u][i])
break;
}
for(int i=;i<e[u].size();i++){
int v=e[u][i].v;
if(v!=grand[u][]){
grand[v][]=u;
deep[v]=deep[u]+;
dis[v][]=e[u][i].w;
dfs(v);
} // cout<<"!!"<<endl;
}
}
void init(){
s=floor(log(1.0*n)/log(2.0));
deep[]=-;
dfs(root);
}
int LCA(int a,int b){
if(deep[a]>deep[b])
swap(a,b);
int ans=;
for(int i=s;i>=;i--){
if(deep[b]>deep[a]&&deep[a]<=deep[grand[b][i]])
ans+=dis[b][i],b=grand[b][i];
}
for(int i=s;i>=;i--)
if(grand[a][i]!=grand[b][i])
ans+=dis[a][i]+dis[b][i],b=grand[b][i],a=grand[a][i];
if(a!=b)
ans+=dis[a][]+dis[b][];
return ans;
}
bool check(int a,int b){
return b>=a&&(a%)==(b%);
}
int main(){ scanf("%d",&n);
for(int v,u,i=;i<n;i++){
scanf("%d%d",&u,&v);
e[u].pb(node(v,));
e[v].pb(node(u,));
}
root=;
init();
int m;
scanf("%d",&m);
while(m--){
int x,y,a,b,k;
scanf("%d%d%d%d%d",&x,&y,&a,&b,&k);
/// cout<<LCA(a,b)<<endl;
if(check(LCA(a,b),k)||check(LCA(a,x)+1+LCA(y,b),k)||check(LCA(a,y)+1+LCA(x,b),k))
puts("YES");
else
puts("NO");
}
return ;
}

Codeforces Round #620 (Div. 2)E LCA的更多相关文章

  1. Codeforces Round #620 (Div. 2)

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

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

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

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

  4. Codeforces Round #620 (Div. 2) E

    LCA的倍增 模板: ], depth[maxn]; int dist[maxn],head[maxn]; void add(int u,int v,int dist0){ a[tot].next=h ...

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

  6. Codeforces Round #620 (Div. 2) 题解

    A. Two Rabbits 思路: 很明显,如果(y-x)%(a+b)==0的话ans=(y-x)/(a+b),否则就为-1 #include<iostream> #include< ...

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

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

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

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

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

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

随机推荐

  1. 人脸识别 API Key和Secret Key作用

    App key简称API接口验证序号,是用于验证API接入合法性的.接入哪个网站的API接口,就需要这个网站允许才能够接入,如果简单比喻的话:可以理解成是登陆网站的用户名 App Secret简称AP ...

  2. window下Apache Jmeter基础用法

    1.下载Apache-jmeter-5.1.1.zip 2.解压到一个地方,就可以开始使用了,如果需要在CMD里快速打开,可以设置环境变量. 3.在bin里面,直接打开jmeter.bat. 可以修改 ...

  3. sql 报错问题

    SQLServer数据库密码已过期问题 处理

  4. RadioButton之互斥选择和Toast显示

    前言: RadioButton用来单选并且用Toast来进行提示所选内容 RadioButton标签单独写的时候不能出现互斥现象,代码如下 <RadioButton android:layout ...

  5. UVA - 548 Tree(二叉树的递归遍历)

    题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...

  6. pr cs6安装教程

    这是通过我自己实践操作,网上查询整理的安装流程: 安装 1.下载:http://www.smzy.com/smzy/smzy93225.html 2.断网,安装 如果到2%显示安装失败,在这里有详细解 ...

  7. Windows Server 2008 R2 ntoskrnl.exe 引起蓝屏故障,重新启动

    前不久在HP ProLiant DL360 G6的服务器上面安装了Windows Server 2008 R2,系统一到晚上凌晨就出现蓝屏.重启现象,并且在 C:\Windows\Minidump 目 ...

  8. ssh服务启动失败 /var/empty must be owned by root and not group or world-writable.

    输入 /etc/rc.d/init.d/sshd start 启动sshd服务,报如下错误: /var/empty must be owned by root and not group or wor ...

  9. 使用pip install jupyter报错处理办法及修改Jupyter默认加载路径的方法

    1.配置python环境之后想使用Jupyter,网上查看可以使用pip install Jupyter安装,执行命令行后正常安装,安装到一半以后报错,如图1.2 图1 图2 2.发现是安装过程中安装 ...

  10. 箭头函数arrow funtion

    1.定义一个匿名函数常规语法: function (x) { return x * x; } 2.该函数使用箭头函数可以使用仅仅一行代码搞定! x => x * x 箭头函数相当于匿名函数,并且 ...