time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

This is an interactive problem. In the interaction section below you will find the information about flushing the output.

The New Year tree of height h is a perfect binary tree with vertices numbered 1 through 2h - 1 in some order. In this problem we assume that h is at least 2. The drawing below shows one example New Year tree of height 3:

Polar bears love decorating the New Year tree and Limak is no exception. To decorate the tree, he must first find its root, i.e. a vertex with exactly two neighbours (assuming that h ≥ 2). It won’t be easy because Limak is a little bear and he doesn’t even see the whole tree. Can you help him?

There are t testcases. In each testcase, you should first read h from the input. Then you can ask at most 16 questions of format “? x” (without quotes), where x is an integer between 1 and 2h - 1, inclusive. As a reply you will get the list of neighbours of vertex x (more details in the “Interaction” section below). For example, for a tree on the drawing above after asking “? 1” you would get a response with 3 neighbours: 4, 5 and 7. Your goal is to find the index of the root y and print it in the format “! y”. You will be able to read h for a next testcase only after printing the answer in a previous testcase and flushing the output.

Each tree is fixed from the beginning and it doesn’t change during your questions.

Input

The first line of the input contains a single integer t (1 ≤ t ≤ 500) — the number of testcases.

At the beginning of each testcase you should read from the input a single integer h (2 ≤ h ≤ 7) — the height of the tree. You can’t read the value of h in a next testcase until you answer a previous testcase.

Interaction

To ask a question about neighbours of vertex x, print “? x” (without quotes) on a separate line. Note, you must print an end-of-line character after the last character of the line and flush your output to get a response.

The response will consist of two lines. The first line will contain a single integer k (1 ≤ k ≤ 3) — the number of neighbours of vertex x. The second line will contain k distinct integers t1, …, tk (1 ≤ t1 < … < tk ≤ 2h - 1) — indices of neighbours of vertex x, gives in the increasing order.

After asking at most 16 questions you have to say y — the index of the root. Print “! y” (without quotes) and an end-of-line character, and flush the output.

Each tree is fixed from the beginning and it doesn’t change during your questions.

You can get Idleness Limit Exceeded if you don’t print anything or if you forget to flush the output.

To flush you can use (just printing a query/answer and end-of-line):

fflush(stdout) in C++;

System.out.flush() in Java;

stdout.flush() in Python;

flush(output) in Pascal;

See the documentation for other languages.

In any moment if the program reads h = 0 or k = 0 it should immediately terminate normally (for example, calling exit(0)). It means that the system detected incorrect request/output from your program and printed 0 because if can’t process your requests anymore. In this case you’ll receive verdict “Wrong Answer”, but if you ignore case h = 0 or k = 0 it could lead to “Runtime Error”, “Time/Memory limit exceeded” or any other verdict because your program could read a trash from the closed input stream.

Hacking. To hack someone, use the following format:

The first line should contain a single integer t equal to 1 (only one testcase is allowed in hacks). The second line should contain a single integer h. Each of next 2h - 2 lines should contain two distinct integers ai and bi (1 ≤ ai, bi ≤ 2h - 1), denoting two nodes connected with an edge. The printed edges must form a perfect binary tree of height h.

Of course, contestant programs will not be able to see this input.

Examples

input

1

3

3

4 5 7

2

1 2

1

2

output

? 1

? 5

? 6

! 5

input

2

2

1

3

2

1 2

2

1 2

4

3

3 12 13

output

? 1

? 3

? 3

! 3

? 6

! 1

Note

In the first sample, a tree corresponds to the drawing from the statement.

In the second sample, there are two two testcases. A tree in the first testcase has height 2 and thus 3 vertices. A tree in the second testcase has height 4 and thus 15 vertices. You can see both trees on the drawing below.

【题目链接】:http://codeforces.com/contest/750/problem/F

【题解】

