| <p> | |
| Carlos is hoping to strike it rich with his new consulting firm, Carlos | |
| Structures Industries (CSI). With a solid mission statement and a can-do | |
| attitude, he's sure to succeed in the wide world of bespoke data structures. | |
| </p> | |
| <p><em> | |
| "Our vision is to synergistically leverage our core competencies to deliver | |
| competitive market solutions that assertively meet our clients' needs." | |
| </em></p> | |
| <p> | |
| His first client, the well-known translation firm Treehouse, is looking to | |
| update their logo. Treehouse wants their logo to be a rooted tree with | |
| <strong>N</strong> nodes numbered 1 through <strong>N</strong>. They have a rough idea of what the tree should | |
| look like, but they want Carlos to finish fleshing it out. In particular, | |
| they have <strong>M</strong> requirements, the <em>i</em>th of which states that the | |
| <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">lowest common ancestor</a> | |
| of nodes <strong>X<sub>i</sub></strong> and <strong>Y<sub>i</sub></strong> must be node <strong>Z<sub>i</sub></strong>. | |
| </p> | |
| <p> | |
| Carlos's goal is to find any valid tree consistent with all of these requirements | |
| if possible, or to determine that no such tree exists. | |
| </p> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of tree designs. | |
| For each design, there is first a line containing the space-separated integers <strong>N</strong> and <strong>M</strong>. | |
| Then <strong>M</strong> lines follow, the <em>i</em>th of which contains the space-separated integers | |
| <strong>X<sub>i</sub></strong>, <strong>Y<sub>i</sub></strong>, and <strong>Z<sub>i</sub></strong>. | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <em>i</em>th design, print a line containing "Case #<em>i</em>: " | |
| followed by a description of a valid rooted tree if possible, or the string "Impossible" if no valid tree exists. | |
| A tree description is <strong>N</strong> space-separated integers, the <em>j</em>th of which is node <em>j</em>'s parent (or 0 if node <em>j</em> is the root). | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 100 <br /> | |
| 2 ≤ <strong>N</strong> ≤ 60 <br /> | |
| 1 ≤ <strong>M</strong> ≤ 120 <br /> | |
| 1 ≤ <strong>X<sub>i</sub></strong>, <strong>Y<sub>i</sub></strong>, <strong>Z<sub>i</sub></strong> ≤ <strong>N</strong> <br /> | |
| <strong>X<sub>i</sub></strong> ≠ <strong>Y<sub>i</sub></strong> | |
| </p> | |
| <h3>Explanation of Sample</h3> | |
| <p> | |
| In the first case, the LCA of nodes 1 and 2 in the chosen tree is 3, as required. This is the only valid output for this case. | |
| </p> | |
| <p> | |
| In the third case, "2 3 0 3" would also be accepted, while no other outputs would be. | |
| </p> | |
| <p> | |
| In the fifth and sixth cases, multiple possible outputs would be accepted. | |
| </p> | |