CF2195E.Idiot First Search

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a binary tree of n+1n+1 vertices (nn is odd), with vertices labeled 0,1,…,n0,1,\ldots,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 00.

In the tree, vertex 00 is the parent of vertex 11, while all other vertices have either 22 children or 00 children.

Bob is lost in one vertex of the tree and wishes to escape the tree by reaching vertex 00. 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 vv (1≤v≤n1 \le v \le n), Bob's movement is determined as follows:

  • If vertex vv is a leaf, Bob always moves to the parent of vv; otherwise, check the next few conditions.
  • If nothing is written on vertex vv, Bob writes 'L' on vertex vv and moves to the left child of vv;
  • If 'L' is written on vertex vv, Bob overwrites it to 'R' and moves to the right child of vv;
  • If 'R' is written on vertex vv, Bob erases it and moves to the parent of vv.

It takes exactly 11 second for Bob to move to an adjacent vertex, so Bob will take exactly xx seconds to perform xx moves.

It has been shown that regardless of which vertex Bob starts on, Bob can reach vertex 00 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,…,nk=1,2,\ldots,n, please determine the total time it takes to reach vertex 00 if Bob started on vertex kk, in seconds. As the values may be huge, you are only asked to compute them modulo 109+710^9+7.

存在一棵含有 n+1n+1 个顶点(其中 nn 为奇数)的二叉树,顶点编号为 0,1,…,n0,1,\ldots,n。每个顶点上至多可写一个字母,且所有顶点初始时均为空。该树的根节点为顶点 00。

在该树中,顶点 00 是顶点 11 的父节点;其余所有顶点要么有恰好 22 个子节点,要么没有子节点(即为叶子节点)。

Bob 迷失在树中的某个顶点上,他希望逃出这棵树,即到达顶点 00。对大多数具备常识的人来说,这非常简单。然而,由于 Bob 是个傻瓜,他发明了一种全新的树遍历方式——“傻瓜优先搜索”(Idiot First Search)。

当 Bob 位于顶点 vv(其中 1≤v≤n1 \le v \le n)时,其移动规则如下:

  • 若顶点 vv 是叶子节点,则 Bob 总是移向 vv 的父节点;否则,继续检查以下条件。
  • 若顶点 vv 上尚未写入任何字母,则 Bob 在 vv 上写入字母 'L',并移向 vv 的左子节点;
  • 若顶点 vv 上已写有 'L',则 Bob 将其覆盖为 'R',并移向 vv 的右子节点;
  • 若顶点 vv 上已写有 'R',则 Bob 将其擦除,并移向 vv 的父节点。

Bob 每次移动到相邻顶点恰好耗时 11 秒,因此执行 xx 次移动共耗时 xx 秒。

已证明:无论 Bob 从哪个顶点出发,他总能在有限(尽管可能大得离谱)的时间内到达顶点 00。我们不知道是谁证明了这一点;显然不可能是 Bob,但该结论确已被证实。

对每个顶点 k=1,2,…,nk = 1,2,\ldots,n,请计算 Bob 从顶点 kk 出发到达顶点 00 所需的总时间(单位:秒)。由于结果可能极大,你只需输出其对 109+710^9+7 取模后的值。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤300 0011 \le n \le 300\,001, nn is odd).

Each of the next nn lines contains two integers lil_i and rir_i denoting the children of vertex ii (0≤li,ri≤n0 \le l_i,r_i \le n).

For each vertex, li=ri=0l_i=r_i=0 is given if the vertex has no children. Otherwise, lil_i and rir_i are the left and right children of vertex ii.

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 nn over all test cases does not exceed 300 001300\,001.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤300 0011 \le n \le 300\,001,且 nn 为奇数)。

接下来的 nn 行中,每行包含两个整数 lil_i 和 rir_i,分别表示顶点 ii 的左子节点和右子节点(0≤li,ri≤n0 \le l_i,r_i \le n)。

对于每个顶点,若其无子节点,则给出 li=ri=0l_i = r_i = 0;否则,lil_i 和 rir_i 分别为其左子节点和右子节点。

保证输入所定义的是一棵满足题目所述条件的有效二叉树。

保证所有测试用例的 nn 值之和不超过 300 001300\,001。

输出格式

For each test case, output nn integers τ1,τ2,…,τn\tau_1,\tau_2,\ldots,\tau_n separated by spaces.

Here, τk\tau_k denotes the total time it takes to reach vertex 00 if Bob started on vertex kk, modulo 109+710^9+7.

对于每个测试用例,输出 nn 个整数 τ1,τ2,…,τn\tau_1,\tau_2,\ldots,\tau_n,以空格分隔。

其中,τk\tau_k 表示若 Bob 从顶点 kk 出发,到达顶点 00 所需的总时间(对 109+710^9+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 00 and vertex 11. It takes only 11 second for Bob to reach vertex 00 from vertex 11.

On the second test case, the tree is given as follows.

It takes 1414 seconds for Bob to reach vertex 00 from vertex 33. The moves are as follows:

3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttL2xrightarrowmathttX1xrightarrowmathttR3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttX03 \\xrightarrow{\\mathtt{L}} 4 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{R}} 5 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{L}} 2 \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{R}} 3 \\xrightarrow{\\mathtt{L}} 4 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{R}} 5 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{X}} 0

Here, the letters above the arrows denote the letter on the vertex before moving to the adjacent vertex, where X\mathtt{X} denotes nothing written.

在第一个测试用例中,图中仅有两个顶点:顶点 00 和顶点 11。Bob 从顶点 11 到达顶点 00 仅需 11 秒。

在第二个测试用例中,树的结构如下所示。

Bob 从顶点 33 到达顶点 00 需要 1414 秒。具体移动过程如下:

3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttL2xrightarrowmathttX1xrightarrowmathttR3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttX03 \\xrightarrow{\\mathtt{L}} 4 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{R}} 5 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{L}} 2 \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{R}} 3 \\xrightarrow{\\mathtt{L}} 4 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{R}} 5 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{X}} 0

其中,箭头上的字母表示移动至相邻顶点前所在顶点上所写的字母;X\mathtt{X} 表示该顶点上未写任何字母。

输入解题思路,AI测评打分。不知道怎么写?

首页