atcoder#ABC240E. [ABC240E] Ranges on Tree
[ABC240E] Ranges on Tree
Score : points
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 denote the set of all integers between and , that is, .
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 .- if
- 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.
3
2 1
3 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.
5
3 4
5 4
1 2
1 4
1 3
3 3
2 2
1 2
1 1
5
4 5
3 2
5 2
3 1
1 1
1 1
1 1
1 1
1 1