2023.7.3 Problem Link

交互库有一棵 \(n\) 个点的二叉树,你每次可以询问两个点之间的距离,猜出这棵二叉树。\(n\le 3000\),询问次数上限 \(30000\)。


首先给你距离一定是先把每个点的深度问出来,确定一个大致的考虑顺序。

然后我们开始仔细思考“距离”这个条件怎么用。发现询问两个未知的点之间的距离没啥用,询问两个已知的点很蠢,于是只可能询问未知点和已知点之间的距离。

假设未知点是 \(u\),已知点是 \(v\),那我们就得到了 \(dep[\mathrm{lca}(u,v)]\)。进一步,\(v\) 已知,这意味着我们可以知道 \(\mathrm{lca}(u,v)\) 是哪个。

走到这一步这道题就很明朗了。我们希望进行一个类似于二分的过程,结合二叉树这一点,我们可以将考虑范围缩小到链往下的一棵子树内。这不就是重剖吗?做完了。

具体地,对于每个新点,对已知的树进行重剖,找到当前根所在重链的底端,询问新节点和它的距离,并改变当前节点。次数为 \(\sum_{i=1}^n \log i=O(n\log n)\),并且常数很小,很符合题目限制。

【构造,树】【Loj】Loj6669 Nauuo and Binary Tree的更多相关文章

  1. LOJ #6669 Nauuo and Binary Tree (交互题、树链剖分)

    题目链接 https://loj.ac/problem/6669 题解 Orz yyf太神了,出这种又有意思又有意义的好题造福人类-- 首先\(n\)次询问求出所有节点的深度. 考虑按深度扩展(BFS ...

  2. 【树】Serialize and Deserialize Binary Tree

    题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...

  3. [LeetCode 114] - 将树平面化为链表(Flatten Binary Tree to Linked List)

    问题 给出一个二叉树,将其原地平面化为链表. 例如,给出: 1   /  \  2    5 / \     \ 3  4     6 平面化后的树看起来应该是这样: 1 \  2    \      ...

  4. leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal----- java

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  6. [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  8. [Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:  You may assume tha ...

  9. leetcode 106. 从中序与后序遍历序列构造二叉树(Construct Binary Tree from Inorder and Postorder Traversal)

    目录 题目描述: 示例: 解法: 题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 示例: 给出 中序遍历 inorder = [9,3,15,20,7] ...

  10. leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

随机推荐

  1. Solon 问答:项目如何直接添加 https 支持?

    app.yml 添加两行配置即可: #设定SSL证书(支持:solon.boot.jdkhttp 或 solon.boot.jlhttp 或 solon.boot.jetty 或 solon.boot ...

  2. Solon 开发进阶

    Solon 开发进阶 一.插件扩展机制 二.体外扩展机制 三.常用配置说明 四.启动参数说明 五.全局异常订阅 本系列在内核知识的基础上做进一步延申.主要涉及: 插件扩展体系 体外扩展体系 常用配置 ...

  3. Django增删改查

    增删改查.配置对应路由,函数,视图.报错注意看控制台. 添加取到前台传来的参数,后端给予验证.入库 编辑,取到当前编辑得id,在后台查到对应数据.重新update 删除,取到当前点击ID,后台dele ...

  4. Java线程池使用浅谈

    1. 线程池相关基本概念 任务(Task):任务是线程池中要执行的工作单元.任务可以是实现了 Runnable 接口或 Callable 接口的对象.Runnable 任务没有返回值,而 Callab ...

  5. Python | __init__.py的神奇用法

    0._init_.py 在Python工程里,当python检测到一个目录下存在_init_.py文件时,python就会把它当成一个模块(module).Module跟C++的命名空间和Java的P ...

  6. 如何做一次完美的 ABTest?

    本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/mO5MdwG7apD6RzDhFwZhog作者:DuZhimin 越来越多的公司都在尝试 AB ...

  7. 基于python+django的求职招聘网站-网上招聘管理系统设计与实现

    该系统是基于python+django的求职招聘网站.网上招聘管理系统.网上人才招聘系统.毕业生求职招聘系统.大学生求职招聘系统.校园招聘系统.企业招聘系统.系统适合场景:大学生.课程作业.毕业设计. ...

  8. Nacos源码 (1) 源码编译及idea环境

    本文介绍从gitee下载nacos源码,在本地编译,并导入idea进行本地调试. 从gitee下载源码 由于github访问速度慢,所以我选择使用gitee的镜像仓库: git clone https ...

  9. 非标准库--conio.h库

    1.getch函数 主要内容 int getch(void): 所在头文件:conio.h 函数用途:从控制台读取一个字符,但不显示在屏幕上,即一个不需要通过ENTER确定的getchar. 函数原型 ...

  10. JS - HTML精确定位

    scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离 scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最 ...