I. Guess the Tree

Codeforces
IDCF10175I
Time2000ms
Memory256MB
Difficulty
English · Original
Formal · Original
Jury has a complete binary tree with n = 2h - 1 vertices. Its vertices are numbered with distinct integers from 1 to n, but you don't know which vertices are connected with which. You can ask a jury's program, what is the distance between some two vertices. You must restore tree structure with at most 2.5·h·n such queries. This is an interactive problem. Your program should communicate with the jury's program, using standard input and output for that. In the very beginning your program gets a single integer n (1 ≤ n ≤ 1023, n + 1 is the power of two) — the number of vertices in the tree. After that you can make at most 2.5·h·n queries. To make a query, output character «_?_» and two integers from 1 to n — numbers of vertices x and y you want to know the distance between. As a response a single integer will be given to you — the distance between vertices x and y. Once you find out a complete tree structure, output character «_!_», and then n integers pi, where pi is the parent of vertex i or 0 if i is the root of the tree. After this your program must terminate. Please note that each your message must end with a line break. Also after outputting each message your program must flush the stream buffer, so that the outputted information could reach jury's program: for instance, this can be done by calling «_fflush(stdout)_» or «_cout.flush()_» in C++, «_System.out.flush()_» in Java, «_Console.Out.Flush()_» in C#, «_flush(output)_» in Pascal, «_sys.stdout.flush()_» in Python. ## Interaction This is an interactive problem. Your program should communicate with the jury's program, using standard input and output for that.In the very beginning your program gets a single integer n (1 ≤ n ≤ 1023, n + 1 is the power of two) — the number of vertices in the tree.After that you can make at most 2.5·h·n queries. To make a query, output character «_?_» and two integers from 1 to n — numbers of vertices x and y you want to know the distance between. As a response a single integer will be given to you — the distance between vertices x and y.Once you find out a complete tree structure, output character «_!_», and then n integers pi, where pi is the parent of vertex i or 0 if i is the root of the tree. After this your program must terminate. [samples] ## Note Please note that each your message must end with a line break. Also after outputting each message your program must flush the stream buffer, so that the outputted information could reach jury's program: for instance, this can be done by calling «_fflush(stdout)_» or «_cout.flush()_» in C++, «_System.out.flush()_» in Java, «_Console.Out.Flush()_» in C#, «_flush(output)_» in Pascal, «_sys.stdout.flush()_» in Python.
**Definitions** Let $ n = 2^h - 1 $ for some $ h \in \mathbb{Z}^+ $, where $ 1 \leq n \leq 1023 $. Let $ T = (V, E) $ be a complete binary tree with vertex set $ V = \{1, 2, \dots, n\} $ and unknown edge set $ E $. Let $ d(x, y) $ denote the shortest path distance between vertices $ x, y \in V $ in $ T $. **Constraints** 1. $ n + 1 $ is a power of two. 2. At most $ \lfloor 2.5 \cdot h \cdot n \rfloor $ distance queries may be made. 3. Each query: output `? x y` for $ x, y \in V $, receive $ d(x, y) \in \mathbb{Z}_{\geq 0} $. **Objective** Reconstruct the parent array $ p = (p_1, p_2, \dots, p_n) $, where $ p_i \in \{0\} \cup V $, such that: - $ p_i = 0 $ if vertex $ i $ is the root, - $ p_i = j $ if vertex $ j $ is the parent of vertex $ i $, and output `! p_1 p_2 ... p_n`.
API Response (JSON)
{
  "problem": {
    "name": "I. Guess the Tree",
    "description": {
      "content": "Jury has a complete binary tree with n = 2h - 1 vertices. Its vertices are numbered with distinct integers from 1 to n, but you don't know which vertices are connected with which. You can ask a jury'",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10175I"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Jury has a complete binary tree with n = 2h - 1 vertices. Its vertices are numbered with distinct integers from 1 to n, but you don't know which vertices are connected with which.\n\nYou can ask a jury'...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n = 2^h - 1 $ for some $ h \\in \\mathbb{Z}^+ $, where $ 1 \\leq n \\leq 1023 $.  \nLet $ T = (V, E) $ be a complete binary tree with vertex set $ V = \\{1, 2, \\dots, n\\} $ and unkno...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments