#abc240e. [abc240_e]Ranges on Tree
[abc240_e]Ranges on Tree
Problem Statement
You are given a rooted tree with vertices. The root is Vertex .
For each , the -th edge connects Vertex and Vertex .
For each , let denote the set of all vertices in the subtree rooted at Vertex . (Each vertex is in the subtree rooted at itself, that is, .)
Additionally, for integers and , let \[l, r\] denote the set of all integers between and , that is, $\[l, r\] = \\lbrace l, l+1, l+2, \\ldots, r \\rbrace$.
Consider a sequence of pairs of integers $\\big((L_1, R_1), (L_2, R_2), \\ldots, (L_N, R_N)\\big)$ that satisfies the conditions below.
- for every integer such that .
- The following holds for every pair of integers such that .
- \[L_i, R_i\] \\subseteq \[L_j, R_j\] if
- \[L_i, R_i\] \\cap \[L_j, R_j\] = \\emptyset if
It can be shown that there is at least one sequence $\\big((L_1, R_1), (L_2, R_2), \\ldots, (L_N, R_N)\\big)$. Among those sequences, print one that minimizes $\\max \\lbrace L_1, L_2, \\ldots, L_N, R_1, R_2, \\ldots, R_N \\rbrace$, the maximum integer used. (If there are multiple such sequences, you may print any of them.)
Constraints
- All values in input are integers.
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
Output
Print lines in the format below. That is, for each , the -th line should contain and separated by a space.
Sample Input 1
3
2 1
3 1
Sample Output 1
1 2
2 2
1 1
$(L_1, R_1) = (1, 2), (L_2, R_2) = (2, 2), (L_3, R_3) = (1, 1)$ satisfies the conditions.
Indeed, we have $\[L_2, R_2\] \\subseteq \[L_1, R_1\], \[L_3, R_3\] \\subseteq \[L_1, R_1\], \[L_2, R_2\] \\cap \[L_3, R_3\] = \\emptyset$.
Additionally, $\\max \\lbrace L_1, L_2, L_3, R_1, R_2, R_3 \\rbrace = 2$ is the minimum possible value.
Sample Input 2
5
3 4
5 4
1 2
1 4
Sample Output 2
1 3
3 3
2 2
1 2
1 1
Sample Input 3
5
4 5
3 2
5 2
3 1
Sample Output 3
1 1
1 1
1 1
1 1
1 1