#P8977. 「DTOI-4」行走

    ID: 9973 远端评测题 500ms 128MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>数学贪心2023洛谷原创O2优化深度优先搜索 DFS

「DTOI-4」行走

Background

Xiao L feels bored, so he wants to walk on a tree.

Problem Description

Xiao L has a tree with nn nodes. Each node has a weight, and the weight of node ii is aia_i.

He does not like strange weights, so he guarantees that aia_i must be one of 1,0,1-1, 0, 1.

He thinks walking on a tree is interesting, so he wants to find a path PP on this tree that satisfies the following conditions:

  • PP is a simple directed path that can be empty.
  • Let the nodes visited by PP in order be P1,P2,,PPP_1, P_2, \cdots, P_{|P|}. The path you find must satisfy P1=1P_1 = 1.
  • Let $f(P) = \displaystyle\sum_{i = 1}^{|P|} \frac{a_{P_i}}{2^{i - 1}}$. The path you find must maximize f(P)f(P).
  • Under the condition that f(P)f(P) is maximized, the path you find must also have the smallest lexicographical order.

Please find a path PP that satisfies the conditions above.


Definition of lexicographical order in this problem:

Given two paths to compare PQP \neq Q.

  • If there exists 1imin(P,Q)1 \leq i \leq \min(|P|, |Q|) such that 1j<i,Pj=Qj\forall 1 \leq j < i, P_j = Q_j and Pi<QiP_i < Q_i, then we say that PP is lexicographically smaller than QQ.
  • If there exists 1imin(P,Q)1 \leq i \leq \min(|P|, |Q|) such that 1j<i,Pj=Qj\forall 1 \leq j < i, P_j = Q_j and Pi>QiP_i > Q_i, then we say that PP is lexicographically larger than QQ.
  • If 1imin(P,Q),Pi=Qi\forall 1 \leq i \leq \min(|P|, |Q|), P_i = Q_i and P<Q|P| < |Q|, then we say that PP is lexicographically smaller than QQ.
  • If 1imin(P,Q),Pi=Qi\forall 1 \leq i \leq \min(|P|, |Q|), P_i = Q_i and P>Q|P| > |Q|, then we say that PP is lexicographically larger than QQ.

Input Format

The first line contains an integer nn.

The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n.

The next n1n - 1 lines each contain two integers u,vu, v, representing an edge in the tree.

Output Format

Output one line with P|P| integers, representing the nodes visited by the path PP in order.

In particular, if PP 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 P=[1,2,4]P = [1, 2, 4], we have f(P)=1+0+14=54f(P) = 1 + 0 + \frac{1}{4} = \frac{5}{4}. It can be proven that there is no better PP.

Constraints

Subtask\textbf{Subtask} nn aia_i Dependencies Score
11 1n501 \leq n \leq 50 No special restrictions None 10pts10 \operatorname{pts}
22 1n5001 \leq n \leq 500 Same as above 11
33 1n5×1031 \leq n \leq 5 \times 10^3 1,21, 2
44 1n1051 \leq n \leq 10^5 131 \sim 3 20pts20 \operatorname{pts}
55 No special restrictions ai{1,1}a_i \in \{-1, 1\} None
66 Same as above No special restrictions 151 \sim 5 30pts30 \operatorname{pts}

For 100%100\% of the testdata, 1n5×1051 \leq n \leq 5 \times 10^5, ai{1,0,1}a_i \in \{-1, 0, 1\}, 1u,vn1 \leq u, v \leq n. It is guaranteed that the given edges form an unrooted tree.

Translated by ChatGPT 5