102404: [AtCoder]ABC240 E - Ranges on Tree

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $500$ points

Problem Statement

You are given a rooted tree with $N$ vertices. The root is Vertex $1$.
For each $i = 1, 2, \ldots, N-1$, the $i$-th edge connects Vertex $u_i$ and Vertex $v_i$.

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

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

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

  • $1 \leq L_i \leq R_i$ for every integer $i$ such that $1 \leq i \leq N$.
  • The following holds for every pair of integers $(i, j)$ such that $1 \leq i, j \leq N$.
    • $[L_i, R_i] \subseteq [L_j, R_j]$ if $S_i \subseteq S_j$
    • $[L_i, R_i] \cap [L_j, R_j] = \emptyset$ if $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

  • $2 \leq N \leq 2 \times 10^5$
  • $1 \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:

$N$
$u_1$ $v_1$
$u_2$ $v_2$
$\vdots$
$u_{N-1}$ $v_{N-1}$

Output

Print $N$ lines in the format below. That is, for each $i = 1, 2, \ldots, N$, the $i$-th line should contain $L_i$ and $R_i$ separated by a space.

$L_1$ $R_1$
$L_2$ $R_2$
$\vdots$
$L_N$ $R_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

Input

Output

分数:500分

问题描述

你得到了一个有N个顶点的根树,根顶点是顶点1。

对于每个$i = 1, 2, \ldots, N-1$,第$i$条边连接顶点$u_i$和顶点$v_i$。

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

此外,对于整数$l$和$r$,令$[l, r]$表示所有在$l$和$r$之间的整数的集合,即$[l, r] = \lbrace l, l+1, l+2, \ldots, r \rbrace$。

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

  • 对于每个整数$i$,满足$1 \leq L_i \leq R_i$,其中$1 \leq i \leq N$。
  • 对于每对整数$(i, j)$,满足$1 \leq i, j \leq N$,有以下情况:
    • 如果$S_i \subseteq S_j$,则$[L_i, R_i] \subseteq [L_j, R_j]$
    • 如果$S_i \cap S_j = \emptyset$,则$[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$最小的序列,即使用的最大整数。(如果有多个这样的序列,你可以输出其中任何一个)。

约束

  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq u_i, v_i \leq N$
  • 输入中的所有值都是整数。
  • 给定的图是一棵树。

输入

输入从标准输入按以下格式给出:

$N$
$u_1$ $v_1$
$u_2$ $v_2$
$\vdots$
$u_{N-1}$ $v_{N-1}$

输出

按以下格式输出$N$行。即对于每个$i = 1, 2, \ldots, N$,第$i$行应包含由空格分隔的$L_i$和$R_i$。

$L_1$ $R_1$
$L_2$ $R_2$
$\vdots$
$L_N$ $R_N$

样例输入1

3
2 1
3 1

样例输出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)$满足条件。

实际上,我们有$[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$。

此外,$\max \lbrace L_1, L_2, L_3, R_1, R_2, R_3 \rbrace = 2$是最小可能的值。


样例输入2

5
3 4
5 4
1 2
1 4

样例输出2

1 3
3 3
2 2
1 2
1 1

样例输入3

5
4 5
3 2
5 2
3 1

样例输出3

1 1
1 1
1 1
1 1
1 1

加入题单

算法标签: