这是用户在 2024-4-7 21:32 为 https://www.luogu.com.cn/problem/P9872 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

P9872 [EC Final 2021] DFS Order

提交 1.15k
通过 381
时间限制 3.00s
内存限制 1.00GB

标签

查看算法标签
进入讨论版

相关讨论

查看讨论

推荐题目

暂无
 洛谷推荐

题目描述

Prof. Pang has a rooted tree which is rooted at 11 with nn nodes. These nn nodes are numbered from 11 to nn.

Now he wants to start the depth-first search at the root. He wonders for each node vv, what is the minimum and the maximum position it can appear in the depth-first search order\textbf{depth-first search order}. The depth-first search order is the order of nodes visited during the depth-first search. A node appears in the jj-th (1jn1\le j\le n) position in this order means it is visited after j1j-1 other nodes. Because sons of a node can be iterated in arbitrary order, multiple possible depth-first orders exist. Prof. Pang wants to know for each node vv, what are the minimum value and the maximum value of jj such that vv appears in the jj-th position.

Following is a pseudo-code for the depth-first search on a rooted tree. After its execution, dfs_order\texttt{dfs\_order} is the depth-first search order.

源代码
复制

let dfs_order be an empty list

def dfs(vertex x):
    append x to the end of dfs_order.
    for (each son y of x): // sons can be iterated in arbitrary order.
        dfs(y)

dfs(root)

输入格式

The first line contains a single integer T (1T106)T~(1 \le T \le 10^6) denoting the number of test cases.

For each test case, the first line contains an integer n (1n105)n~(1 \le n \le 10 ^ 5). Each of the next n1n-1 lines contains two integers xx and yy, indicating node xx is node yy's parent (1x,yn1\le x, y\le n). These edges form a tree rooted at 11.

It is guaranteed that the sum of nn over all test cases is no more than 10610^6.

输出格式

For each test case, print nn lines. The ii-th line contains two integers denoting the minimum and the maximum position node ii can appear in the depth-first search order.

题意翻译

给定一棵以 11 为根节点的树,若按任意顺序进行深度优先搜索,则第 ii 个点最小是第几个被搜到的以及最大是第几个?

输入输出样例

输入 #1
2
4
1 2
2 3
3 4
5
1 2
2 3
2 4
1 5
输出 #1
1 1
2 2
3 3
4 4
1 1
2 3
3 5
3 5
2 5