我现在爱死树链剖分了

题目

具体分析的话在洛谷blog

这里只是想放一下改完之后的代码

多了一个son数组少了一个for

少了找size最大的儿子的for

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = ;
int n, q, head[N], cnt, dad[N], top[N], size[N], deep[N], son[N];
struct edge{
int next, to;
}e[N << ];
int read() {
int s = , w = ;
char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') w = -;ch = getchar();}
while(isdigit(ch)) {s = s * + ch - '';ch = getchar();}
return s * w;
}
void add(int x, int y) {
e[++cnt].next = head[x];
e[cnt].to = y;
head[x] = cnt;
}
void dfs(int now) {
size[now] = ;
deep[now] = deep[dad[now]] + ;
for(int i = head[now]; i; i = e[i].next)
if(e[i].to != dad[now]) {
dad[e[i].to] = now, dfs(e[i].to), size[now] += size[e[i].to];
if(size[e[i].to] > size[son[now]]) son[now] = e[i].to;
}
}
void dfs1(int now) {
if(!top[now]) top[now] = now;
if(son[now]) top[son[now]] = top[now], dfs1(son[now]);
for(int i = head[now]; i; i = e[i].next)
if(e[i].to != dad[now] && e[i].to != son[now])
dfs1(e[i].to);
}
int lca(int x, int y) {
while(top[x] != top[y]) {
if(deep[top[x]] < deep[top[y]]) swap(x, y);
x = dad[top[x]];
}
return deep[x] > deep[y] ? y : x;
}
int main () {
n = read();q = read();
for(int i = , u, v; i < n; i++) {
u = read();v = read();
add(u, v), add(v, u);
}
dfs();
dfs1();
for(int i = , a, b, c, d; i <= q; i++) {
a = read(), b = read(), c = read(), d = read();
int fir = max(deep[lca(a, b)], deep[lca(c, d)]);
int sec = max(max(deep[lca(a, c)], deep[lca(a, d)]), max(deep[lca(b, c)], deep[lca(b, d)]));
if(sec >= fir) puts("Y");
else puts("N");
}
return ;
}

谢谢收看, 祝身体健康!

洛谷p3398仓鼠找suger题解的更多相关文章

  1. 洛谷 P3398 仓鼠找sugar 题解

    每日一题 day44 打卡 Analysis 首先有一个结论:先找 p1=(a,b),p2=(c,d) 的LCA的深度,在与(a,c),(a,d),(b,c),(b,d)中最深的LCA n的深度比较, ...

  2. 洛谷P3398 仓鼠找suger

    传送门啦 思路: 那么从 $ A $ 到 $ B $ 可以分解成 先从 $ A $ 到 $ X $ 再从 $ X $ 到 $ B $ ... 另一个同理 假设能相遇 那么 要么在 $ A $ 到 $ ...

  3. 洛谷 P3398 仓鼠找sugar 解题报告

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

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

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

  5. 洛谷P3398 仓鼠找sugar

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

  6. 洛谷 P3398 仓鼠找sugar —— 树链剖分

    题目:https://www.luogu.org/problemnew/show/P3398 树链剖分一下,路径就变成线段树上的几个区间: 两条路径相交就是线段树上有区间相交,所以在相应位置打个标记, ...

  7. 洛谷——P3398 仓鼠找sugar

    https://www.luogu.org/problem/show?pid=3398#sub 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴 ...

  8. 洛谷 [P3398] 仓鼠找sugar

    树剖求LCA 我们可以发现,两条路径ab,cd相交,当且仅当 dep[lca(a,b)]>=dep[lca(c,d)]&(lca(lca(a,b),c)==lca(a,b)||lca(l ...

  9. 洛谷P3412 仓鼠找$Sugar\ II$题解(期望+统计论?)

    洛谷P3412 仓鼠找\(Sugar\ II\)题解(期望+统计论?) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1327573 原题链接:洛谷P3412 ...

随机推荐

  1. 第四周(1):数据分布-Python实战

    数据准备 数据集地址:http://jse.amstat.org/datasets/normtemp.dat.txt 数据集描述:总共只有三列:体温.性别.心率 数据集详细描述:Journal of ...

  2. Java自学-接口与继承 默认方法

    默认方法 步骤 1 : 什么是默认方法 默认方法是JDK8新特性,指的是接口也可以提供具体方法了,而不像以前,只能提供抽象方法 Mortal 这个接口,增加了一个默认方法 revive,这个方法有实现 ...

  3. 基于react-app搭建react-router+redux项目

    前言 总括: 本文采用react+redux+react-router+less+es6+webpack,以实现一个简易备忘录(todolist)为例尽可能全面的讲述使用react全家桶实现一个完整应 ...

  4. echarts 3D地球实现自动旋转

    素材已上传至https://gitee.com/i1520/echarts3DEarth.git     https://github.com/i1520/echarts3DEarth 1.引入js文 ...

  5. The Vertu of the Dyamaund钻石

    dyamaund and the English words dyamaund The Vertu of the Dyamaund": Gemstones, Knowledge and Va ...

  6. Unity手游汉化笔记①:UABE+AssetStudio编辑MonoBehavior类型Asset

    总的笔记:https://www.cnblogs.com/guobaoxu/p/12055930.html 目录 一.使用工具 二.具体操作 [1]利用AssetStudio进行预览 [2]UABE修 ...

  7. MySQL Config--参数innodb_flush_method

    延迟写 传统的UNIX实现在内核中设有缓冲区高速缓存或页面高速缓存,大多数磁盘I/O都通过缓冲进行.当将数据写入文件时,内核通常先将该数据复制到其中一个缓冲区中,如果该缓冲区尚未写满,则并不将其排入输 ...

  8. linux系统IO操作

    本文重点说明下面内容: 什么是标准IO,什么是文件IO? 什么是Direct IO? O_SYNC标识有什么意义? 各个层面的缓存如何同步? 还在page cache中的脏页可以读写吗? IO路径上的 ...

  9. 《linux就该这么学》课堂笔记09 存储结构、磁盘划分

    Linux一切都是文件 "/"为根目录(万物起始)  **挂载后要想永久生效,需要修改开机启动项 vim /etc/fstab  

  10. kubernetes(k8s)与其架构

    1. kubernetes简介 kubernetes,简称K8s,是用8代替8个字符“ubernete”而成的缩写.是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是 ...