#abc240e. E - Ranges on Tree

E - Ranges on Tree

Score : 500500 points

问题描述

给定一棵有 NN 个顶点的根树,其中根节点为顶点 11
对于每个 i=1,2,,N1i = 1, 2, \ldots, N-1,第 ii 条边连接顶点 uiu_i 和顶点 viv_i

对于每个 i=1,2,,Ni = 1, 2, \ldots, N,令 SiS_i 表示以顶点 ii 为根的子树中所有顶点的集合。(每个顶点都在以其自身为根的子树中,即 iSii \in S_i。)

另外,对于整数 llrr,令 [l,r][l, r] 表示从 llrr 之间的所有整数集合,即 [l,r]={l,l+1,l+2,,r}[l, r] = \lbrace l, l+1, l+2, \ldots, r \rbrace

考虑一个由 NN 对整数组成的序列 $\big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big)$,满足以下条件:

  • 对于每一个整数 ii(满足 1iN1 \leq i \leq N),都有 1LiRi1 \leq L_i \leq R_i
  • 对于每一对整数 (i,j)(i, j)(满足 1i,jN1 \leq i, j \leq N),满足以下情况:
    • SiSjS_i \subseteq S_j,则有 [Li,Ri][Lj,Rj][L_i, R_i] \subseteq [L_j, R_j]
    • SiSj=S_i \cap S_j = \emptyset,则有 [Li,Ri][Lj,Rj]=[L_i, R_i] \cap [L_j, R_j] = \emptyset

可以证明至少存在一个序列 $\big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big)$。在这些序列中,找出并输出一个使得 $\max \lbrace L_1, L_2, \ldots, L_N, R_1, R_2, \ldots, R_N \rbrace$ 达到最小值的序列,即使用的最大整数最小化。(如果有多个这样的序列,你可以任选其中一个打印。)

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a rooted tree with NN vertices. The root is Vertex 11.
For each i=1,2,,N1i = 1, 2, \ldots, N-1, the ii-th edge connects Vertex uiu_i and Vertex viv_i.

For each i=1,2,,Ni = 1, 2, \ldots, N, let SiS_i denote the set of all vertices in the subtree rooted at Vertex ii. (Each vertex is in the subtree rooted at itself, that is, iSii \in S_i.)

Additionally, for integers ll and rr, let [l,r][l, r] denote the set of all integers between ll and rr, that is, [l,r]={l,l+1,l+2,,r}[l, r] = \lbrace l, l+1, l+2, \ldots, r \rbrace.

Consider a sequence of NN pairs of integers $\big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big)$ that satisfies the conditions below.

  • 1LiRi1 \leq L_i \leq R_i for every integer ii such that 1iN1 \leq i \leq N.
  • The following holds for every pair of integers (i,j)(i, j) such that 1i,jN1 \leq i, j \leq N.
    • [Li,Ri][Lj,Rj][L_i, R_i] \subseteq [L_j, R_j] if SiSjS_i \subseteq S_j
    • [Li,Ri][Lj,Rj]=[L_i, R_i] \cap [L_j, R_j] = \emptyset if SiSj=S_i \cap S_j = \emptyset

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

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1ui,viN1 \leq u_i, v_i \leq N
  • All values in input are integers.
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

NN

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uN1u_{N-1} vN1v_{N-1}

Output

Print NN lines in the format below. That is, for each i=1,2,,Ni = 1, 2, \ldots, N, the ii-th line should contain LiL_i and RiR_i separated by a space.

L1L_1 R1R_1

L2L_2 R2R_2

\vdots

LNL_N RNR_N

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

update @ 2024/3/10 10:21:31