Trees

Time limit: 0.5s Memory limit: 64MB Input: Output:

Task

Little Tree is interested in trees of various sizes. She has a rooted binary tree TT (i.e.~one where every vertex has at most 2 children), not necessarily a balanced tree, in front of her, whose root is denoted by 11 and whose vertices are labeled 1,,2N1, \ldots, 2^N. The label of any vertex is always strictly less than any of its children.

A set of vertices SS is called valid if no vertex in SS is the ancestor of any other vertex in SS. The value of SS, denoted by val(S)\text{val}(S), is given by the number of vertices within the tree that have any vertex of SS as an ancestor. (We consider a node to be its own ancestor.) In other words, it is the sum of the sizes of the subtrees rooted at vertices in SS.

Her enemy, Little Cactus, challenges her to the following task: Given NN and the tree TT, create as many valid, nonempty sets S1,,SKS_1, \ldots, S_K as possible, such that

S1++SKN×2N1+1|S_1| + \cdots + |S_K| \leq N \times 2^{N - 1} + 1

and val(Si)val(Sj)\text{val}(S_i) \neq \text{val}(S_j) for any iji \neq j.

Input data

The first line of the input file contains the integer NN. The second line contains integers p2,,p2Np_2, \ldots, p_{2^N}, separated by spaces, where pip_i denotes the parent of node ii.

Output data

Output KK, the number of sets you can create, followed by each of the KK sets on its own line. To output a set, first output its size, then its elements, separated by spaces.

Constraints and clarifications

  • N18N \leq 18.
  • If you output an incorrect solution (i.e.~one using too many vertices, or where two sets have the same value) then you will receive 0 points for a test. Otherwise, the fraction of the score you will receive for a particular test will be equal to (K/2N)1.5(K / 2^N)^{1.5}.
# Points Clarifications
1 7 pi=i1p_i = i - 1 for i=2,,2Ni = 2, \ldots, 2^N
2 12 N=4N = 4
3 13 N=6N = 6
4 21 N=9N = 9
5 26 N=11N = 11
6 21 No further restrictions

Example 1

stdin

2
1 2 3

stdout

4
1 4
1 3
1 2
1 1

Explanation

The tree is given by

We see that the sets S1={4}S_1 = \{ 4 \}, S2={3}S_2 = \{3\}, S3={2}S_3 = \{2 \}, S4={1}S_4 = \{1 \} all have distinct values (namely 1, 2, 3, 4 respectively), and have total size 42×221+1=54 \leq 2 \times 2^{2 - 1} + 1 = 5. Hence this output is correct.

Example 2

stdin

2
1 2 2

stdout

4
1 4
2 3 4
1 2
1 1

Explanation

The tree is given by

We see that the sets S1={4}S_1 = \{ 4 \}, S2={3,4}S_2 = \{3, 4\}, S3={2}S_3 = \{2\}, S4={1}S_4 = \{1\} all have distinct values (namely 1, 2, 3, 4 respectively), and have total size 52×221+1=55 \leq 2 \times 2^{2 - 1} + 1 = 5. Hence this output is correct.

Log in or sign up to be able to send submissions!