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 k -ary tree if each vertex is either a leaf (has no children) or has exactly k k children. Also, in perfect k k -ary tree all leafs must have same depth.
For example, the picture below illustrates perfect binary tree with 15 15 vertices:
There is a perfect k k -ary tree with n n nodes. The nodes are labeled with distinct integers from 1 1 to n 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⋅n 60⋅n queries of the following type:
- "? a a b b c c ", the query returns "Yes" if node with label b b lies on the path from a a to c c and "No" otherwise.
Both a a and c c are considered to be lying on the path from a a to c c .
When you are ready to report the root of the tree, print
- "! s s ", where s 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⋅n 60⋅n queries.
Interaction
The first line of the standard input stream contains two integers n n and k k (3≤n≤1500 3≤n≤1500 , 2≤k<n 2≤k<n ) — the number of nodes in the tree and the value of k k .
It is guaranteed that n n is such that the tree forms a perfect k k -ary tree.
You can ask at most 60⋅n 60⋅n queries. To ask a query, print a line of form "? a a b b c c ", where 1≤a,b,c≤n 1≤a,b,c≤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 s ", where s 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⋅n 60⋅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 n and k k (3≤n≤1500 3≤n≤1500 , 2≤k≤1500 2≤k≤1500 ) — the number of vertices and the k k parameter of the tree.
Of course, the value of n n must correspond to the size of the valid k k -ary tree of some depth.
The second line should contain a 1 ,a 2 ,…,a n a1,a2,…,an (1≤a i ≤n 1≤ai≤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 a1 is the answer for the hack.
Example
3 2 No Yes
? 1 3 2 ? 1 2 3 ! 2
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:
3 2
2 3 1
题目:给定一个完全K叉树,节点数的N(其实的告诉了高度D,K没什么用),交互,可以询问(?,a,b,c),回答b是否再a到c的路径上,求根节点。
思路:我们先求出两个叶子节点X,Y,然后然他们之间的节点个数,如果=2D-1,则说明根在路径上,然后我们去试探路径上的点,如果这个点到X和Y的距离都是D,说明是根。
复杂度:首先得到一个根的概率是1/2;其次经过根节点的概率大于3/4; 每次的复杂度是O(N),次数显然小于60次;
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int D,N,K,p[maxn],cnt; char s[];
int getleaf()
{
while(){
int x=(rand()%N)+,y=,F=; if(x==) y=;
rep(i,,N){
if(i==x) continue;
cout<<"?"<<" "<<i<<" "<<x<<" "<<y<<endl;
cin>>s;
if(s[]=='Y') {F=; break;}
}
if(!F) return x;
}
}
int getnum(int x,int y,int opt) //opt==1的时候记录路径
{
int num=; if(opt) cnt=;
rep(i,,N){
cout<<"?"<<" "<<x<<" "<<i<<" "<<y<<endl;
cin>>s;
if(s[]=='Y') {
num++; if(opt) p[++cnt]=i;
}
}
return num;
}
int main()
{
scanf("%d%d",&N,&K);
int tmp=,D=,tK=;while(tmp<N) tK*=K,tmp+=tK,D++;
while(true){
int x=getleaf();
int y=getleaf();
while(y==x) y=getleaf();
if(getnum(x,y,)!=D+D-) continue;
rep(i,,cnt) {
if(getnum(p[i],x,)==D){
cout<<"!"<<" "<<p[i]<<endl;
return ;
}
}
}
return ;
}