【codeforces 750F】New Year and Finding Roots
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的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 604D】Moodular Arithmetic
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【33.33%】【codeforces 608C】Chain Reaction
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- 4. Spring Boot 过滤器、监听器
转自:https://blog.csdn.net/catoop/article/details/50501688
- ospp.vbs是什么文件?激活过程cscript ospp.vbs命令详解
ospp.vbs是什么文件?激活过程cscript ospp.vbs命令详解 在Office 2013激活过程中我们经常会用到cscript ospp.vbs这个命令.那么,很有必要来了解一下,osp ...
- 对于学习apache软件基金会顶级项目源码的一点思路(转)
ASF的开源项目,为软件行业贡献了太多好的产品和软件思维.学习ASF的项目源码能很大的提升自身的能力.程序运行在服务器上的流程:执行启动脚本(start.sh) -> 指向程序的主方法 -> ...
- Python写爬虫-爬甘农大学校新闻
Python写网络爬虫(一) 关于Python: 学过C. 学过C++. 最后还是学Java来吃饭. 一直在Java的小世界里混迹. 有句话说: "Life is short, you ne ...
- 详解javascript的深拷贝与浅拷贝
1. 认识深拷贝和浅拷贝 javascript中一般有按值传递和按引用传递两种复制,按值传递的是基本数据类型(Number,String,Boolean,Null,Undefined),一般存放于内存 ...
- 程序员的困境 - R中国用户组-炼数成金
原文:http://www.oschina.net/news/43389/the-plight-of-programmer 在大型公司中不能腐蚀自己的学习能力和时间能力. 最近我为一个内核程序员的职位 ...
- 洛谷—— P1018 乘积最大
https://www.luogu.org/problem/show?pid=1018#sub 题目描述 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年 ...
- codeforces Round 246 D. Prefixes and Suffixes (后缀数组 || KMP)
题目大意: 求一个子串,子串既是前缀又是后缀. 然后再求出它在整个串中出现的次数. 思路分析: 能够非常easy想到怎样推断一个串既是前缀又是后缀. 仅仅须要它与 sa[0] 的lcp 等于 整个串的 ...
- MongoDbHelper 帮助类(下)
对MongoDbHelper帮助类进行了一下整合,但是代码中一个方法需要将string类型转化为BsonValue类型一直出错.所以欢迎留言指正 using System; using System. ...
- POJ 2983 Is the Information Reliable? 依旧差分约束
http://poj.org/problem?id=2983 题目大意: 星际大战开始了.你购买了情报,需要判断它的准确性.已知地方的根据地在由南向北排成一条直线.P A B X,表示A在B北面距离X ...