Texting with Grandma

Grandma has bought a state-of-the-art shinny new mobile phone. She is specially excited because she will text with Eloi even if he is away from home. “Grandma is sugar and spice, and everything nice, specially when she is texting ... she is a living message sent from a time we did not live.” Eloi, always set in the old ways, is worried he will not be able to text with Grandma because his mobile phone is so old that he is hardly able to play ‘snakes’ on it. As a matter of fact, his mobile has been discontinued for a couple of years already. The new hype in the texting scene is to text trees because research in computer science have found a way to ‘pack’ any tree. By ‘tree’ we mean a labeled undirected graph in which there is exactly one path between any pair of vertices. Grandma cannot wait to send Eloi messages packed with tons of electronic trees of many sorts drawn with all her love. For a tree T with set of n vertices V = {0,...,n − 1}, where n ≥ 2, the tree packing algorithm implemented in Grandma’s mobile on input T builds a sequence t of exactly n − 2 vertices as follows: let t the empty sequence for each k = 1, ..., n-2 let L be the set of leaves of T let m be the minimum element of L append to t the unique vertex adjacent to m remove m from T As an example, consider the following ‘oak’ drawn by Grandma, with set of vertices {0, 1, . . . , 8}: 4 16 503 287 One can check that the oak drawn by Grandma is packed into the sequence 5, 8, 0, 3, 3, 0, 5 by the tree packing algorithm. Eloi will be getting texts from Grandma soon and he is faced with the following problem: given a sequence t of integer numbers, he would like to have an app for his old mobile for detecting if t can be the output of the tree packing algorithm for some input tree T with set of vertices V = {0, . . . , m − 1} for some positive integer m. If this is the case, he would like to ‘unpack’ t into its associated tree T, so that he can appreciate Grandma’s texting love. Since Eloi’s mobile is old (as in ‘very old’), this app needs to be very efficient. Input The input consists of several test cases. Each test case consists of a single line describing a non-empty sequence t of blank-separated integers between 0 and 100000, both limits inclusive. You can assume that 1 ≤ |t| ≤ 100000.

2/2 Output For sequence t described in each test case: • if t can not be the output of the tree packing algorithm, then output ‘impossible’; • if t is the output of the tree packing algorithm for some input tree T with set of vertices V , output |V | + 1 lines: the first line contains |V | and the next 1 ≤ i ≤ |V | lines each lists in ascending order the set of vertices directly associated to vertex i − 1 in T (output a single blank between consecutive numbers). Print a line with a single asterisk (‘*’) between consecutive test cases. Sample Input 5803305 1712 Sample Output 9 345 5 8 067 0 018 3 3 25

  • impossible