#P8977. 「DTOI-4」行走
「DTOI-4」行走
Background
Xiao L feels bored, so he wants to walk on a tree.
Problem Description
Xiao L has a tree with nodes. Each node has a weight, and the weight of node is .
He does not like strange weights, so he guarantees that must be one of .
He thinks walking on a tree is interesting, so he wants to find a path on this tree that satisfies the following conditions:
- is a simple directed path that can be empty.
- Let the nodes visited by in order be . The path you find must satisfy .
- Let $f(P) = \displaystyle\sum_{i = 1}^{|P|} \frac{a_{P_i}}{2^{i - 1}}$. The path you find must maximize .
- Under the condition that is maximized, the path you find must also have the smallest lexicographical order.
Please find a path that satisfies the conditions above.
Definition of lexicographical order in this problem:
Given two paths to compare .
- If there exists such that and , then we say that is lexicographically smaller than .
- If there exists such that and , then we say that is lexicographically larger than .
- If and , then we say that is lexicographically smaller than .
- If and , then we say that is lexicographically larger than .
Input Format
The first line contains an integer .
The second line contains integers .
The next lines each contain two integers , representing an edge in the tree.
Output Format
Output one line with integers, representing the nodes visited by the path in order.
In particular, if is an empty path, do not output anything.
5
1 0 -1 1 -1
1 2
2 3
2 4
1 5
1 2 4
Hint
Sample #1 Explanation

When , we have . It can be proven that there is no better .
Constraints
| Dependencies | Score | |||
|---|---|---|---|---|
| No special restrictions | None | |||
| Same as above | ||||
| No special restrictions | None | |||
| Same as above | No special restrictions |
For of the testdata, , , . It is guaranteed that the given edges form an unrooted tree.
Translated by ChatGPT 5