第一发lg[]没开够RE了,下了数据本地一直停止运行,还以为是dfs死了,绝望一交,A了。。。

判断\(x\)是否在路径\(s-t\)上,只需满足

\(dep_{x} >= dep_{LCA(s,t)}\)

\(LCA(s, x) = x\) \(or\) \(LCA(t, x) = x\)

//#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause") #else #define D_e_Line ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 100007; struct Edge{
int nxt, pre;
}e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v){
e[++cntEdge] = (Edge){head[u], v}, head[u] = cntEdge;
} int dep[N], f[N][19];
int lg[N];
inline void DFS(int u, int fa){
dep[u] = dep[fa] + 1, f[u][0] = fa;
for(register int i = 1; (1 << i) <= dep[u]; ++i){
f[u][i] = f[f[u][i - 1]][i - 1];
}
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v == fa) continue; DFS(v, u);
}
}
inline int LCA(int x, int y){
if(dep[x] < dep[y]) Swap(x, y);
while(dep[x] > dep[y]){
x = f[x][lg[dep[x] - dep[y]] - 1];
}
if(x == y) return x;
nR(i,lg[dep[x]] - 1,0){
if(f[x][i] != f[y][i]){
x = f[x][i];
y = f[x][i];
}
}
return f[x][0];
} //int fa[N], siz[N], son[N], dep[N];
//inline void DFS_First(int u, int father){
// fa[u] = father, siz[u] = 1, dep[u] = dep[father] + 1;
// for(register int i = head[u]; i; i = e[i].nxt){
// int v = e[i].pre;
// if(v == father) continue;
// DFS_First(v, u);
// siz[u] += siz[v];
// if(!son[u] || siz[v] > siz[son[u]]){
// son[u] = v;
// }
// }
//}
//int top[N], dfn[N], dfnIdx;
//inline void DFS_Second(int u, int ancester){
// top[u] = ancester, dfn[u] = ++dfnIdx;
// if(!son[u]) return;
// DFS_Second(son[u], ancester);
// for(register int i = head[u]; i; i = e[i].nxt){
// int v = e[i].pre;
// if(v != fa[u] && v != son[u]){
// DFS_Second(v, v);
// }
// }
//}
//inline int LCA(int x, int y){
// while(top[x] != top[y]){
// if(dep[top[x]] < dep[top[y]]) Swap(x, y);
// x = fa[top[x]];
// }
// return dep[x] < dep[y] ? x : y;
//} int main(){
// freopen("in.txt","r",stdin);
// freopen("my.txt","w",stdout);
int n, Ques;
io >> n >> Ques;
R(i,2,n){
int u, v;
io >> u >> v;
add(u, v);
add(v, u);
}
R(i,1,n) lg[i] = lg[i - 1] + ((1 << lg[i - 1]) == i); // DFS_First(1, 0);
// DFS_Second(1, 1); DFS(1, 0); while(Ques--){
int a, b, c, d;
io >> a >> b >> c >> d; int S1 = LCA(a, b), S2 = LCA(c, d);
if(dep[S1] < dep[S2]){
Swap(S1, S2);
c = a;
d = b;
}
if(LCA(S1, c) == S1 || LCA(S1, d) == S1)
printf("Y\n");
else
printf("N\n");
} return 0;
}

Luogu3398 仓鼠找sugar (LCA)的更多相关文章

  1. 洛谷P3398 仓鼠找sugar [LCA]

    题目传送门 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...

  2. luogu P3398 仓鼠找sugar [LCA]

    题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而他的基友同时要从他的卧室(c) ...

  3. 洛谷10月月赛Round.1| P3398 仓鼠找sugar[LCA]

    题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而他的基友同时要从他的卧室(c) ...

  4. 仓鼠找sugar(LCA)

    小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而他的基友同时要从他的卧室(c)到图书馆( ...

  5. P3398 仓鼠找sugar[LCA]

    题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而他的基友同时要从他的卧室(c) ...

  6. [luogu3398][仓鼠找sugar]

    luogu3398 思路: 假设松鼠a要从a1去a2,松鼠b要从b1去b2,ks表示lca(a1,a2)和lca(b1,b2)中深度较深的那个.那么,若要使得两只松鼠可能相遇,则只要满足lca(a1, ...

  7. p3398 仓鼠找sugar (LCA+讨论)

    分情况讨论,结果是两条路径有公共点时,深度大的LCA在另一条路径上且另一条路径的两个端点至少其中一个的与深度大的LCA的LCA为那个深度大的LCA #include <cstdio> #i ...

  8. 【Luogu3398】仓鼠找sugar(树链剖分)

    [Luogu3398]仓鼠找sugar(树链剖分) 题面 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他 ...

  9. 【洛谷】【lca+结论】P3398 仓鼠找sugar

    [题目描述:] 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而他的基友同时要从他的卧室 ...

随机推荐

  1. 关于『进击的Markdown』:第五弹

    关于『进击的Markdown』:第五弹 建议缩放90%食用 路漫漫其修远兮,吾将上下而求索.  我们要接受Mermaid的考验了呢  Markdown 语法真香(一如既往地安利) ( 进击吧!Mark ...

  2. 测试open

    // 此处,返回的 undefined 是 JS 中的一个值 return undefined } // 这种写法是明确指定函数返回值类型为 void,与上面不指定返回值类型相同 const add ...

  3. 五分钟搞懂POM设计模式

    转载请注明出处️ 作者:IT小学生蔡坨坨 原文链接:五分钟搞懂POM设计模式 大家好,我是IT小学生蔡坨坨. 今天,我们来聊聊Web UI自动化测试中的POM设计模式. 为什么要用POM设计模式 前期 ...

  4. mysql中max_connections与max_user_connections使用区别

    问题描述:把max_connections和max_user_connections参数进行分析测试,顾名思义,max_connections就是负责数据库全局的连接数,max_user_connec ...

  5. 获取请求体数据 POST

    POST获取请求体 请求体中封装了 POST请求的请求参数 获取流对象 再从流对象中那数据 一种字节流 一种字符流 BufferedReader getReader()获取字符输入流 只能操作字符 S ...

  6. sql-DQL-单表查询

    单表查询 select [distint]* 字段列表 from 表名列表 where 条件列表 group by 分组字段 having 分组之后的条件 order by 排序 limit 分页限定 ...

  7. 修改windows字符集

    手动 临时修改cmd默认字符集(代码页) chcp xxxx 自动<打开cmd后应该自动运行dhcp 65001,临时设置为utf-8> D:\Develope\apache-tomcat ...

  8. python小题目练习(八)

    题目:电视剧的收视率排行榜 需求:实现如下图所示需求  代码展示: """Author:mllContent:电视剧的收视率排行榜Date:2020-11-16" ...

  9. EasyExcel导出添加批注

    直接看代码.根据个人需要做改动 注:POI也可以做批注,文章链接https://www.cnblogs.com/qq1445496485/p/15622664.html /** * 导出(批注) * ...

  10. Java创建TXT文件并写入 内容

    public static void main(String[] args) { String filePath = "E:/" + "1.txt"; Stri ...