#P8949. [YsOI2022] 淀粉树
[YsOI2022] 淀粉树
Background
Ysuperman teaches everyone about starch and starch trees.
Problem Description
Ysuperman defines a rooted tree to be a starch tree of a tree if and only if satisfies the following two conditions (let denote the set of all vertices in the subtree of rooted at ):
- and have the same number of vertices (assume it is ) and are labeled .
- For any vertex in that has at least one child, and for any child of , it holds that in , vertex has a direct edge to at least one vertex in .
It is easy to see that a tree may have many starch trees.
Now Ysuperman gives you and two trees and with vertices labeled . Let be the maximum degree of any vertex in . You need to perform at least one operation and at most operations. In each operation, replace with any of its starch trees, so that in the end becomes .
Note that the given is unrooted. As long as the final edge set of is the same as that of , we consider that has become .
The input guarantees that at least one solution exists.
Input Format
The first line contains two integers . It is guaranteed that equals the maximum degree of any vertex in .
The next lines each contain two integers , indicating that there is an edge between and in . It is guaranteed that they form a tree.
The next lines each contain two integers , indicating that there is an edge between and in . It is guaranteed that they form a tree.
Output Format
For easier checking, Ysuperman asks you to output the answer in the form of rooted trees.
The first line of the output contains a positive integer , representing the number of operations you perform.
Then output lines. On the -th line, output integers describing, after the -th operation, the parent of each vertex in the rooted tree that becomes. The parent of the root is defined to be . Please ensure that the root you output is the root of the starch tree.
5 3
1 2
1 3
3 4
3 5
3 2
3 4
3 5
1 2
1
2 3 0 3 3
Hint
Sample 1 Explanation
This is :

This is :

This output performs only one operation on , turning into the following rooted tree:

This rooted tree is a starch tree of , for the following reasons:
- For child of , in , has a direct edge to in .
- For child of , in , has a direct edge to in .
- For child of , in , has a direct edge to in .
- For child of , in , has a direct edge to in .
The final rooted tree has the same edges as , so this output will be judged as correct.
Constraints
Subtask ( points): .
Subtask ( points): .
Subtask ( points): can be turned into with only one operation, and .
Subtask ( points): .
Subtask ( points): no special restrictions.
For all testdata, , and .
Hint
The checker for this problem is provided as an attachment.
Translated by ChatGPT 5