Under construction

Still being built. Things may change

Compressed Spanning Subtrees

Time limit2sMemory limit256 MB

Summary
Reconstruct a hidden tree with no degree-two vertices by asking how many vertices the compressed spanning subtree of a chosen set contains.
Level

Hard9 of 10

Topics
Tree, Divide and conquer, Implementation
Solved
No attempts yet

Problem

Consider a tree TT with nn vertices numbered from 1 to nn. For a set XX of spanned vertices (vertices not in XX are called not spanned), the compressed spanning subtree S(X)S(X) is computed as follows.

  1. Set S(X)←TS(X) \leftarrow T.
  2. If a not spanned vertex has exactly one incident edge, remove it together with that edge.
  3. Repeat step 2 while the condition still holds.
  4. If a not spanned vertex has exactly two incident edges, remove it together with both edges and add a new edge connecting the two other endpoints.
  5. Repeat step 4 while the condition still holds.

Formally, S(X)S(X) is the smallest subgraph of TT that contains all vertices of XX, after every other vertex of degree two or less is smoothed out.

The tree from test 1, and its compressed spanning subtrees for X={3,4,6}X = \{3, 4, 6\} and X={2,5,6}X = \{2, 5, 6\}.

The tree TT is not given to you. Your task is to find it. You can ask questions of this form: "How many vertices does the compressed spanning subtree of XX contain?" Without a guarantee, finding the tree this way would be impossible, so TT has no vertex with exactly two incident edges.

Examples1

  1. Example 1

    Input
    6
    
    3
    
    4
    
    4
    
    
    Expected output
    
    ? 3 4 3 6
    
    ? 4 1 2 3 6
    
    ? 3 2 5 6
    
    ! 1 2 2 3 3