最近公共祖先。

如果$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的更多相关文章

  1. BNUOJ ->Borrow Classroom(LCA)

    B. Borrow Classroom Time Limit: 5000ms Memory Limit: 262144KB 每年的BNU校赛都会有两次赛前培训,为此就需要去借教室,由于SK同学忙于出题 ...

  2. 北京师范大学第十五届ACM决赛-重现赛 B Borrow Classroom (树 ——LCA )

    链接:https://ac.nowcoder.com/acm/contest/3/B 来源:牛客网 Borrow Classroom 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 2 ...

  3. BNUOJ 52325 Increasing or Decreasing 数位dp

    传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...

  4. bnuoj 24251 Counting Pair

    一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...

  5. bnuoj 44359 快来买肉松饼

    http://www.bnuoj.com/contest/problem_show.php?pid=44359 快来买肉松饼 Time Limit: 5000 ms     Case Time Lim ...

  6. BNUOJ 1006 Primary Arithmetic

    Primary Arithmetic 来源:BNUOJ 1006http://www.bnuoj.com/v3/problem_show.php?pid=1006 当你在小学学习算数的时候,老师会教你 ...

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

  8. Rust: move和borrow

    感觉Rust官方的学习文档里关于ownship,borrow和lifetime介绍的太简略了,无法真正理解这些语法设计的原因以及如何使用(特别是lifetime).所以找了一些相关的blog来看,总结 ...

  9. bnuoj 25659 A Famous City (单调栈)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659 #include <iostream> #include <stdio.h ...

随机推荐

  1. fastjson的@JSONField注解的一点问题

    @JSONField 看源码它可以作用于字段和方法上. 引用网上说的, 一.作用Field @JSONField作用在Field时,其name不仅定义了输入key的名称,同时也定义了输出的名称. 但是 ...

  2. springsecurity 表达式一览

    表达式 描述 hasRole([role]) 当前用户是否拥有指定角色. hasAnyRole([role1,role2]) 多个角色是一个以逗号进行分隔的字符串.如果当前用户拥有指定角色中的任意一个 ...

  3. 【CodeForces】576 C. Points on Plane

    [题目]C. Points on Plane [题意]给定坐标系中n个点的坐标(范围[0,10^6]),求一种 [ 连边形成链后总长度<=2.5*10^9 ] 的方案.n<=10^6. [ ...

  4. 51Nod - 1006 最长公共子序列Lcs模板

    给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的).   比如两个串为:   abcicba abdkscab   ab是两个串的子序列,abc也是,abca也是,其中abca是这 ...

  5. ES6基础知识汇总

    1.如何理解ECMAScript6? ECMAScript是什么,ECMASCript的作用 2.新增let关键字 let的用途 3.关键字const const作用,传址赋值 4.解构赋值 解构赋值 ...

  6. for 、forEach 、 forof、 forin遍历对比

    一.遍历内容的异同 1.for 和 for...in 是针对数组下标的遍历 2.forEach 及 for...of 遍历的是数组中的元素 二.对非数字下标的处理 由于array在js中也是对象中的一 ...

  7. 模板为webpack的目录结构

    目录结构 | -- build // 项目构建(webpack)相关代码 | |-- build.js // 生产环境构建代码 | |-- check-version.js // 检查node.npm ...

  8. oracle中的符号含义

    1.Oracle数据库存储过程中:=是什么意思?答:赋值的意思.举例:str := 'abcd';将字符串abcd赋值给变量str. 2.oracle 存储过程中的 := 和=有什么区别?答::= 是 ...

  9. [ python ] 字典的使用

    数据类型划分:    可变数据类型:list.dict.set    不可哈希    不可变数据类型:tuple.bool.int.str    可哈希 字典 python内置了字典类型,使用键-值( ...

  10. C#子线程中更新主线程UI-----注意项

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...