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 ...
随机推荐
- C语言备忘录——取余与取模
前几天,一个小姐姐问我取余和取模有什么区别,我当时第一反应就是二者是一样的,但是小姐姐咬死说不一样.我去百度了一下还真的不一样.脑壳疼,我当初误导了多少人.所以为了帮助我记忆也为了帮助预防我误人子弟 ...
- POJ 3614:Sunscreen 贪心+优先队列
Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5378 Accepted: 1864 Descrip ...
- 配置tomcat、nginx实现反向代理(需操作)
配置tomcat.nginx实现反向代理现在我想通过nginx访问tomcat 这就要我们去修改nginx的核心配置文件,在其目录下的conf文件夹下的nginx.conf文件,那么首先我们就要了解该 ...
- cf754 754D - Fedor and coupons
2个多小时,弱智了..(连A都做不对,就不要做D了(迷)) #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL ...
- Vulkan 之 Synchronization
1.2之前定的版本采用 vkSemaphore和vkFence来进行同步, VkSemaphore allowed applications to synchronize operations acr ...
- servlet中调用注入spring管理的dao(转)
今天做大型仪器的的时候遇到的问题,转下为了以后能用 http://blog.csdn.net/jiyingying_up/article/details/44803585 我们用spring的依赖注入 ...
- StackExchange.Redis.DLL 操作redis加强版
直接引用StackExchange.Redis.dll这一个dll来操作redis App.config配置 <?xml version="1.0" encoding=&qu ...
- ajax异步提交 有时会出现无bug的数据处理异常-----debug没有问题,正常运行却数据处理不正确,极少机会会出现正常的处理结果
ajax 被使用时,常默认的就使用了异步处理. 当遇到后面的代码对同样的数据进行处理 或 要依赖前面ajax处理的结果时,就会导致数据处理结果不正确,未达到预期值. 且,debug时却能正常完成功能 ...
- VIJOS-P1282 佳佳的魔法照片 排序
Description 一共有n(n≤20000)个人(以1--n编号)向佳佳要照片,而佳佳只能把照片给其中的k个人.佳佳按照与他们的关系好坏的程度给每个人赋予了一个初始权值W[i].然后将初始权值从 ...
- javaweb 最简单的分页技术
原文来自于https://www.cnblogs.com/xwlych/p/6017833.html 个人由加了一点注释,他的代码我运行不起来,弄了好一会 bean包 User.java packa ...