Codeforces Round 523 (Div. 2)
F
Lost Root
Codeforces
2400
原题
← E→
翻译中…

The graph is called tree if it is connected and has no cycles. Suppose the tree is rooted at some vertex. Then tree is called to be perfect $k$-ary tree if each vertex is either a leaf (has no children) or has exactly $k$ children. Also, in perfect $k$-ary tree all leafs must have same depth.

翻译中…

For example, the picture below illustrates perfect binary tree with $15$ vertices:

翻译中…

image

翻译中…

There is a perfect $k$-ary tree with $n$ nodes. The nodes are labeled with distinct integers from $1$ to $n$, however you don't know how nodes are labelled. Still, you want to find the label of the root of the tree.

翻译中…

You are allowed to make at most $60 \cdot n$ queries of the following type:

翻译中…
  • "? $a$ $b$ $c$", the query returns "Yes" if node with label $b$ lies on the path from $a$ to $c$ and "No" otherwise.
翻译中…

Both $a$ and $c$ are considered to be lying on the path from $a$ to $c$.

翻译中…

When you are ready to report the root of the tree, print

翻译中…
  • "! $s$", where $s$ is the label of the root of the tree.
翻译中…

It is possible to report the root only once and this query is not counted towards limit of $60 \cdot n$ queries.

翻译中…

Interaction

翻译中…

The first line of the standard input stream contains two integers $n$ and $k$ ($3 \le n \le 1500$, $2 \le k \lt n$) — the number of nodes in the tree and the value of $k$.

翻译中…

It is guaranteed that $n$ is such that the tree forms a perfect $k$-ary tree.

翻译中…

You can ask at most $60 \cdot n$ queries. To ask a query, print a line of form "? $a$ $b$ $c$", where $1 \le a, b, c \le n$. After that you should read a single line containing "Yes" or "No" depending on the answer of the query.

翻译中…

The tree is fixed for each test and it doesn't depend on your queries.

翻译中…

When you are ready to print the answer, print a line of the form "! $s$", where $s$ is the label of the root vertex and then terminate your program.

翻译中…

After printing each query do not forget to print end of line and flush the output. Otherwise you may get Idleness limit exceeded. To do this, use:

翻译中…
  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • See documentation for other languages.
翻译中…

In case your program will make more than $60 \cdot n$ queries, but in other aspects would follow the interaction protocol and terminate coorectly, it will get verdict «Wrong Answer».

翻译中…

Hacks

翻译中…

To hack the solution use the following test format:

翻译中…

The first line should contain integers $n$ and $k$ ($3 \le n \le 1500$, $2 \le k \le 1500$) — the number of vertices and the $k$ parameter of the tree.

翻译中…

Of course, the value of $n$ must correspond to the size of the valid $k$-ary tree of some depth.

翻译中…

The second line should contain $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the labels of the tree in the natural order, all labels must be distinct.

翻译中…

Let's call the following ordering of the tree vertices to be natural: first the root of the tree goes, then go all vertices on depth of one edge from root, ordered from left to right, then go all vertices on depth of two edges from root, ordered from left to right, and so on until the maximum depth.

翻译中…

This way, the $a_1$ is the answer for the hack.

翻译中…

Note

翻译中…

The tree in the example is as follows:

翻译中…

image

翻译中…

The input and output for example illustrate possible interaction on that test (empty lines are inserted only for clarity).

翻译中…

The hack corresponding to the example would look like:

翻译中…

3 2
2 3 1

翻译中…

样例

Input #1
3 2

No

Yes
Output #1
? 1 3 2

? 1 2 3

! 2
InputCopy OutputCopy
题目信息
题号
1061F
时间限制
3 s
内存限制
256 MB
标签
交互题
概率论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。