CF - 1110F Nearest Leaf】的更多相关文章

题目传送门 题解: 先用题目给定的dfs方式得到dfs序,记录下出入的dfs序. 很明显可以得知的是,以u为根的子树的dfs序在 in[u] - out[u] 的范围之内. 将每个询问先全部存到对应的节点上. 然后我们以1为root,先求出每个叶子节点到1的距离. 对1的询问查询更新完答案之后. (假设1和2之间有一条权值为w的边) 那么以2为根的话,就相当于是 in[2] --- out[2] 区间里面的所有值的距离 -= w, 其他点的距离 += w. 然后对于每个点都这样处理. 注意修改和…
我--又诈尸了-- 代码几乎都不会写了,打场CF居然上分啦,开心!(虽然还是比不过列表里的各路神仙) 题目链接 题目描述 一棵\(n\)个点的有根树,规定一种dfs序(规则:编号小的点优先dfs),\(m\)次询问一个点\(u\)和一个区间\([l, r]\),求dfs序在这个区间内的叶子中,到\(u\)最小的距离. \(n, m \le 500000\) 题解 这题--很简单-- 题面一上来给个什么欧拉遍历定义--我吓得比赛中没看这题--(实际上码量对于代码几乎都不会敲的退役选手来说,不是非常…
题目链接 \(dls\)讲过这道题,所以这不是线段树裸题吗,这场没打气气气气气=-= 现在是写着玩=v= \(Description\) 给定一棵\(n\)个点的树.\(q\)次询问,每次询问给定\(v,l,r\),求离\(v\)最近且DFS序在\([l,r]\)之间的叶节点是哪个. \(n,q\leq5\times10^5\). \(Solution\) 把询问离线. 在树上走,每次从\(fa[x]\)走到\(x\)时,设边权为\(len\).此时距离\(x\)子树外的点的距离会增加\(len…
Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of a tree (a connected undirected graph without cycles) as follows: consider a depth-first search algorithm which traverses vertices of the tree and enu…
F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离<=k. 分析: 可以证明,对于一个子树内的两个叶子节点,把它们分到同一个集合中一定比分到两个集合中好. 所以一个子树内的叶子集合,能合并,尽量合并. 从叶子节点dfs,往上推,当到达一个点时,求出这棵子树内所有的叶子集合到这个节点的距离.如果两个距离小于等于k,那么显然是可以合并的.合并后,只返回这棵子…
传送门 这是我第二次看见这个题目了,第一次看见是另一场比赛的A题,想了一个小时不会写就弃了 从来没想过这个题能这么玩 线段树上记录根到叶子节点的距离 初始线段树上先记下根节点1到各叶子节点的距离 先离线,然后dfs整颗树,在dfs过程中考虑根的移动对线段树的影响 如果当前点是查询点,在当前线段树上查询 代码: #include<cstdio> #include<iostream> #include<algorithm> using namespace std; void…
Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree. Here, nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf of th…
[抄题]: Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree. Here, nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf…
Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree. Here, nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf of th…
原题链接在这里:https://leetcode.com/problems/closest-leaf-in-a-binary-tree/ 题目: Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree. Here, nearest to a leaf means the l…