最近公共祖先。

如果$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. LeetCode-Reverse Words in a String[AC源码]

    package com.lw.leet1; import java.util.Stack; /** * @ClassName:Solution * @Description: * Reverse Wo ...

  2. [LeetCode] 30. Substring with Concatenation of All Words ☆☆☆

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  3. 2017北京国庆刷题Day3 morning

    期望得分:100+60+0=160 实际得分:100+30+0=130 考场上用的哈希 #include<cstdio> #include<cstring> #include& ...

  4. Qt undefined reference to ***

    错因:某个类声明了一个函数但是没有定义就直接使用.

  5. 【BZOJ】1040: [ZJOI2008]骑士 环套树DP

    [题意]给定n个人的ai和bi,表示第i个人能力值为ai且不能和bi同时选择,求能力值和最大的选择方案.n<=10^6. [算法]环套树DP(基环树) [题解]n个点n条边——基环森林(若干环套 ...

  6. 【VIJOS】P1512 SuperBrother打鼹鼠

    [算法]二维树状数组 [题解] 1.树状数组尽量不要出现0,因此所有坐标+1 2.面积求法(默认1开始):(x1,y1)(x2,y2)=sum(x2,y2)-sum(x1-1,y2)-sum(x2,y ...

  7. uboot之---make smdk2410_config命令详细解析

    先进入顶层Makefile.有很多相对不同板子的配置,如: gec2440_config:unconfig @$(MKCONFIG) $(@:_config=) arm arm920t gec2440 ...

  8. 多维尺度变换MDS(Multidimensional Scaling)

    流形学习(Manifold Learning)是机器学习中一大类算法的统称,流形学习是非线性的降维方法(an approach to non-linear dimensionality reducti ...

  9. 生成器(generator)和迭代(iterable , iterator, iteration)

    在搞清楚Generator之前,我们先讨论一下 iterable , iterator, iteration 1.Iterable 我们知道,在Python中所有东西都是object, 比如说变量,容 ...

  10. MYSQL三种安装方式--rpm包安装

    1. 首先检查机器里是否已经存在MySQL $ rpm -qa | grep mysql 2. 去官网下载相应的rpm包:https://dev.mysql.com/downloads/mysql/ ...