题目链接: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. hbase实践之HFile结构

    本文目录如下所示: 目录 HFile在HBase架构中的位置 什么是HFile HFile逻辑结构 HFile逻辑结构的优点 HFile物理结构 HFile生成流程 HFile中Block块解析 多大 ...

  2. 长春理工大学第十四届程序设计竞赛H Arithmetic Sequence——使用特例

    题目 链接 题意:给定一个数X,输出一个等差数列,使得和为X. 分析 由等差数列的定义,可见一个数就是等差数列,两个数也是等差数列 #include<bits/stdc++.h> usin ...

  3. 包过滤防火墙iptables(网络层)

    安装: yum -y install iptables-services 启动:systemctl start iptables.service 四表五链 过滤:filter - input forw ...

  4. python基于opencv实现人脸定位

    import cv2 # 读取图片 img = cv2.imread("image.jpg") # 加载模型,模型可以从https://github.com/opencv/open ...

  5. MySQL多表查询总结

    MySQL术语: Redundacncy(冗余):存储两次或多次数据,以便实现快速查询. Primary Key(主键):主键是唯一的.表中每条记录的唯一标识. Foreign Key(外键):用于连 ...

  6. Codevs 1404 字符串匹配(Kmp)

    1404 字符串匹配 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你两个串A,B,可以得到从A的任意位开始的子串和B匹配的 ...

  7. 记录一:tensorflow下载安装

    1.下载Anaconda,默认选项安装 2.验证 conda --v 3.下载 tensorflow     3.1 创建环境 conda create -n tensorflow python=3. ...

  8. 查看API工具 https://editor.swagger.io/

    The base URL for the API is:    https://api.cloud.nalantis.com/api/ The OpenAPI documentation is ava ...

  9. umei-spider

    umei-spider 1 #!/usr/bin/python3 2 3 import requests 4 from bs4 import BeautifulSoup 5 from contextl ...

  10. Dubbo系列(二)dubbo的环境搭建

    dubbo是一个分布式服务框架,提供一个SOA的解决方案.简单的说,dubbo就像在生产者和消费者中间架起了一座桥梁,使之能透明交互.本文旨在搭建一个可供使用和测试的dubbo环境,使用了spring ...