#P9988. [Ynoi2079] 2stmo
[Ynoi2079] 2stmo
Problem Description
Given a rooted tree with vertices, the vertices are numbered , where is the root. denote the parents of vertices , respectively.
Given pairs of integers , you need to construct a permutation of such that the cost of this permutation does not exceed .
The cost of a permutation is defined as:
$|S(a_{p_1})|+|S(b_{p_1})|+\sum\limits_{i=2}^m |S(a_{p_{i-1}})\oplus S(a_{p_i})|+|S(b_{p_{i-1}})\oplus S(b_{p_i})|$
Here, is the set of vertices in the subtree rooted at , is the number of elements in set , and is the symmetric difference of sets and (that is, the set of elements that appear in exactly one of and ).
Input Format
The first line contains two integers .
The second line contains integers .
The next lines each contain two integers representing , for .
Output Format
Output lines, each with one integer, representing in order.
5 3
1 1 2 3
1 1
2 5
4 3
2
3
1
Hint
Idea: nzhtl1477, Solution: ccz181078, Code: ccz181078, Data: ccz181078.
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, , , .
Constraints.
Translated by ChatGPT 5