|
<p> |
|
Ethan is doing his second programming assignment: implementing pre-order tree traversal. |
|
</p> |
|
|
|
<p> |
|
Ethan has a binary tree with <strong>N</strong> nodes (numbered 1 to <strong>N</strong>), rooted at node 1. |
|
Each node <em>i</em>'s left child is node <strong>A<sub>i</sub></strong> (with <strong>A<sub>i</sub></strong> = 0 indicating no left child), |
|
and similarly its right child is <strong>B<sub>i</sub></strong> (with <strong>B<sub>i</sub></strong> = 0 indicating no right child). |
|
Each node <em>i</em> is also assigned an integral label <strong>L<sub>i</sub></strong>. |
|
</p> |
|
|
|
<p> |
|
Given such a tree, Ethan must compute its <a href="https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_(NLR)">pre-order traversal</a> (expressed as a sequence of node labels). |
|
The pre-order traversal of a tree involves taking its root node, then concatenating the pre-order traversal of the root's left sub-tree (if any), |
|
and then concatenating the pre-order traversal of the root's right sub-tree (if any). |
|
</p> |
|
|
|
<p> |
|
Ethan has attempted to solve this problem, but unfortunately he got his computer science terms mixed up, and now his algorithm finds the tree's |
|
<a href="https://en.wikipedia.org/wiki/Tree_traversal#Post-order_(LRN)">post-order traversal</a> instead! |
|
The post-order traversal of a tree involves taking the post-order traversal of the root's left sub-tree (if any), |
|
and then concatenating the post-order traversal of the root's right sub-tree (if any), and finally concatenating the root node at the end. |
|
</p> |
|
|
|
<p> |
|
Since you were mean to Ethan on his first assignment, you'd like to cheer him up by making his algorithm work out after all. |
|
Though the tree's shape must stay as is, you can choose a set of labels <strong>L<sub>1..N</sub></strong> for its nodes such that Ethan's algorithm will still produce the correct answer |
|
— in other words, such that the sequence of node labels in the tree's pre-order traversal is equal to the sequence of node labels in its post-order traversal. |
|
Your only two restrictions are that each node label must be between 1 and <strong>K</strong> (inclusive), |
|
and that every integer between 1 and <strong>K</strong> (inclusive) must be used as the label of at least one node. |
|
You'd like to find any way of validly labelling the nodes, or determine that no way exists. |
|
</p> |
|
|
|
|
|
<h3>Input</h3> |
|
|
|
<p> |
|
Input begins with an integer <strong>T</strong>, the number of trees. |
|
For each tree, there is first a line containing the space-separated integers <strong>N</strong> and <strong>K</strong>. |
|
Then, <strong>N</strong> lines follow. The <em>i</em>th of these lines contains the space-separated integers <strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong>. |
|
</p> |
|
|
|
|
|
<h3>Output</h3> |
|
|
|
<p> |
|
For the <em>i</em>th tree, print a line containing "Case #<em>i</em>: " |
|
followed by your chosen node labels <strong>L<sub>1..N</sub></strong> separated by spaces, or "Impossible" if there's no valid way to label the nodes. |
|
</p> |
|
|
|
|
|
<h3>Constraints</h3> |
|
|
|
<p> |
|
1 ≤ <strong>T</strong> ≤ 80 <br /> |
|
1 ≤ <strong>K</strong> ≤ <strong>N</strong> ≤ 2,000 <br /> |
|
0 ≤ <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong> ≤ <strong>N</strong> <br /> |
|
</p> |
|
|
|
<p> |
|
Every tree is guaranteed to be a valid binary tree rooted at node 1. |
|
</p> |
|
|
|
<h3>Explanation of Sample</h3> |
|
|
|
<p> |
|
In the first case, if <strong>L</strong> = [1, 1], then both the pre-order and post-order label sequences will be [1, 1]. |
|
</p> |
|
|
|
<p> |
|
In the second case, for each label between 1 and <strong>K</strong> to be present, you must choose either |
|
<strong>L</strong> = [1, 2] or <strong>L</strong> = [2, 1], both of which would result in the pre-order and post-order label sequences differing. |
|
For example, if <strong>L</strong> = [1, 2], then the pre-order sequence will be [1, 2] while the post-order sequence will be [2, 1]. |
|
</p> |
|
|
|
<p> |
|
In the third case, if <strong>L</strong> = [2, 2, 1], then the pre-order and post-order label sequences will both be [2, 1, 2]. |
|
</p> |
|
|
|
<p> |
|
Note that other outputs for example cases 3 to 5 would also be accepted. |
|
</p> |
|
|