Codeforces Round #620 (Div. 2)E LCA
题: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的更多相关文章
- Codeforces Round #620 (Div. 2)
Codeforces Round #620 (Div. 2) A. Two Rabbits 题意 两只兔子相向而跳,一只一次跳距离a,另一只一次跳距离b,每次同时跳,问是否可能到同一位置 题解 每次跳 ...
- 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) 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的倍增 模板: ], depth[maxn]; int dist[maxn],head[maxn]; void add(int u,int v,int dist0){ a[tot].next=h ...
- 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) 题解
A. Two Rabbits 思路: 很明显,如果(y-x)%(a+b)==0的话ans=(y-x)/(a+b),否则就为-1 #include<iostream> #include< ...
- 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)D(LIS,构造)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ io ...
随机推荐
- Most simple basic of internet programming based on two different machines sharing the same local net
This blog is just shown for the most simple basic of internet programming based on two different mac ...
- POJ 2829 Buy Tickets
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 26443 Accepted: 12639 Des ...
- 安装npm install时,长时间停留在fetchMetadata: sill 解决方法——换npm的源
安装npm install时,长时间停留在fetchMetadata: sill mapToRegistry uri http://registry.npmjs.org/whatwg-fetch处, ...
- 2 —— js语法 —— 对象和方法的声明 。变量提升。闭包
一,声明对象 var obj1 = {}; var obj2 = {name:'kk',age:18,fun:function{ // name,age,fun为对象的属性,只是属性 ...
- Python MySQL 删除表
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- RDD转为Dataset如何指定schema?
与RDD进行互操作 Spark SQL支持两种不同方法将现有RDD转换为Datasets.第一种方法使用反射来推断包含特定类型对象的RDD的schema.这种基于反射的方法会导致更简洁的代码,并且在编 ...
- Linux基础命令-02
Linux基础命令-02:
- python中MD5使用练习
import platformpv = platform.python_version()print (pv) import hashlib deomo_val = 'a4ec18'' md5_jm ...
- windows driver 枚举串口
//枚举串口 NTSTATUS status; HANDLE hKey = NULL; OBJECT_ATTRIBUTES oa; UNICODE_STRING strPath = RTL_CONST ...
- idea的简单使用
1.新建项目 (1)点击 file -->new project ,选择java(创建普通java项目) ,点next (2)不选择create project from template (不 ...