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
For example, the picture below illustrates perfect binary tree with
There is a perfect
You are allowed to make at most
- "?
Both
When you are ready to report the root of the tree, print
- "!
It is possible to report the root only once and this query is not counted towards limit of
InteractionThe first line of the standard input stream contains two integers
It is guaranteed that
You can ask at most
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 "!
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
Hacks
To hack the solution use the following test format:
The first line should contain integers
Of course, the value of
The second line should contain
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
Example3 2
No
Yes
? 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:
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=100010; int D,N,K,p[maxn],cnt; char s[10]; int getleaf() { while(1){ int x=(rand()%N)+1,y=1,F=0; if(x==1) y=2; rep(i,1,N){ if(i==x) continue; cout<<"?"<<" "<<i<<" "<<x<<" "<<y<<endl; cin>>s; if(s[0]=='Y') {F=1; break;} } if(!F) return x; } } int getnum(int x,int y,int opt) //opt==1的时候记录路径 { int num=0; if(opt) cnt=0; rep(i,1,N){ cout<<"?"<<" "<<x<<" "<<i<<" "<<y<<endl; cin>>s; if(s[0]=='Y') { num++; if(opt) p[++cnt]=i; } } return num; } int main() { scanf("%d%d",&N,&K); int tmp=1,D=1,tK=1;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,1)!=D+D-1) continue; rep(i,1,cnt) { if(getnum(p[i],x,0)==D){ cout<<"!"<<" "<<p[i]<<endl; return 0; } } } return 0; }