102917: [Atcoder]ABC291 Ex - Balanced Tree

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

Description

Score : $600$ points

Problem Statement

You are given a tree $T$ with $N$ vertices. The $i$-th edge connects vertices $A_i$ and $B_i$.

Construct an $N$-vertex rooted tree $R$ that satisfies the following conditions.

  • For all integer pairs $(x,y)$ such that $1 \leq x < y \leq N$,
    • if the lowest common ancestor in $R$ of vertices $x$ and $y$ is vertex $z$, then vertex $z$ in $T$ is on the simple path between vertices $x$ and $y$.
  • In $R$, for all vertices $v$ except for the root, the number of vertices of the subtree rooted at $v$, multiplied by $2$, does not exceed the number of vertices of the subtree rooted at the parent of $v$.

We can prove that such a rooted tree always exists.

Constraints

  • $1 \leq N \leq 10^5$
  • $1\leq A_i,B_i \leq N$
  • All values in the input are integers.
  • The given graph is a tree.

Input

The input is given from Standard Input in the following format:

$N$
$A_1$ $B_1$
$\vdots$
$A_{N-1}$ $B_{N-1}$

Output

Let $R$ be a rooted tree that satisfies the conditions in the Problem Statement. Suppose that the parent of vertex $i$ in $R$ is vertex $p_i$. (If $i$ is the root, let $p_i=-1$.)
Print $N$ integers $p_1,\ldots,p_N$, with spaces in between.


Sample Input 1

4
1 2
2 3
3 4

Sample Output 1

2 -1 4 2

For example, the lowest common ancestor in $R$ of vertices $1$ and $3$ is vertex $2$; in $T$, vertex $2$ is on the simple path between vertices $1$ and $3$.
Also, for example, in $R$, the subtree rooted at vertex $4$ has two vertices, and the number multiplied by two does not exceed the number of vertices of the subtree rooted at vertex $2$, which has $4$ vertices.

Figure


Sample Input 2

5
1 2
1 3
1 4
1 5

Sample Output 2

-1 1 1 1 1

Input

题意翻译

平衡的树 给你一棵树 $T$,你要建一棵树 $R$,使其满足以下性质: - 任意两个点 $x,y$ 在 $R$ 中的最近公共祖先 $z$ 在 $T$ 中都位于 $x,y$ 之间的简单路径上。 - 对于任意一个非根的节点 $v$,以它为根的子树大小的两倍不得超过以它父亲为根的子树大小。 对于每个节点,输出它在 $R$ 中父亲的编号,根节点输出 `-1`。

Output

分数:600分 部分 问题描述 给定一个有N个顶点的树T。第i条边连接顶点A_i和B_i。 构造一个N个顶点的根树R,满足以下条件。 对于所有整数对(x,y),使得1≤x<y≤N, * 如果在R中,顶点x和y的最近公共祖先为顶点z,则在T中,顶点z在顶点x和y之间的简单路径上。 在R中,对于除了根以外的所有顶点v,以v为根的子树的顶点数乘以2不超过以v的父顶点为根的子树的顶点数。 我们可以证明这样的根树总是存在的。 部分 约束条件 * 1≤N≤10^5 * 1≤A_i,B_i≤N * 输入中的所有值都是整数。 * 给定的图是一棵树。 输入输出格式 输入格式: * 标准输入给出以下格式的输入: 输出格式: * 让R是一个满足问题描述中条件的根树。假设R中顶点i的父顶点是顶点p_i。(如果i是根,令p_i=-1)。 * 用空格分隔,打印p_1,...,p_N。 输入示例1 输出示例1 * 例如,在R中,顶点1和3的最近公共祖先为顶点2;在T中,顶点2在顶点1和3之间的简单路径上。 * 另外,例如,在R中,以顶点4为根的子树有两个顶点,乘以2的数字不超过以顶点2为根的子树的顶点数,该子树有4个顶点。 * Figure 输入示例2 输出示例2

加入题单

算法标签: