CF2195E.Idiot First Search
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There is a binary tree of n+1 vertices (n is odd), with vertices labeled 0,1,…,n. At most one letter can be written on each vertex of the tree, and all vertices initially have nothing written on them. The root of the tree is vertex 0.
In the tree, vertex 0 is the parent of vertex 1, while all other vertices have either 2 children or 0 children.
Bob is lost in one vertex of the tree and wishes to escape the tree by reaching vertex 0. This is very easy for most people with common sense. However, since Bob is an idiot, he created a new way of traversing the tree; introducing the "Idiot First Search".
When Bob is on vertex v (1≤v≤n), Bob's movement is determined as follows:
- If vertex v is a leaf, Bob always moves to the parent of v; otherwise, check the next few conditions.
- If nothing is written on vertex v, Bob writes 'L' on vertex v and moves to the left child of v;
- If 'L' is written on vertex v, Bob overwrites it to 'R' and moves to the right child of v;
- If 'R' is written on vertex v, Bob erases it and moves to the parent of v.
It takes exactly 1 second for Bob to move to an adjacent vertex, so Bob will take exactly x seconds to perform x moves.
It has been shown that regardless of which vertex Bob starts on, Bob can reach vertex 0 in a finite (though possibly inexplicably large) amount of time. We don't know who proved it; surely it can't be Bob, but it is definitely proven.
For each vertex k=1,2,…,n, please determine the total time it takes to reach vertex 0 if Bob started on vertex k, in seconds. As the values may be huge, you are only asked to compute them modulo 109+7.
存在一棵含有 n+1 个顶点(其中 n 为奇数)的二叉树,顶点编号为 0,1,…,n。每个顶点上至多可写一个字母,且所有顶点初始时均为空。该树的根节点为顶点 0。
在该树中,顶点 0 是顶点 1 的父节点;其余所有顶点要么有恰好 2 个子节点,要么没有子节点(即为叶子节点)。
Bob 迷失在树中的某个顶点上,他希望逃出这棵树,即到达顶点 0。对大多数具备常识的人来说,这非常简单。然而,由于 Bob 是个傻瓜,他发明了一种全新的树遍历方式——“傻瓜优先搜索”(Idiot First Search)。
当 Bob 位于顶点 v(其中 1≤v≤n)时,其移动规则如下:
- 若顶点 v 是叶子节点,则 Bob 总是移向 v 的父节点;否则,继续检查以下条件。
- 若顶点 v 上尚未写入任何字母,则 Bob 在 v 上写入字母
'L',并移向 v 的左子节点; - 若顶点 v 上已写有
'L',则 Bob 将其覆盖为'R',并移向 v 的右子节点; - 若顶点 v 上已写有
'R',则 Bob 将其擦除,并移向 v 的父节点。
Bob 每次移动到相邻顶点恰好耗时 1 秒,因此执行 x 次移动共耗时 x 秒。
已证明:无论 Bob 从哪个顶点出发,他总能在有限(尽管可能大得离谱)的时间内到达顶点 0。我们不知道是谁证明了这一点;显然不可能是 Bob,但该结论确已被证实。
对每个顶点 k=1,2,…,n,请计算 Bob 从顶点 k 出发到达顶点 0 所需的总时间(单位:秒)。由于结果可能极大,你只需输出其对 109+7 取模后的值。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤300001, n is odd).
Each of the next n lines contains two integers li and ri denoting the children of vertex i (0≤li,ri≤n).
For each vertex, li=ri=0 is given if the vertex has no children. Otherwise, li and ri are the left and right children of vertex i.
It is guaranteed that the input defines a valid binary tree satisfying the conditions given in the statement.
It is guaranteed that the sum of n over all test cases does not exceed 300001.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤300001,且 n 为奇数)。
接下来的 n 行中,每行包含两个整数 li 和 ri,分别表示顶点 i 的左子节点和右子节点(0≤li,ri≤n)。
对于每个顶点,若其无子节点,则给出 li=ri=0;否则,li 和 ri 分别为其左子节点和右子节点。
保证输入所定义的是一棵满足题目所述条件的有效二叉树。
保证所有测试用例的 n 值之和不超过 300001。
输出格式
For each test case, output n integers τ1,τ2,…,τn separated by spaces.
Here, τk denotes the total time it takes to reach vertex 0 if Bob started on vertex k, modulo 109+7.
对于每个测试用例,输出 n 个整数 τ1,τ2,…,τn,以空格分隔。
其中,τk 表示若 Bob 从顶点 k 出发,到达顶点 0 所需的总时间(对 109+7 取模)。
输入输出样例
输入#1
3 1 0 0 5 2 3 0 0 4 5 0 0 0 0 7 2 3 4 5 0 0 6 7 0 0 0 0 0 0
输出#1
1 9 10 14 15 15 13 22 14 27 23 28 28
说明/提示
On the first test case, there are only two vertices, vertex 0 and vertex 1. It takes only 1 second for Bob to reach vertex 0 from vertex 1.
On the second test case, the tree is given as follows.

It takes 14 seconds for Bob to reach vertex 0 from vertex 3. The moves are as follows:
3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttL2xrightarrowmathttX1xrightarrowmathttR3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttX0
Here, the letters above the arrows denote the letter on the vertex before moving to the adjacent vertex, where X denotes nothing written.
在第一个测试用例中,图中仅有两个顶点:顶点 0 和顶点 1。Bob 从顶点 1 到达顶点 0 仅需 1 秒。
在第二个测试用例中,树的结构如下所示。

Bob 从顶点 3 到达顶点 0 需要 14 秒。具体移动过程如下:
3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttL2xrightarrowmathttX1xrightarrowmathttR3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttX0
其中,箭头上的字母表示移动至相邻顶点前所在顶点上所写的字母;X 表示该顶点上未写任何字母。
输入解题思路,AI测评打分。不知道怎么写?