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

Input
3 2

No

Yes
Output
? 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 ;
}

CF1061F:Lost Root(交互&概率)的更多相关文章

  1. Android DRM

    最近在了解Android DRM相关的一些知识,下面转一个ARM大佬分享的内容: 前言 本文简略地介绍了如何在Android下实现DRM(Digital Rights Management, 数字版权 ...

  2. PRML读书会第八章 Graphical Models(贝叶斯网络,马尔科夫随机场)

    主讲人 网神 (新浪微博: @豆角茄子麻酱凉面) 网神(66707180) 18:52:10 今天的内容主要是: 1.贝叶斯网络和马尔科夫随机场的概念,联合概率分解,条件独立表示:2.图的概率推断in ...

  3. (转)Python实例手册

    原文地址:http://hi.baidu.com/quanzhou722/item/cf4471f8e23d3149932af2a7 实在是太好的资料了,不得不转 python实例手册 #encodi ...

  4. 转载 python实例手册

    python实例手册 #encoding:utf8# 设定编码-支持中文 0说明 手册制作: 雪松 更新日期: 2013-12-19 欢迎系统运维加入Q群: 198173206 # 加群请回答问题 请 ...

  5. 【转载】python实例手册

    今天写爬虫的时候遇到了问题,在网上不停地查找资料,居然碰到两篇好文章: 1.python实例手册   作者:没头脑的土豆 另一篇在这:shell实例手册 python实例手册 #encoding:ut ...

  6. Huffman编码实现文件的压缩与解压缩。

    以前没事的时候写的,c++写的,原理很简单,代码如下: #include <cstdio> #include <cstdlib> #include <iostream&g ...

  7. Python实例手册

    在电脑中突然发现一个这么好的资料,雪松大神制作,不敢独享,特与大家共享.连他的广告也一并复制了吧! python实例手册 #encoding:utf8 # 设定编码-支持中文 0说明 手册制作: 雪松 ...

  8. GPU加速:宽深度推理

    GPU加速:宽深度推理 Accelerating Wide & Deep Recommender Inference on GPUs 推荐系统推动了许多最流行的在线平台的参与.随着为这些系统提 ...

  9. Kali2搭建Metasploitable3靶机

    Metasploitable3简介 Metasploitable3是Metasploitable2的升级版本,它是一个虚拟靶机系统,里面含有大量未被修复的安全漏洞,它主要是用于metasploit-f ...

随机推荐

  1. html 音频

    <!DOCTYPE html><meta charset="utf-8"><video src="movie.webm" cont ...

  2. Es+kafka搭建日志存储查询系统(设计)

    现在使用的比较常用的日志分析系统有Splunk和Elk,Splunk功能齐全,处理能力强,但是是商用项目,而且收费高.Elk则是Splunk项目的一个开源实现,Elk是ElasticSearch(Es ...

  3. pycharm添加git ignore

    pycharm现在提供了git ignore,很方便 从这里下载扩展 https://plugins.jetbrains.com/plugin/7495--ignore 放到pycharm根目录\pl ...

  4. JS事件监听手机屏幕触摸事件 Touch

    JS移动客户端--触屏滑动事件 移动端触屏滑动的效果其实就是图片轮播,在PC的页面上很好实现,绑定click和mouseover等事件来完成.但是在移动设备上,要实现这种轮播的效果,就需要用到核心的t ...

  5. ubuntu 中启动SDK manager

    Android SDK安装后,目录结构如下: root@localhost:/home/ranxf/Android/Sdk/android-sdk-linux# ll 总用量 drwxrwxr-x r ...

  6. CF337C - Quiz

    /*题目大意,给出n道题,假设答对了m道题,求最小的分数,有一个规则,就是连续答对num==k道题那么分数就翻倍,然后num清零,从新开始计数,到大连续k道的时候 要先加上这道题的分数1,再乘以2, ...

  7. SQL学习笔记三(补充-2)之MySQL数据类型

    阅读目录 一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型 一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 ...

  8. Mysql索引结构与索引原理

    Mysql索引主要包括四种,Btree索引.Hash索引.full-text全文索引.R-tree索引,因为作为一名PHP开发者,并不是专业的DBA,在这里只需要了解第一种开发相关的BTree索引. ...

  9. hdu_2048 错排问题

    错排问题本质上就是一个动态规划问题,其状态转移方程为: 记d[n]为n个人错排情况的总数. 那么策略可以描述为:分析第n个人错排的可能情况: 1)前n-1个人满足错排的情况,那么第n个人加入后还要错排 ...

  10. 【安装】ES的安装过程

    1.安装ES 首先我们需要去官网下载安装包  官方下载地址 下载后不需要编译,直接解压 解压后结构是这样的(2.5以上版本会有plugins目录,没有的需要手动创建) 方式一: 创建一个es用户(因为 ...