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 ...
随机推荐
- NOR Flash驱动
驱动程序 1 ] ] );81 ;83 }84 85 86 static void __exit nor_exit(void)87 {88 iounmap(nor_ ...
- C# Winform使用线程,委托定时更新界面UI控件,解决界面卡顿问题(转载)
一.定时执行主界面控件值 1.利用定时器 Thread t = null; private void InitTSJK() { t = new Thread(new ThreadStart(GetDa ...
- RadioGroup的使用
前言: 使用RadioGroup就可以在选择情况多的时候,简化代码 RadioGroup 使用互斥选择时,会使用RadioGroup标签下面RadioButton,如下面的代码:(这样写下来,男和女的 ...
- UVA - 11354 Bond(最小生成树+LCA+瓶颈路)
题意:N个点,M条路,每条路的危险度为路上各段中最大的危险度.多组询问,点s到点t的所有路径中最小的危险度. 分析: 1.首先建个最小生成树,则s到t的路径一定是危险度最小的. 原因:建最小生成树的最 ...
- 配置tomcat、nginx实现反向代理(需操作)
配置tomcat.nginx实现反向代理现在我想通过nginx访问tomcat 这就要我们去修改nginx的核心配置文件,在其目录下的conf文件夹下的nginx.conf文件,那么首先我们就要了解该 ...
- 【LeetCode】160. 相交链表
题目 输入两个链表,找出它们的第一个公共节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB ...
- Web基础之Servlet
Servlet Servlet : server applet,直译服务小程序.那Servlet到底是什么呢? Servlet说白了其实就是一个接口,接口的作用是什么?规范呗,这个接口规定了下面三个问 ...
- POJ 1068:Parencodings
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22849 Accepted: 13394 De ...
- DuplicateHandle伪句柄与实句柄的应用二
//扫描进程列表,获得进程名为 ConsoleApplication2.exe的进程句柄B,把当前进程A的伪 //句柄传递给B,在B进程中关闭它 #include "stdafx.h&quo ...
- 20 ~ express ~ 前台内容分页展示
一,后台路由文件 /router/main.js var express = require('express') var router = express.Router() var Category ...