BNUOJ 52509 Borrow Classroom
最近公共祖先。
如果$A$到$1$的时间小于$B$到$C$再到$1$的时间,那么一定可以拦截。
如果上述时间相等,需要在到达$1$之前,两者相遇才可以拦截。
#include<bits/stdc++.h>
using namespace std;
int T,n,Q,sz;
int dep[],f[];
int dp[][];
int h[],to[],nx[];
void add(int x,int y)
{
to[sz] = y;
nx[sz] = h[x];
h[x] = sz++;
}
int LCA(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
while()
{
if(dep[x]==dep[y]) break;
for(int j=;j>=;j--)
{
int p = dp[x][j];
if(p==-) continue;
if(dep[p]<dep[y]) continue;
x=p;
break;
}
}
if(x==y) return x;
while()
{
if(dp[x][]==dp[y][]) return dp[x][];
for(int j=;j>=;j--)
{
int p = dp[x][j];
int q = dp[y][j];
if(p==q) continue;
x = p;
y = q;
break;
}
}
}
int dis(int x,int y)
{
int t = LCA(x,y);
return dep[x]-dep[t] + dep[y]-dep[t];
}
void dfs(int x,int y,int d)
{
f[x]=; dp[x][] = y; dep[x] = d;
for(int i = h[x];i!=-;i=nx[i])
{
int p = to[i];
if(f[p]) continue;
dfs(p,x,d+);
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&Q);
for(int i=;i<=n;i++)
{
f[i]=;
h[i]=-;
}
sz=;
for(int i=;i<=n-;i++)
{
int x,y; scanf("%d%d",&x,&y);
add(x,y); add(y,x);
}
dfs(,-,);
for(int j=;j<=;j++)
for(int i=;i<=n;i++)
{
if(dp[i][j-]==-) dp[i][j]=-;
else dp[i][j] = dp[dp[i][j-]][j-];
}
for(int i=;i<=Q;i++)
{
int A,B,C; scanf("%d%d%d",&A,&B,&C);
int ans=;
int disAC = dis(A,C);
int disBC = dis(B,C);
int disA1 = dep[A]-dep[];
int disC1 = dep[C]-dep[];
if(disAC<=disBC) ans=;
if(disA1<disBC+disC1) ans=;
if(disA1==disBC+disC1&&LCA(A,C)!=) ans=;
if(ans==) printf("YES\n");
else printf("NO\n");
}
}
return ;
}
BNUOJ 52509 Borrow Classroom的更多相关文章
- BNUOJ ->Borrow Classroom(LCA)
B. Borrow Classroom Time Limit: 5000ms Memory Limit: 262144KB 每年的BNU校赛都会有两次赛前培训,为此就需要去借教室,由于SK同学忙于出题 ...
- 北京师范大学第十五届ACM决赛-重现赛 B Borrow Classroom (树 ——LCA )
链接:https://ac.nowcoder.com/acm/contest/3/B 来源:牛客网 Borrow Classroom 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 2 ...
- BNUOJ 52325 Increasing or Decreasing 数位dp
传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...
- bnuoj 24251 Counting Pair
一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...
- bnuoj 44359 快来买肉松饼
http://www.bnuoj.com/contest/problem_show.php?pid=44359 快来买肉松饼 Time Limit: 5000 ms Case Time Lim ...
- BNUOJ 1006 Primary Arithmetic
Primary Arithmetic 来源:BNUOJ 1006http://www.bnuoj.com/v3/problem_show.php?pid=1006 当你在小学学习算数的时候,老师会教你 ...
- bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...
- Rust: move和borrow
感觉Rust官方的学习文档里关于ownship,borrow和lifetime介绍的太简略了,无法真正理解这些语法设计的原因以及如何使用(特别是lifetime).所以找了一些相关的blog来看,总结 ...
- bnuoj 25659 A Famous City (单调栈)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659 #include <iostream> #include <stdio.h ...
随机推荐
- bootstrap 栅格calss
container container-fluid row col-xs- col-sm- col-md- col-lg- col-md-offset- col-md-push- col-md-pul ...
- --BEA官方网站(http: //www.bea.com)甲骨文已完成对该公司的收购BEA Weblogic Server 7.0x应用服务器简明安 装、配置手册 1
====================简 介: BEA公司是业内著名的中间件产商,以Tuxedo及Weblogic闻名于世,而其基础件平台(infrastructure)Weblogic platf ...
- 在Centos系统下使用命令安装gnome图形界面程序
第一步:先检查yum 是否安装了,以及网络是否有网络.如果这两者都没有,先解决网络,在解决yum的安装. 第二步:在命令行下 输入下面的命令来安装Gnome包. # yum groupinstall ...
- linux系统df和du命令的区别
发现一台用户的电脑,df检查出来的/磁盘空间占用了16G,比用du查看得到的磁盘空间大的多,du查看/下所有程序目录加起来还不到5G.这是什么原因呢? 即便是有隐藏文件,查了也很小啊. 因为df和 ...
- 【CodeForces】947 D. Picking Strings
[题目]D. Picking Strings [题意]给定只含'A','B','C'的字符串,支持以下变换:1.A - BC 2.B - AC 3.C - AB 4.AAA - empty ...
- 【CodeForces】913 E. Logical Expression
[题目]E. Logical Expression [题意]令x=11110000(2),y=11001100(2),z=10101010(2),n次询问,每次要求用[与][或][非][括号]构成含至 ...
- win10本地搭建php运行环境
一.下载搭建环境所需软件,安装顺序也要按照列表顺序安装 1.Vc2015(根据需要安装Vc2012或者Vc2015) Vc2015:https://www.microsoft.com/zh-CN/do ...
- 用create-react-app来快速配置react
最近在学react,然后感觉自己之前用的express+gulp+webpack+ejs的工作环境还是基于html+js+css这种三层架构的应用,完全跟react不是一回事. 愚蠢的我居然在原先的这 ...
- spring3-spring的事务管理机制
1. Spring的事务管理机制 Spring事务管理高层抽象主要包括3个接口,Spring的事务主要是由他们共同完成的: PlatformTransactionManager:事务管理器—主要用于平 ...
- 摘: 给Shapre命名
有两种解决方式: 在 VBA 中将slide中的Shape命名,改变shape.name即可. 另外一种方式就是有点投机取巧:你可以点击shap,右键选择web/alternativetext做些标记 ...