最近公共祖先。

如果$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. react UI组件库 Salt UI

    https://salt-ui.github.io/?spm=a219a.7629140.0.0.JWztQO

  2. Mahout源码目录说明&&算法集

    Mahout源码目录说明 mahout项目是由多个子项目组成的,各子项目分别位于源码的不同目录下,下面对mahout的组成进行介绍: 1.mahout-core:核心程序模块,位于/core目录下: ...

  3. MongoDB常用方法

    一.查询 find方法 db.collection_name.find(); 查询所有的结果: select * from users; db.users.find(); 指定返回那些列(键): se ...

  4. MySQl学习-——Mysql体系结构与Mysql存储引擎

    Mysql体系结构与Mysql存储引擎 Mysql体系结构 mysql体系结构图:

  5. Web 开发者不可不知的15条编码原则

    HTML 已经走过了近20的发展历程.从HTML4到XHTML,再到最近十分火热的HTML5,它几乎见证了整个互联网的发展.但是,即便到现在,有很多基础的概念和原则依然需要开发者高度注意.下面,向大家 ...

  6. 【BZOJ】3998: [TJOI2015]弦论

    [题意]给定长度为n的小写字母字符串S,求第k小子串.n<=5*10^5. 给定T,T=0时不同位置的相同子串算一个,T=1时算多个. [算法]后缀自动机 [题解]对S建立SAM,T=0则每个节 ...

  7. 【BZOJ】3238: [Ahoi2013]差异

    [题意]给定长度为n的小写字母字符串,令Ti表示以i开头的后缀,求Σ[Ti+Tj-2*lcp(Ti,Tj)],1<=i<j<=n. [算法]后缀自动机 [题解]Σ(Ti+Tj)只与n ...

  8. 【CodeForces】713 C. Sonya and Problem Wihtout a Legend

    [题目]C. Sonya and Problem Wihtout a Legend [题意]给定n个数字,每次操作可以对一个数字±1,求最少操作次数使数列递增.n<=10^5. [算法]动态规划 ...

  9. discuz2.5登录后台闪退的解决办法

    今天突然发现discuz2.5论坛后台进不去,开始以为密码错了,但发现登录后也是闪退.我试着清除浏览器cookie,也换了其他浏览器也没有用,还是上网找找吧! discuz2.5进入后台闪退的原因: ...

  10. 导航狗IT周报-2018年05月27日

    原文链接:https://www.daohanggou.cn/2018/05/27/it-weekly-9/ 摘要: “灰袍技能圈子”将闭圈:物理安全:为什么我们现在的生活节奏越来越快? 技术干货 1 ...