题目链接:http://cogs.pro:8081/cogs/problem/problem.php?pid=vQXmxVaPU

【题目描述】

“这就是我们最新研制的,世界上第一种可持久化动态计算机病毒,‘创世纪’。”方教授介绍道。

“哦。”主席面无表情地点点头。

“‘创世纪’无法真正杀死透明计算网络,但是可以把它变成傻子。可惜透明计算网络能轻松地辨认出病毒,所以我建议……”

“为什么不伪装呢?”Asm.Def说。

“当然不行,它比我们更懂伪装。”

“我是说,把我们的病毒伪装成杀毒软件。”

方教授震惊地盯着Asm.Def看了一会。“你是个天才。”

Asm.Def想把病毒伪装成杀毒软件,入侵透明计算网络。透明计算网络的文件结构是一棵N个节点的树,每个病毒可以入侵一条路径上的所有节点。但如果两个病毒入侵了同一个节点,由于它们伪装成了杀毒软件,就会自相残杀。Asm.Def不希望这样的情况发生,所以他需要仔细制定入侵计划。为此,他需要频繁地询问,两条路径是否经过同一个节点(即是否相交)。

【输入格式】

第一行两个整数N,Q。

接下来N-1行,每行两个整数a,b,表示(a,b)是树上的一条边。

接下来Q行,每行四个整数s1,t1,s2,t2,表示询问s1~t1的路径是否与s2~t2的路径相交。

【输出格式】

对每个询问,若相交则输出一行”YES”,否则输出一行”NO”。

【样例输入】

6 5
1 2
1 3
2 4
4 5
4 6
1 1 5 6
1 2 6 3
2 3 5 6
6 4 3 1
4 3 1 2

【样例输出】

NO
YES
NO
NO
YES

【提示】

N,Q<=1000.

1<=s1,t1,s2,t2<=N。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 1010
using namespace std;
struct edge
{
int to;
edge* pre;
edge() :to(), pre(NULL) {};
}e[MAXN << ]; int n, q;
int s1, t1, s2, t2;
int a, b, cnt = ;
edge* preV[MAXN] = { NULL }; //preV[n]用于取以n为起点的边的地址,依次更新,如果链式遍历每个以n为起点的边
int p[MAXN] = { }; //p[n] 为n的父亲节点
bool vis[MAXN << ]; void insert(int a, int b)
{
e[cnt].to = b;
e[cnt].pre = preV[a];
preV[a] = &e[cnt++];
} void dfs(int x) //遍历处理p数组
{
int y;
for (edge* ee = preV[x]; ee; ee = ee->pre)
{
y = ee->to;
if (y == p[x])
continue ;
p[y] = x;
dfs(y);
}
} int LCA(int x, int y) //找到x和y的lca
{
memset(vis, , sizeof(vis));
while (x)
{
vis[x] = true;
x = p[x];
}
while (y)
{
if(vis[y])
return y;
y = p[y];
}
return x;
} int query(int lca, int s, int t) //查询lca是否在s和t的路径上
{
int l = LCA(s, t);
do
{
if (lca == s) //判断是否lca就是节点s
return true;
if (s == l) //判断节点s是否是s和t的lca
break;
s = p[s]; //往上走
} while (s);
/*不能用while的原因是:如果s刚好就等于L,无法进入循环,就无法对于lca==s返回true,如果将s==l放后边,则s = p[s]就把s转移了*/
while (t && t != l)
{
if (lca == t)
return true;
t = p[t];
}
return false;
} int main()
{
//freopen("asm_virus.in", "r", stdin);
//freopen("asm_virus.out", "w", stdout);
scanf("%d %d", &n, &q);
for (int i = ; i < n; i++)
{
scanf("%d %d", &a, &b);
insert(a, b);
insert(b, a);
}
dfs();
int firLCA, secLCA;
while (q--)
{
scanf("%d %d %d %d", &s1, &t1, &s2, &t2);
firLCA = LCA(s1, t1);
secLCA = LCA(s2, t2);
if (query(firLCA, s2, t2) || query(secLCA, s1, t1))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return ;
}

LCA -cogs2098 [SYOI 2015] Asm.Def的病毒的更多相关文章

  1. cogs 2098. [SYOI 2015] Asm.Def的病毒 LCA 求两条路径是否相交

    2098. [SYOI 2015] Asm.Def的病毒 ★☆   输入文件:asm_virus.in   输出文件:asm_virus.out   简单对比时间限制:1 s   内存限制:256 M ...

  2. cogs——2098. Asm.Def的病毒

    2098. Asm.Def的病毒 ★☆   输入文件:asm_virus.in   输出文件:asm_virus.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] “这就 ...

  3. COGS 2098. Asm.Def的病毒

    ★☆   输入文件:asm_virus.in   输出文件:asm_virus.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] “这就是我们最新研制的,世界上第一种可持 ...

  4. COGS——C2098. Asm.Def的病毒

    http://www.cogs.pro/cogs/problem/problem.php?pid=2098 ★☆   输入文件:asm_virus.in   输出文件:asm_virus.out    ...

  5. 2084. Asm.Def的基本算法

    2084. Asm.Def的基本算法 传送门 ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] ...

  6. COGS 2084. Asm.Def的基本算法

    ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] “有句美国俗语说,如果走起来像鸭子,叫起来像 ...

  7. cogs——2084. Asm.Def的基本算法

    2084. Asm.Def的基本算法 ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] “有句 ...

  8. COGS——T2084. Asm.Def的基本算法

    http://cogs.pro/cogs/problem/problem.php?pid=2084 ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间 ...

  9. Asm.Def点大兵

    syzoj上的题,收货很多,orz天天学长 原题: Asm.Def奉命组建一支m人的特种作战小队前往圣迭戈.他有n名候选人,可以在其中任意挑选.由于小队中每个人都有独特的作用,所以次序不同的两种选法被 ...

