#abc291h. Ex - Balanced Tree
Ex - Balanced Tree
Score : points
问题陈述
给定一棵包含 个顶点的树 ,其中第 条边连接顶点 和 。
构建一棵拥有 个顶点的有根树 ,满足以下条件:
- 对于所有满足 的整数对 ,
- 如果在树 中顶点 和 的最低公共祖先为顶点 ,则在树 中顶点 位于顶点 和 之间的简单路径上。
- 在树 中,对于除了根节点以外的所有顶点 ,以 为根的子树的顶点数量乘以 后,不超过以 的父节点为根的子树的顶点数量。
我们可以证明,这样的有根树总是存在的。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given a tree with vertices. The -th edge connects vertices and .
Construct an -vertex rooted tree that satisfies the following conditions.
- For all integer pairs such that ,
- if the lowest common ancestor in of vertices and is vertex , then vertex in is on the simple path between vertices and .
- In , for all vertices except for the root, the number of vertices of the subtree rooted at , multiplied by , does not exceed the number of vertices of the subtree rooted at the parent of .
We can prove that such a rooted tree always exists.
Constraints
- 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:
Output
Let be a rooted tree that satisfies the conditions in the Problem Statement. Suppose that the parent of vertex in is vertex . (If is the root, let .)
Print integers , 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 of vertices and is vertex ; in , vertex is on the simple path between vertices and .
Also, for example, in , the subtree rooted at vertex has two vertices, and the number multiplied by two does not exceed the number of vertices of the subtree rooted at vertex , which has vertices.
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