Lost Root
Contest: Codeforces Round 523 (Div. 2)
Difficulty rating: 2400
Tags: interactive, probabilities

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:

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 format:
The first line of the standard input stream contains two integers $$$n$$$ and $$$k$$$ ($$$3 \le n \le 1500$$$, $$$2 \le k < 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.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
The tree in the example is as follows:

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:

Example input:
3 2
No
Yes

Example output:
? 1 3 2
? 1 2 3
! 2

Editorial:
This solution had many randomized approaches, some with higher probability of passing and some with lower probability of passing.
The author's solution (there exist better solutions with even lower probability of failure - comment yours below) is as follows:
Part 1: Checking if a node is a leaf node:
It can be done in $$$O(n)$$$ queries.
Suppose candidate node is $$$X$$$
Generate a random node $$$Y (!=X)$$$
For all $$$Z$$$, if $$$Y$$$ $$$X$$$ $$$Z$$$ is $$$false$$$, then $$$X$$$ is a leaf node, otherwise is not.
Part 2: Finding a leaf node:
Generate a random node and check if it is a leaf node. Probability of getting a lead node is $$$>=0.5$$$. Higher the $$$K$$$, higher the probability.
So we can find a leaf node in $$$O(20\cdot n)$$$ queries with failure probability $$$(1/2)^{20}$$$
Part 3: Generating a leaf node in other subtree of the actual root:
Fix a random node (that is not the same as the leaf node, $$$L1$$$, that we found), check if it is a leaf node, and if it is a leaf node and check if $$$2h-1$$$ nodes separate Leaf $$$1$$$ and this current leaf. If yes, we have found two separate leaf nodes and the $$$2h-1$$$ candidate nodes for the root. We can use  $$$O(40\cdot n)$$$ queries to ensure a failure probability of $$$(1/2)^{20}$$$
Finally: Instead of checking all of them separately in $$$2H\cdot N$$$, we can fix their order in $$$O(H^2)$$$ by finding each node's appropriate position by placing them incrementally. Let the initial path be $$$L1$$$ $$$L2$$$, then we add $$$X$$$ to get $$$L1$$$ $$$X$$$ $$$L2$$$. Now to find $$$Y$$$'s appropriate position, we check if it lies between $$$L1$$$, $$$X$$$ or $$$X$$$, $$$L2$$$. And so on. In the final order, the middle node would be the root.