#abc291h. Ex - Balanced Tree

Ex - Balanced Tree

Score : 600600 points

问题陈述

给定一棵包含 NN 个顶点的树 TT,其中第 ii 条边连接顶点 AiA_iBiB_i

构建一棵拥有 NN 个顶点的有根树 RR,满足以下条件:

  • 对于所有满足 1x<yN1 \leq x < y \leq N 的整数对 (x,y)(x,y)
    • 如果在树 RR 中顶点 xxyy 的最低公共祖先为顶点 zz,则在树 TT 中顶点 zz 位于顶点 xxyy 之间的简单路径上。
  • 在树 RR 中,对于除了根节点以外的所有顶点 vv,以 vv 为根的子树的顶点数量乘以 22 后,不超过以 vv 的父节点为根的子树的顶点数量。

我们可以证明,这样的有根树总是存在的。

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

Problem Statement

You are given a tree TT with NN vertices. The ii-th edge connects vertices AiA_i and BiB_i.

Construct an NN-vertex rooted tree RR that satisfies the following conditions.

  • For all integer pairs (x,y)(x,y) such that 1x<yN1 \leq x < y \leq N,
    • if the lowest common ancestor in RR of vertices xx and yy is vertex zz, then vertex zz in TT is on the simple path between vertices xx and yy.
  • In RR, for all vertices vv except for the root, the number of vertices of the subtree rooted at vv, multiplied by 22, does not exceed the number of vertices of the subtree rooted at the parent of vv.

We can prove that such a rooted tree always exists.

Constraints

  • 1N1051 \leq N \leq 10^5
  • 1Ai,BiN1\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:

NN

A1A_1 B1B_1

\vdots

AN1A_{N-1} BN1B_{N-1}

Output

Let RR be a rooted tree that satisfies the conditions in the Problem Statement. Suppose that the parent of vertex ii in RR is vertex pip_i. (If ii is the root, let pi=1p_i=-1.)
Print NN integers p1,,pNp_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 RR of vertices 11 and 33 is vertex 22; in TT, vertex 22 is on the simple path between vertices 11 and 33.
Also, for example, in RR, the subtree rooted at vertex 44 has two vertices, and the number multiplied by two does not exceed the number of vertices of the subtree rooted at vertex 22, which has 44 vertices.

Figure

Sample Input 2

5
1 2
1 3
1 4
1 5

Sample Output 2

-1 1 1 1 1

update @ 2024/3/10 12:10:13