logo AlgoBeat OnlineJudge
登录 注册

#209438. [EC Final 2021] DFS Order

内存限制:1024 MiB 时间限制:3000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

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

Now he wants to start the depth-first search at the root. He wonders for each node , what is the minimum and the maximum position it can appear in the . The depth-first search order is the order of nodes visited during the depth-first search. A node appears in the -th () position in this order means it is visited after 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 , what are the minimum value and the maximum value of such that appears in the -th position.

Following is a pseudo-code for the depth-first search on a rooted tree. After its execution, 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 denoting the number of test cases.

For each test case, the first line contains an integer . Each of the next lines contains two integers and , indicating node is node 's parent (). These edges form a tree rooted at .

It is guaranteed that the sum of over all test cases is no more than .

输出格式

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

样例

样例输入 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