QOJ.ac

QOJ

时间限制: 1 s 内存限制: 512 MB 总分: 100

#780. Living on the Tree

统计

Living on a Tree (tree)

Xiao Yi and Xiao Ai live on a tree. The tree $T$ has $n$ nodes and is connected by $n-1$ edges. There is a permutation $p$ on the tree. In each step, Xiao Ai can choose an edge $(u, v) \in T$ and swap $p_u$ and $p_v$. Xiao Ai's task is to sort the permutation. To estimate the minimum number of swaps required, Xiao Ai asks Xiao Yi for advice. After some thought, Xiao Yi writes down the following formula:

$$\frac{1}{2} \sum_{i=1}^{n} \text{dist}(i, p_i)$$

After trying, Xiao Ai discovers that, coincidentally, the current permutation exactly reaches the lower bound given by Xiao Yi! She wants to test you: can you provide a sorting scheme that reaches this lower bound? In particular, she also hopes that the scheme you provide has the lexicographically smallest order. We consider the edges on the tree to be numbered from $1$ to $n-1$, and the lexicographical order of the scheme is determined by comparing the indices of the edges used in sequence.

Input

The first line contains a positive integer $n$, representing the number of nodes in the tree.

The next $n-1$ lines each contain two positive integers $u_i, v_i$, representing the $i$-th edge ($1 \le i \le n-1$).

The next line contains $n$ positive integers, representing the permutation $p$.

Output

Output one line. Output the indices of the edges used in the lexicographically smallest solution in order.

Examples

Input 1

5
5 2
3 2
2 4
1 3
2 1 5 3 4

Output 1

2 1 3 4 2

Note 1

The initial sequence is $2, 1, 5, 3, 4$. During the following $5$ operations, the sequence becomes: $2, 5, 1, 3, 4$ $2, 4, 1, 3, 5$ $2, 3, 1, 4, 5$ $1, 3, 2, 4, 5$ * $1, 2, 3, 4, 5$

Examples 2

See tree/tree2.in and tree/tree2.ans in the contestant's directory.

Subtasks

For $100\%$ of the data, it is guaranteed that $1 \le n \le 10^3$, and the given permutation can be sorted within $\frac{1}{2} \sum_{i=1}^{n} \text{dist}(i, p_i)$ operations.

Test Case $n$ Special Constraints
1, 2 $= 5$
3, 4 $= 30$
5 $= 10^2$
6 $= 10^3$ A0
7 $= 10^3$ A1
8 $= 10^3$ B0
9 $= 10^3$ B1
10 $= 10^3$

Special constraints: A represents the edge set $\{(1, 2), (2, 3), \dots, (n-1, n)\}$. B represents the edge set $\{(1, 2), (1, 3), \dots, (1, n)\}$. 0 indicates that the edges are guaranteed to be given in the order specified above. 1 indicates that they may be given in any order.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.