Jumping Monkey
Time limit1sMemory limit128 MB
Given a graph with up to 21 nodes representing trees, find the shortest lexicographically smallest shooting sequence that guarantees catching a monkey moving along edges each turn, or report it is impossible.
- Level
Hard8 of 10
- Topics
- BFS, Graph, Bit manipulation
- Solved
- No attempts yet
Problem
You are a hunter chasing a monkey in the forest, trying to shoot it down with your all-powerful automatic machine gun. The monkey is hiding somewhere behind the branches of one of the trees, out of your sight. You can aim at one of the trees and shoot; your bullets go through the branches and kill the monkey instantly if it happens to be in that tree. If it isn't, the monkey takes advantage of the time it takes you to reload and leaps into a neighbouring tree without you noticing. It never stays in the same place after a shot. You would like to find out whether there is a strategy that lets you capture the monkey for sure, no matter its initial location and subsequent jumps. If so, you must determine the shortest sequence of shots that guarantees this.
As an example, consider a forest with only two neighbouring trees. You can guarantee catching the monkey by shooting twice at the same tree: your first shot succeeds if the monkey was there in the first place; otherwise the monkey was behind the other tree and must have moved to the tree you shoot at the second time.
Depending on the shape of the forest, however, it may be impossible to ensure victory. One such example is a forest of three trees, all connected to one another. No matter where you aim, there are always two possible locations for the monkey at any given moment. (Here we consider the worst case, in which the monkey may consistently guess your next target tree.)
Input
The input consists of several test cases, separated by single blank lines. Each test case begins with a line containing two integers and (): is the number of trees in the forest, and is the number of adjacency relations between trees. Each of the following lines contains two distinct integers between and inclusive, the identifiers of an adjacent pair of trees. The order of the two trees within a pair carries no meaning, and no pair appears more than once. No tree is adjacent to itself, and there is always a path between any two trees in the forest.
The input ends with a line containing only two zeros (also preceded by a blank line).
Output
Print one line for each test case. If the task is impossible, the line must contain the single word Impossible. Otherwise it must describe the shortest sequence of shots with the required property: print the length of the sequence, then a colon and a space, then the identifiers of the trees to shoot at, in order, separated by single spaces (so the line reads L: V_1 V_2 ... V_L). If several shortest sequences exist, print the lexicographically smallest one. (One sequence is smaller than another in lexicographic order if, at the first position where they differ, its value is smaller.)