/*
首先任意找一个点v;
如果它的邻居数为x
如果x==1(x==3的话就要找两个叶子节点了,x==2的情况归到x==3的情况里面去);
{
则这个点是叶子节点;
则我们以这个点为起点再沿着它的其他邻居走,直到找到另外一个叶子节点为止;
设另外一个叶子节点为u;
则记录一下v到u的路径,找到这段路程的中点,则这个中点肯定是这段路程的
所有节点里面高度最高的(树的叶子节点高度设为1,然后往上递增);
设中间这个节点为y
则y如果只有2个邻居,那么它就是根节点;
如果y有3个邻居,则沿着那第3个节点找(肯定是在往上走了,则我们更靠近根节点了;
然后对那个节点做同样的事情;
即也从那个节点开始一直找啊找,找叶子节点u';
根据我们刚才找到的y的高度->就是路程除2,,再根据从y到u'的路程,我们也能算出来
从叶子节点u到u'这个路程中的高度最高的节点y';
且它的高度高于y节点;
这样就又迫近根节点了;
..一直纪录新的y'节点的高度;
直到它的高度大于等于4(或等于n了);
大于等于4之后,剩下的节点就不用再用上面的方法了;
直接就可以枚举出来了;
当高度为n-1
则答案就是剩下的那个;
当高度为n-2;
则需要试探3次;
当高度为n-3;
则需要试探7次;
最后一个根节点可以用排除法,这样就少试一次了;
画一画、发现不会超过16次.balabala;
如下图,是最坏情况;
黑色表示查询的次数,黑色数字旁的节点是这次查询的节点;
(高度为n-3及以上的时候,只要有限次的搜寻就能确定根节点了)
(bfs最多两层);
}
*/

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXN = 256;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); int h,v,num[256],xl1[MAXN],xl2[MAXN],si0,si1;
vector <int> g[256];
bool bo[256]; void get(int x)
{
bo[x] = true;
printf("? %d\n",x);
fflush(stdout);
rei(num[x]);
rep1(i,1,num[x])
{
int y;
rei(y);
g[x].pb(y);
}
} int nex(int x)
{
int len = g[x].size();
rep1(i,0,len-1)
if (!bo[g[x][i]])
return g[x][i];
return g[x][0];
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
int T;
rei(T);
while (T--)
{
memset(bo,false,sizeof bo);
rep1(i,1,200)
g[i].clear(),num[i] = 0;
int n,dep;
rei(n);
si0 = 0,si1 = 0;
v = 1;
get(v);
if (num[v]==1)
dep = 1;
else
{
int x = v,y =v;
xl1[++si0] = v;
while (true)
{
int yy = nex(x);
xl1[++si0] = yy;
get(yy);
x= yy;
if (num[yy] == 1)
break;
}
while (true)
{
int yy = nex(y);
xl2[++si1] = yy;
get(yy);
y = yy;
if (num[yy] == 1)
break;
}
dep = (si0+si1+1)/2;
if (si0>si1)
v = xl1[dep-si1];
else//si0<si1 si0!=si1
v = xl2[dep-si0];
}
while (dep < n && dep<4)
{
si0 = 0;
int x = v;
while (true)
{
int u = nex(x);
get(u);
xl1[++si0] = u;
x = u;
if (num[u]==1)
break;
}
int tdep = (dep+si0+1)/2;
v = xl1[tdep-dep];
dep = tdep;
}
int q,w,e,r,t,y,u;
if (dep<n)
{
q = nex(v);
get(q);
if (num[q]==2)
v = q;
}
if (dep<n-1)
{
w = nex(q);get(w);if (num[w]==2) v = w;
e = nex(q);get(e);if (num[e]==2) v = e;
}
if (dep<n-2)
{
r = nex(w);get(r);if (num[r]==2) v = r;
t = nex(w);get(t);if (num[t]==2) v = t;
y = nex(e);get(y);if (num[y]==2) v = y;
u = nex(e);if (num[v]!=2) v = u;
}
printf("! %d\n",v);
fflush(stdout);
}
return 0;
}

【codeforces 750F】New Year and Finding Roots的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 604D】Moodular Arithmetic

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【33.33%】【codeforces 608C】Chain Reaction

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  5. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  6. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  7. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  8. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  9. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

随机推荐

  1. cookie记住用户名密码

    <script src="js/jquery.cookie.js" type="text/javascript"></script> $ ...

  2. .NET中StringBuilder用法实例分析

    string s1 = "33"; string s2 = "44"; string s3 = "55"; //需求是把s1 s2 s3拼接 ...

  3. 无法显式调用运算符或访问器 错误处理方法 DLL study

    无法显式调用运算符或访问器 错误处理方法 转 无法显式调用运算符或访问器 错误处理方法 反汇编一个dll类库,导出的项目会报出很多bug,其中主要的就是“无法显式调用运算符或访问器”这个错误,看了一下 ...

  4. [置顶] WebService学习总结(4)——第三方webService服务调用

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...

  5. uva 10098 Generating Fast(全排列)

    还是用的两种方法,递归和STL,递归那个是含有反复元素的全排列,这道题我 没有尝试没有反复元素的排列,由于从题目上并没有发现一定是有反复元素的() 贴代码: <span style=" ...

  6. linux终端下一些“风骚”的按键操作及Linux终端命令

    linux终端下一些"风骚"的按键操作 <backspace>  删除 <ctrl-l>     清空屏幕, 相当于clear tab            ...

  7. (转)在server 2008R2组策略设置所有域计算机防火墙都处于更关闭状态

    组策略在域控中相当重要,我们可以下放一个组策略去统一管理下面客户端的配置,具体配置如下: 首先点击开始____管理工具____组策略管理 防火墙关闭完之后我们该如何到客户端验证呢? 首先我们需要现在客 ...

  8. Genymotion加入模拟器时报“Unable to create virtual device,Server returned HTTP status code 0”

    今天也遇到这个问题,算是对这个文章的一点补充 打开图中这个文件 C:\Users\xxx\AppData\Local\Genymobile 搜索 [downloadFile] 找到这个一串URL ht ...

  9. CleanCode代码整洁之道培训总结(2015-03-14)

    为期四天的CleanCode培训时间非常短.非常难准确掌握一些知识.但让我对代码有了一个又一次的认识和启示:之前也有看过设计模式.重构之类的书,看完之后也有一些感触,过后在写代码中还是不能应用进来,事 ...

  10. Yarn架构基本概况(一)

    1)引言 针对MRv1在扩展性.可靠性,资源利用率和多框架的支持上存在着明显的不足.进而诞生了下一代的MapReduce的计算框架MapReduce Version2,MRV1中有一个非常大的问题就是 ...