随机推荐

  1. debug:The key "" is not recognized and ignored.

    今天写代码时候发现chrome浏览器有一个警告The key "" is not recognized and ignored.既然发现了就处理一下吧,于是就有了这篇小文章. 查阅 ...

  2. “景驰科技杯”2018年华南理工大学程序设计竞赛 A. 欧洲爆破(思维+期望+状压DP)

    题目链接:https://www.nowcoder.com/acm/contest/94/A 题意:在一个二维平面上有 n 个炸弹,每个炸弹有一个坐标和爆炸半径,引爆它之后在其半径范围内的炸弹也会爆炸 ...

  3. 题解 [CF525D] Arthur and Walls

    题面 解析 首先考虑将一个\('*'\)变成\('.'\)后会形成什么, 显然至少是一个\(2\times 2\)的矩形. 因为\(1\times 1\)和\(1\times 2\)的改了没用啊, 而 ...

  4. Task , Thread 学习

    1.任务StartNew后就开始执行,使用Wait()来确保任务结束后继续 static void Main(string[] args) { try { int numberOfUsers = 10 ...

  5. neo4j 一些常用的CQL

    创建节点.关系 创建节点(小明):create (n:people{name:’小明’,age:’18’,sex:’男’}) return n; 创建节点(小红): create (n:people{ ...

  6. 0和5 (51Nod)

    小K手中有n张牌,每张牌上有一个一位数的数,这个字数不是0就是5.小K从这些牌在抽出任意张(不能抽0张),排成一行这样就组成了一个数.使得这个数尽可能大,而且可以被90整除. 注意: 1.这个数没有前 ...

  7. logstash6.5.4同步mysql数据到elasticsearch 6.4.1

    下载logstash-6.5.4 ZIP解压和es 放到es根目录下 下载mysql jdbc的驱动 mysql-connector-java-8.0.12 放在任意目录下 以下方式采用动态模板,还有 ...

  8. mounted里面this.$refs.xxx的内容是undefined

    在mounted(){}钩子里面使用this.$refs.xxx,打印出来的却是undefined? DOM结构已经渲染出来了,但是如果在DOM结构中的某个DOM节点使用了v-if.v-show或者v ...

  9. docker容器安装命令

    apt-get update apt-get install vim 转载请注明博客出处:http://www.cnblogs.com/cjh-notes/

  10. 搬运 centos7.2 apache 绑定二级目录 访问依然是apache页面

    <VirtualHost *:80>ServerName xx.comDocumentRoot /var/www/html/xx</VirtualHost>