[Luogu] 仓鼠找sugar
https://www.luogu.org/problemnew/show/3398
树剖练习题,两个懒标记,搜索时序为全局懒标记
#include <bits/stdc++.h> using namespace std;
const int N = 1e5 + ; #define gc getchar()
#define lson jd << 1
#define rson jd << 1 | 1 struct Node_1{
int v, nxt;
}G[N << ];
struct Node_2{
int fa, son, siz, deep, topp, tree;
}P[N];
struct Node_3{
int l, r, f, ff;
}T[N << ];
int n, Ti, head[N], now = , tim, imp_clock;
bool ans, answer; inline int read(){
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} inline void add(int u, int v){
G[now].v = v;
G[now].nxt = head[u];
head[u] = now ++;
} void dfs_find_son(int u, int fa, int dep){
P[u].fa = fa;
P[u].deep = dep;
P[u].siz = ;
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != fa){
dfs_find_son(v, u, dep + );
P[u].siz += P[v].siz;
if(P[v].siz > P[P[u].son].siz) P[u].son = v;
}
}
} void dfs_to_un(int u, int tp){
P[u].topp = tp;
P[u].tree = ++ tim;
if(!P[u].son) return ;
dfs_to_un(P[u].son, tp);
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != P[u].fa && v != P[u].son) dfs_to_un(v, v);
}
} void Sec_G(int l, int r, int jd, int x, int y, int g){
if(x <= l && r <= y) {
T[jd].f = g;
T[jd].ff = g;
return ;
}
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y, g);
if(y > mid) Sec_G(mid + , r, rson, x, y, g);
if(T[lson].f == g || T[rson].f == g) T[jd].f = g; } void Sec_G_imp(int x, int y){
++ imp_clock;
int tp1 = P[x].topp, tp2 = P[y].topp;
while(tp1 != tp2){
if(P[tp1].deep < P[tp2].deep) swap(x, y), swap(tp1, tp2);
Sec_G(, n, , P[tp1].tree, P[x].tree, imp_clock);
x = P[tp1].fa;
tp1 = P[x].topp;
}
if(P[x].deep > P[y].deep) swap(x, y);
Sec_G(, n, , P[x].tree, P[y].tree, imp_clock);
} void Sec_pd(int l, int r, int jd, int x, int y){
if(x <= l && r <= y){
if(T[jd].f == imp_clock) ans = ;
return ;
}
if(T[jd].ff == imp_clock && T[jd].f == imp_clock){
T[lson].f = T[rson].f = T[jd].f;
T[lson].ff = T[rson].ff = imp_clock;
}
int mid = (l + r) >> ;
if(x <= mid) Sec_pd(l, mid, lson, x, y);
if(y > mid) Sec_pd(mid + , r, rson, x, y);
} bool Sec_pd_imp(int x, int y){
int tp1 = P[x].topp, tp2 = P[y].topp;
while(tp1 != tp2){
if(P[tp1].deep < P[tp2].deep) swap(x, y), swap(tp1, tp2);
ans = ;
Sec_pd(, n, , P[tp1].tree, P[x].tree);
if(ans) return ;
x = P[tp1].fa;
tp1 = P[x].topp;
}
ans = ;
if(P[x].deep > P[y].deep) swap(x, y);
Sec_pd(, n, , P[x].tree, P[y].tree);
if(ans) return ;
return ;
} inline void work(int x_1, int y_1, int x_2, int y_2){
Sec_G_imp(x_1, y_1);
answer = Sec_pd_imp(x_2, y_2);
if(answer) puts("Y");
else puts("N");
return ;
} int main()
{
n = read(); Ti = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i < n; i ++){
int u = read(), v = read();
add(u, v); add(v, u);
}
dfs_find_son(, , );
dfs_to_un(, );
while(Ti --){
int x_1 = read(), y_1 = read(), x_2 = read(), y_2 = read();
work(x_1, y_1, x_2, y_2);
}
return ;
}
/*
5 5
2 5
4 2
1 3
1 4
5 1 5 1
2 2 1 4
4 1 3 4
3 1 1 5
3 5 1 4
*/
[Luogu] 仓鼠找sugar的更多相关文章
- Luogu P3412 仓鼠找$sugar$ $II$
Luogu P3412 仓鼠找\(sugar\) \(II\) 题目大意: 给定一棵\(n\)个点的树, 仓鼠每次移动都会等概率选择一个与当前点相邻的点,并移动到此点. 现在随机生成一个起点.一个终点 ...
- [Luogu 3398] 仓鼠找sugar
[Luogu 3398] 仓鼠找sugar 又是 LCA- 前两天死活写不过的一个题今天终于顺手切了. 思路嘛参考了一楼题解. 就是说,对于 a, b, c, d 四个点, 令 x = LCA(a, ...
- luogu P3412 仓鼠找sugar II 期望 树形dp
LINK:仓鼠找sugar II 以前做过类似的期望题目 加上最后的树形dp不算太难 还是可以推出来的. 容易发现 当固定起点和终点的时候 可以先固定根 这样就不用分到底是正着走还是倒着走了. 1为根 ...
- 洛谷P3398 仓鼠找sugar [LCA]
题目传送门 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...
- P3398 仓鼠找sugar
P3398 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...
- 【Luogu3398】仓鼠找sugar(树链剖分)
[Luogu3398]仓鼠找sugar(树链剖分) 题面 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他 ...
- 【洛谷】【lca+结论】P3398 仓鼠找sugar
[题目描述:] 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而他的基友同时要从他的卧室 ...
- 洛谷 P3398 仓鼠找sugar 解题报告
P3398 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...
- 【树链剖分/倍增模板】【洛谷】3398:仓鼠找sugar
P3398 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...
随机推荐
- 【HC89S003F4开发板】 7ASM&C混合编程
HC89S003F4开发板ASM&C混合编程 一.前言 在配套资料里有实现ASM&C混合编程的说明文档,这里来实现这个功能. 二.实现ASM&C混合编程 1.打开 Keil 工 ...
- mysql创建唯一索引UNIQUE INDEX,以及报错“#失败原因: [Execute: Duplicate entry '733186700' for key 'uniq_video_id_index']”
要给t_video_prods表的video_id字段创建唯一所以,可以使用下面这条语句: alter table t_video_prods add UNIQUE INDEX `uniq_video ...
- 是否应该学习qt源码(碰到问题的时候,或者文档对函数描述不清楚的时候,可以看一下)
是否应该学习qt源码 如果你想调用某个函数,但是文档并没有清晰描述这个函数的功能的时候,你就需要去阅读源码,看看Qt究竟是怎么实现的.比如用QNetworkAccessManager发送一个QHttp ...
- Liar CodeForces - 822E (dp,后缀数组)
大意: 给定串$s,t$, 给定整数$x$, 求判断$t$是否能划分为至多$x$段, 使这些段在$s$中按顺序,不交叉的出现. 设$dp_{i,j}$表示$s$匹配到$i$位, 划分了$j$段, 匹配 ...
- Mock常用占位符一览
1.随机字符串(类型 , 位数)@string(lower,10) lower : 小写字母upper : 大写字母number : 数字 2.随机int(下限 , 上限)@integer(60, 1 ...
- Java架构笔记:用JWT对SpringCloud进行认证和鉴权
写在前面 喜欢的朋友可以关注下专栏:Java架构技术进阶.里面有大量batj面试题集锦,还有各种技术分享,如有好文章也欢迎投稿哦. image.png JWT(JSON WEB TOKEN)是基于RF ...
- Emit用法
[转自]https://blog.csdn.net/xiaouncle/article/details/52890037 本人是从0开始学习Emit的,在学习过程中比较困扰我的就是有很多指令不理解.不 ...
- 杭电1253 超时的bfs...
#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>usin ...
- 自定义AuthorizeFilter
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization.Infrastructure; u ...
- RestFramework之注册器、响应器与分页器
一.注册器的说明与使用 在我们编写url时经常会因请求方式不同,而重复编写某条url,而rest_framework中的注册器帮我节省了很多代码 下面介绍一下如何使用 # 利用注册器来实现路由分发 f ...