题意:求树上两条路径有无祖先。

思路:

瞎搞\(LCA\)啊。。。

可惜我\(LCA\)打错了,我居然调了半小时...qwq

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
#define travel(i,x) for(int i = head[x];i;i=e[i].nxt)
inline int read () {
int q=0,f=1;char ch = getchar();
while(!isdigit(ch)){
if(ch=='-')f=-1;ch=getchar();
}
while(isdigit(ch)){
q=q*10+ch-'0';ch=getchar();
}
return q*f;
}
struct edge {
int to;
int nxt;
}e[maxn << 1];
int cnt;
int head[maxn << 1];
inline void add(int u,int v) {
e[++cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
int f[maxn][30];
int dep[maxn << 1];
inline void dfs(int x,int fa) {
dep[x] = dep[fa] + 1;
travel(i,x) {
int y = e[i].to;
if(y != fa) {
f[y][0] = x;
for(int j = 1;j <= 20; ++j) {
f[y][j] = f[f[y][j - 1]][j - 1];
}
dfs(y,x);
}
}
}
inline int lca(int x,int y) {
if(dep[x] < dep[y]) swap(x,y);
for(int i = 20;i >= 0; --i) {
if(dep[f[x][i]] >= dep[y]) x = f[x][i];
}
if(x == y) return x;
for(int i = 20; i >= 0; --i) {
if(f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
int n,m;
int main () {
freopen("inter.in","r",stdin);
freopen("inter.out","w",stdout);
n = read();
for(int i = 1;i < n; ++i) {
int x = read(),y = read();
add(x,y);add(y,x);
}
dfs(1,0);
m = read();
while(m--) {
int x = read(),y = read();
int LCA = lca(x,y);
int l = read(),r = read();
int _LCA = lca(l,r);
//cout<<"LCA:"<<LCA<<' '<<_LCA<<endl;
int Grand_LCA = lca(LCA,_LCA);
if(LCA == _LCA) {
puts("YES");
}
else if(Grand_LCA == _LCA && (lca(l,LCA) == LCA || lca(r,LCA) == LCA)) {
puts("YES");
}
else if(Grand_LCA == LCA && (lca(x,_LCA) == _LCA || lca(y,_LCA) == _LCA)) {
puts("YES");
}
else puts("NO");
}
return 0;
}

[JZOJ 5852] 相交的更多相关文章

  1. [JZOJ 5852] [NOIP2018提高组模拟9.6] 相交 解题报告 (倍增+LCA)

    题目链接: http://172.16.0.132/senior/#main/show/5852 题目: 题目大意: 多组询问,每次询问树上两条链是否相交 题解: 两条链相交并且仅当某一条链的两个端点 ...

  2. JZOJ 5602.【NOI2018模拟3.26】Cti

    JZOJ 5602.[NOI2018模拟3.26]Cti Description 有一个 \(n×m\) 的地图,地图上的每一个位置可以是空地,炮塔或是敌人.你需要操纵炮塔消灭敌人. 对于每个炮塔都有 ...

  3. 求两圆相交部分面积(C++)

    已知两圆圆心坐标和半径,求相交部分面积: #include <iostream> using namespace std; #include<cmath> #include&l ...

  4. 51node1264(判断线段相交)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1264 题意:中文题诶- 思路:对于直线a1a2, b1b2, ...

  5. POJ 1066 Treasure Hunt (线段相交)

    题意:给你一个100*100的正方形,再给你n条线(墙),保证线段一定在正方形内且端点在正方形边界(外墙),最后给你一个正方形内的点(保证不再墙上) 告诉你墙之间(包括外墙)围成了一些小房间,在小房间 ...

  6. POJ 2653 Pick-up sticks (线段相交)

    题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...

  7. UVALive 4428 Solar Eclipse --计算几何,圆相交

    题意:平面上有一些半径为R的圆,现在要在满足不与现有圆相交的条件下放入一个圆,求这个圆能放的位置的圆心到原点的最短距离. 解法:我们将半径扩大一倍,R = 2*R,那么在每个圆上或圆外的位置都可以放圆 ...

  8. HDU1086You can Solve a Geometry Problem too(判断线段相交)

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  9. 平面内,线与线 两条线找交点 两条线段的位置关系(相交)判定与交点求解 C#

    个人亲自编写.测试,可以正常使用   道理看原文,这里不多说   网上找到的几篇基本都不能用的   C#代码 bool Equal(float f1, float f2) { return (Math ...

随机推荐

  1. C# 16进制转字符串,字符串转16进制

    { //========================================================== //16进制转字符串 public static byte[] HexTo ...

  2. Python内置模块-time && datatime

    Python提供两种时间表示方式,一种是时间戳,从1970年1月1日 0时开始.一种是struct_time数组格式,共有9个元素. import time print(time.time()) #返 ...

  3. nodejs moment 修改时间格式 日期格式与时间戳格式互相转化

    node js moment 修改时间格式 日期格式与int格式互相转化 nvm use 8.3 > moment = require('moment') > days = '2019-0 ...

  4. 三:robotframework框架配合selenium之jquery定位

    RF框架我用的比较多,限于自己的代码水平,目前工作中一直是用RF框架.unittest框架,我也练习了很久,还差一点封装的技巧与设计模式的学习.所以不敢在项目中实践. 等明年我的代码水平再上升一个台阶 ...

  5. POJ 3468 A Simple Problem with Integers (分块)

    Description You have \(N\) integers, \(A_1, A_2, ... , A_N\). You need to deal with two kinds of ope ...

  6. Codeforces 1189B Number Circle

    题目链接:http://codeforces.com/problemset/problem/1189/B AC代码: #include<bits/stdc++.h> using names ...

  7. Java 序列化和反序列化(二)Serializable 源码分析 - 1

    目录 Java 序列化和反序列化(二)Serializable 源码分析 - 1 1. Java 序列化接口 2. ObjectOutputStream 源码分析 2.1 ObjectOutputSt ...

  8. @ApiImplicitParams、ApiImplicitParam的使用

    @ApiImplicitParam:作用在方法上,表示单独的请求参数 参数: 1. name :参数名. 2. value : 参数的具体意义,作用. 3. required : 参数是否必填. 4. ...

  9. SDL系列之 - 用画直线的方法来画正弦曲线

    线段长度无限短后就成为点,所以,现在让我们用画直线的方法来画正弦曲线吧 #include <SDL.h> #include <stdlib.h> #include <st ...

  10. apache+tomcat配置负载均衡,实现http与websocket接口分压

    一.应用场景 在生产环境中,应用服务器(此文以tomcat为例)不免面临高访问量的压力,相比而言web服务器(此文以apache为例)能够承担更大的并发量.Apache本身带有一些模块,可以完成对应用 ...