#P8949. [YsOI2022] 淀粉树

[YsOI2022] 淀粉树

Background

Ysuperman teaches everyone about starch and starch trees.

Problem Description

Ysuperman defines a rooted tree SS to be a starch tree of a tree TT if and only if SS satisfies the following two conditions (let sis_i denote the set of all vertices in the subtree of SS rooted at ii):

  1. SS and TT have the same number of vertices (assume it is nn) and are labeled 1n1\sim n.
  2. For any vertex ii in SS that has at least one child, and for any child jj of ii, it holds that in TT, vertex ii has a direct edge to at least one vertex in sjs_j.

It is easy to see that a tree TT may have many starch trees.

Now Ysuperman gives you nn and two trees TT and SS with vertices labeled 1n1\sim n. Let dd be the maximum degree of any vertex in SS. You need to perform at least one operation and at most dd operations. In each operation, replace TT with any of its starch trees, so that in the end TT becomes SS.

Note that the given SS is unrooted. As long as the final edge set of TT is the same as that of SS, we consider that TT has become SS.

The input guarantees that at least one solution exists.

Input Format

The first line contains two integers n,dn,d. It is guaranteed that dd equals the maximum degree of any vertex in SS.

The next n1n-1 lines each contain two integers u,vu,v, indicating that there is an edge between uu and vv in TT. It is guaranteed that they form a tree.

The next n1n-1 lines each contain two integers u,vu,v, indicating that there is an edge between uu and vv in SS. 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 k(1kd)k(1\le k\le d), representing the number of operations you perform.

Then output kk lines. On the ii-th line, output nn integers describing, after the ii-th operation, the parent of each vertex 1n1\sim n in the rooted tree that TT becomes. The parent of the root is defined to be 00. 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 TT:

This is SS:

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

This rooted tree is a starch tree of TT, for the following reasons:

  1. For child 11 of 22, in TT, 22 has a direct edge to 11 in s1={1}s_1=\{1\}.
  2. For child 22 of 33, in TT, 33 has a direct edge to 11 in s2={1,2}s_2=\{1,2\}.
  3. For child 44 of 33, in TT, 33 has a direct edge to 44 in s4={4}s_4=\{4\}.
  4. For child 55 of 33, in TT, 33 has a direct edge to 55 in s5={5}s_5=\{5\}.

The final rooted tree has the same edges as SS, so this output will be judged as correct.

Constraints

Subtask 11 (2020 points): n6n\le 6.

Subtask 22 (2020 points): d=2d=2.

Subtask 33 (2020 points): TT can be turned into SS with only one operation, and n447n\le 447.

Subtask 44 (2020 points): n2000n\le 2000.

Subtask 55 (2020 points): no special restrictions.

For all testdata, 2n1052\le n\le 10^5, and d×n2×105d\times n\le 2\times 10^5.

Hint

The checker for this problem is provided as an attachment.

Translated by ChatGPT 5