P3884 [JLOI2009]二叉树问题】的更多相关文章

目录 题目 思路 \(Code\) 题目 P3884 [JLOI2009]二叉树问题 思路 深搜统计深度,倍增\(\text{LCA}\)求边数 \(Code\) #include<iostream> #include<cstring> #include<string> #include<cstdio> #include<algorithm> #define MAXN 100 #define max_(a,b) a>b?a:b; using…
题目链接:https://www.luogu.org/problemnew/show/P3884 对方不想和你说话并向你扔了一个lca模板. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; const int maxlog = 10; const int maxn = 105; int n, m, s, wid…
--------------------- 链接:Miku --------------------- 这一道题只需要在倍增lca的板子上改一改就可以了. 宽度和深度可以在倍增lca的dfs预处理的时候判断一下就可以,至于最后问的两点之间的距离 首先需要求出两点公共祖先的位置,然后计算他们深度的差,并且按照题目要求分别处理即可 -------------------- #include<iostream> #include<cstdio> using namespace std;…
题目描述 如下图所示的一棵二叉树的深度.宽度及结点间距离分别为: 深度:4 宽度:4(同一层最多结点个数) 结点间距离: ⑧→⑥为8 (3×2+2=8) ⑥→⑦为3 (1×2+1=3) 注:结点间距离的定义:由结点向根方向(上行方向)时的边数×2, 与由根向叶结点方向(下行方向)时的边数之和. 输入输出格式 输入格式: 输入文件第一行为一个整数n(1≤n≤100),表示二叉树结点个数.接下来的n-1行,表示从结点x到结点y(约定根结点为1),最后一行两个整数u.v,表示求从结点u到结点v的距离.…
题面 题解 这道题目可以用很多方法解决,这里我使用的是树链剖分. 关于树链剖分,可以看一下我的树链剖分学习笔记. 大致思路是这样的: 第\(1\)次\(dfs\)记录出每个点的父亲.重儿子.深度.子树大小: 第\(2\)次\(dfs\)优先遍历重儿子,记录出点所在链的链顶和重新遍历后的\(dfs\)序: 计算出最大的深度及宽度: 树链剖分求\(\texttt{LCA}\)并计算点对之间的距离. 具体实现还要注意细节. 代码 #include <iostream> #include <cs…
嘟嘟嘟 对于求深度和宽度都很好维护.深度dfs时维护就行,宽度统计同一个深度的节点有多少个,然后取max. 对于求距离,我刚开始以为是要走到根节点在回来,然后固输了(dep[u] - 1) * 2 + dep[v] - 1,结果竟然得了80分,数据有点水过头了…… 实际上就是求LCA,然而因为只求一次,所以暴力就行啦~~ #include<cstdio> #include<iostream> #include<algorithm> #include<cmath&g…
洛谷 P3884 [JLOI2009]二叉树问题 洛谷传送门 JDOJ 2024: [JLOI2009]二叉树问题 JDOJ传送门 Description 如下图所示的一棵二叉树的深度.宽度及结点间距离分别为: 深度:4 宽度:4(同一层最多结点个数) 结点间距离: ⑧→⑥为8 (3×2+2=8) ​ ⑥→⑦为3 (1×2+1=3) 注:结点间距离的定义:由结点向根方向(上行方向)时的边数×2, 与由根向叶结点方向(下行方向)时的边数之和. Input 输入文件第一行为一个整数n(1≤n≤100…
题目描述 如下图所示的一棵二叉树的深度.宽度及结点间距离分别为: 深度:\(4\) 宽度:\(4\)(同一层最多结点个数) 结点间距离: \(⑧→⑥为8 (3×2+2=8)\) \(⑥→⑦为3 (1×2+1=3)\) 注:结点间距离的定义:由结点向根方向(上行方向)时的边数\(×2\), 与由根向叶结点方向(下行方向)时的边数之和. 输入输出格式 输入格式: 输入文件第一行为一个整数\(n(1≤n≤100)\),表示二叉树结点个数.接下来的\(n-1\)行,表示从结点\(x\)到结点\(y\)(…
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现.由于篇幅有限,此处仅作一般介绍(如果想要完全了解二叉树以及其衍生出的各种算法,恐怕要写8~10篇). 1)二叉树(Binary Tree) 顾名思义,就是一个节点分出两个节点,称其为左右子节点:每个子节点又可以分出两个子节点,这样递归分叉,其形状很像一颗倒着的树.二叉树限制了每个节点最多有两个子节…
这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二段的内容. 下面是代码,很简单,耐心看看就懂了. package Construct; public class ConstructTree { private int count = 0; class Node { int i; Node left; Node right; public Node…
简单的通过一个寻找嫌疑人的小程序 来演示二叉树的使用 #include <stdio.h> #include <stdlib.h> #include <string.h> /** * 数据结构 - 二叉树 - 节点 */ typedef struct node { char *querstion; struct node *no; struct node *yes; }node; /** * 方法 输入和输入 * 根据打印,询问答案是否正确,y是正确 */ int ye…
题目如下: 题目给出的例子不太好,容易让人误解成不断顺着右节点访问就好了,但是题目意思并不是这样. 换成通俗的意思:按层遍历二叉树,输出每层的最右端结点. 这就明白时一道二叉树层序遍历的问题,用一个队列来处理,但是问题是怎么来辨别每层的最右端结点,我思考了半天,最后想出的办法是利用一个标记位,例如上面的例子: q代表队列,f代表标记结点,right代表记录的最右端结点 q: 1 flag right:{} q: flag 2 3 遇到标记位所以移动标记位,并将队头弹出的数据存起来如下 q: 2…
基于python的list实现二叉树 #!/usr/bin/env python # -*- coding:utf-8 -*- class BinTreeValueError(ValueError): pass class BinTreeList(object): def __init__(self, data, left = None, right = None): self.btree = [data, left, right] #判断二叉树是否为空 def is_empty_bintree…
You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to…
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Example: Given binary tree 1 / \ 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Remove the leaves [4, 5, 3] from the…
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # # For…
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). If two nodes are in the same row and column, the order should be from left to right. Examples: Given binary tree [3,9,20,null,n…
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p…
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput…
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 这道题给我们一个二叉树,让我们返回所有根到叶节点的路径,跟之前那道Path Sum II 二叉树路径之和之二很类似,比那道稍微简单一…