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

思路:

瞎搞\(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. 【leetcode】969. Pancake Sorting

    题目如下: Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.len ...

  2. C/C++ 智能指针线程池

    //这个线程池存在一定的BUG 如果没有多线程编程基础的先生请谨慎使用 //我放弃了这种模板方式的线程池,最好不要使用!!!!!!! ThreadPool.h { #ifndef __THREADPO ...

  3. java-逻辑处理

    类名是ItemDAO package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.s ...

  4. 【LeetCode 19】删除链表的倒数第N个节点

    题目链接 [题解] 经典的一道题. 让p1指向链表的第一个元素. 让p2指向链表的第二个元素. 然后让他们俩同时往后移动. 直到p2到达链表的尾巴. 这时p1和p2之间总是隔了n-1个元素. 所以p1 ...

  5. js文字转语音(speechSynthesis)

    环境: windows 官网网址: https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis 基础使用: var msg = n ...

  6. centos7安装kylo0.10.1

    安装环境centos7,kylo版本0.10.1 常用的链接地址 kylo官网:https://kylo.io/ kylo文档:https://kylo.readthedocs.io/ 下载地址 官网 ...

  7. NX二次开发-C语言文件读写fwrite和fread函数

    NX9+VS2012 #include <uf.h> #include <stdio.h> UF_initialize(); /* //设置文件路径 const char* f ...

  8. Spring源码剖析3:Spring IOC容器的加载过程

    本文转自五月的仓颉 https://www.cnblogs.com/xrq730 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https ...

  9. 如何在一个for语句中迭代多个对象(2.7)

    如何在一个for语句中迭代多个对象 总结: 并行迭代使用zip(l1, l2, l3) 每次迭代从3个列表里各取一个数据 串行迭代使用itertools.chain(l1, l2, l3) 相当于把3 ...

  10. Ubuntu下安装Samba服务器

    闲来无聊尝试自己安装下Samba服务器,使本机和虚拟机可以无障碍传输文件(虽然用VMwaretools可传,但总感觉麻烦,而且速度欠佳) 首先,同安装qemu一样,在安装之前要确定你的系统apt列表已 ...