luogu#P10560. [ICPC2024 Xi'an I] Holes and Balls
[ICPC2024 Xi'an I] Holes and Balls
题目描述
You are given balls, the -th ball's value is . It's guaranteed that is a permutation of .
There is also a rooted tree of vertices, each of the vertices is a hole, and each hole can only hold one ball.
The tree's root is the first vertex.
Now you need to fill the holes with the balls.
You need to throw each ball in order of to in the following steps:
- Throw the ball into vertex .
- Let the vertex where the ball is be .
- If the -th vertex has already been filled with other balls, you need to choose a vertex and throw the ball into the -th vertex, then return to step . You need to guarantee that the -th vertex is the -th vertex's son and at least one vertex in the subtree of the -th vertex is not filled.
- Otherwise, the ball will fill the -th vertex.
After throwing all the balls, let express the value of the ball in the -th vertex.
You need to find the minimum lexicographical order of .
We define as the number of vertices on the path from the -th vertex to the tree's root(the first vertex).
Specially, for any two vertices , it's guaranteed that .
输入格式
The first line contains a single integer - the number of vertices in this tree.
The next line contains numbers, the -th number is . It's guaranteed that is a permutation of .
The next lines contain a description of the tree's edges. The -th of these lines contains two integers and - the numbers of vertices connected by the -th edge.
It is guaranteed that the given edges form a tree.
And for any vertices , it's guaranteed that .
输出格式
Output integers, the minimum lexicographical order of .
5
3 1 5 4 2
1 2
2 3
3 4
4 5
3 1 5 4 2
9
9 2 6 3 5 7 1 4 8
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
9 2 1 3 6 4 8 5 7