CF1061F.Lost Root

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 kk -ary tree if each vertex is either a leaf (has no children) or has exactly kk children. Also, in perfect kk -ary tree all leafs must have same depth.

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

There is a perfect kk -ary tree with nn nodes. The nodes are labeled with distinct integers from 11 to nn , 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 60n60 \cdot n queries of the following type:

  • "? aa bb cc ", the query returns "Yes" if node with label bb lies on the path from aa to cc and "No" otherwise.

Both aa and cc are considered to be lying on the path from aa to cc .

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

  • "! ss ", where ss 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 60n60 \cdot n queries.

输入格式

输出格式

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

It is guaranteed that nn is such that the tree forms a perfect kk -ary tree.

You can ask at most 60n60 \cdot n queries. To ask a query, print a line of form "? aa bb cc ", where 1a,b,cn1 \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 "! ss ", where ss 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 60n60 \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 nn and kk ( 3n15003 \le n \le 1500 , 2k15002 \le k \le 1500 ) — the number of vertices and the kk parameter of the tree.

Of course, the value of nn must correspond to the size of the valid kk -ary tree of some depth.

The second line should contain a1,a2,,ana_1, a_2, \ldots, a_n ( 1ain1 \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 a1a_1 is the answer for the hack.

输入输出样例

  • 输入#1

    3 2
    
    No
    
    Yes
    

    输出#1

    ? 1 3 2
    
    ? 1 2 3
    
    ! 2

说明/提示

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:

<br></br>3 2<br></br>2 3 1<br></br>

首页