atcoder#ABC291H. [ABC291Ex] Balanced Tree
[ABC291Ex] Balanced Tree
Score : points
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 .
- 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.
4
1 2
2 3
3 4
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.
5
1 2
1 3
1 4
1 5
-1 1 1 1 1