题目链接

\(Description\)

  有一棵高度为\(h\)的满二叉树,点从\(1\)到\(2^h-1\)编号(无序)。每次你可以询问一个点的编号,交互库会返回其所有邻接点的编号。你需要在\(16\)次询问内确定这棵树根节点的编号。

  \(h\leq 7\)。

\(Solution\)

  考虑随便问一个点,然后任意找个相邻点走。这样如果不往回走,最差情况下是一直走到一个叶子,这样找走两遍,扩展出一条叶子到叶子的链,就可以往上扩展了。这样最多扩展\(1+2+\ldots+7=28\)个点,但是确定根节点就够了,即\(21\)个。

  还是不行。在深度比较浅时代价会比较高,但是深度浅了我们离根节点就更近。所以在离根足够近(距离为\(2\))直接BFS。这样代价为\(10+1+2+4=17\),但是最后一个点不需要查知道了,代价为\(16\)。

  思路很好理解,但是代码好难写啊。。弃疗了。参考个吧。orz\(yanQval\).

  要对初始点DFS两次,不管路径如何,我们记下两条路径经过点数\(c_1,c_2\),其深度就是\(\frac{c_1+c_2}{2}+1\)。如果有一次是向根节点延伸(\(c_1\neq c_2\)),就可以直接跳到经过路径上最靠近根的点。

  之后保证每次向上走,用之前的深度和新路径的点数同样可以跳。最后手动BFS。

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
const int N=150; int h,dgr[N],son[N][3],A1[N],A2[N];
bool vis[N]; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
#define Check(x) if(dgr[x]==2) return x
inline void Query(int x)
{
vis[x]=1;
printf("? %d\n",x), fflush(stdout);
dgr[x]=read();
for(int i=0; i<dgr[x]; ++i) son[x][i]=read();
}
inline int Step(int x)
{
for(int i=0; i<dgr[x]; ++i) if(!vis[son[x][i]]) return son[x][i];
return son[x][0];
}
int Solve()
{
memset(vis,0,sizeof vis);
int h=read(), x=rand()%((1<<h)-1)+1, dep;
Query(x); Check(x);
if(dgr[x]==1) dep=1;
else
{
int cnt1=0, cnt2=0;
for(int v=Step(x); ; v=Step(v))
{
Query(v), A1[++cnt1]=v; Check(v);
if(dgr[v]==1) break;
}
for(int v=Step(x); ; v=Step(v))
{
Query(v), A2[++cnt2]=v; Check(v);
if(dgr[v]==1) break;
}
dep=(cnt1+cnt2>>1)+1;
if(cnt1>cnt2) x=A1[cnt1-dep+1];
else if(cnt1<cnt2) x=A2[cnt2-dep+1];
}
for(int cnt=0; dep<4/*not 5*/; cnt=0)
{
for(int v=Step(x); ; v=Step(v))
{
Query(v), A1[++cnt]=v; Check(v);
if(dgr[v]==1) break;
}
dep=dep+cnt+1>>1, x=A1[cnt-dep+1];
}
int a,b,c,d,e;
if(dep<h)
{
x=Step(x), Query(x); Check(x);
}
if(dep<h-1)
{
a=Step(x), Query(a); Check(a);
b=Step(x), Query(b); Check(b);
}
if(dep<h-2)
{
c=Step(a), Query(c); Check(c);
d=Step(a), Query(d); Check(d);
e=Step(b), Query(e); Check(e);
return Step(b);
}
return x;
} int main()
{
for(int T=read(); T--; printf("! %d\n",Solve()),fflush(stdout));
return 0;
}

Good Bye 2016 F.New Year and Finding Roots(交互)的更多相关文章

  1. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  2. 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) F dfs序+树状数组

    Performance ReviewEmployee performance reviews are a necessary evil in any company. In a performance ...

  3. Good Bye 2016

    A - New Year and Hurry (water) #include <bits/stdc++.h> using namespace std; int main() { ]; ; ...

  4. CF IndiaHacks 2016 F Paper task 后缀数组

    题目链接:http://codeforces.com/problemset/problem/653/F 大意是给出一个只包含'('和')'的括号串,求有多少不同的子串是合法的括号串 解法:对于每一个后 ...

  5. 2016-2017 ACM-ICPC East Central North America Regional Contest (ECNA 2016) F 区间dp

    Problem F Removal GameBobby Roberts is totally bored in his algorithms class, so he’s developed a li ...

  6. Good Bye 2015 F - New Year and Cleaning

    F - New Year and Cleaning 这题简直是丧心病狂折磨王.. 思路:容易想到这样一个转换,把整个矩形一起移动,矩形移出去的时候相当于一行或者一列. 为了优化找到下一个消去的点,我先 ...

  7. Good Bye 2014 F - New Year Shopping

    F - New Year Shopping 对于一种特殊的不可逆的dp的拆分方法.. 也可以用分治写哒. #include<bits/stdc++.h> #define LL long l ...

  8. April Fools Day Contest 2016 F. Ace It!

    F. Ace It! 题目连接: http://www.codeforces.com/contest/656/problem/F Description Input The only line of ...

  9. Good Bye 2016 A. New Year and Hurry【贪心/做题目每道题花费时间按步长为5等差增长,求剩余时间够做几道题】

    A. New Year and Hurry time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. eclipse启动tomcat内存溢出的解决方式

    eclipse启动tomcat内存溢出的解决方式 ——IT唐伯虎 摘要:eclipse启动tomcat内存溢出的解决方式. 1.打开Run Configurations 2.在VM arguments ...

  2. 纯CSS实现表单验证

    ladies and 乡亲们,表单验证你在做吗?客户端or服务器端,javascript or jquery,动手写 or 使用插件,今天我们来探索下使用纯css实现表单验证,借以学习css sele ...

  3. Nginx配置项优化(转载)

    (1)nginx运行工作进程个数,一般设置cpu的核心或者核心数x2 如果不了解cpu的核数,可以top命令之后按1看出来,也可以查看/proc/cpuinfo文件 grep ^processor / ...

  4. PyQT5 No module named ‘PyQt5.QtWebEngineWidgets’

    PyQT5查找不到模块QtWebEngineWidgets pip install pyqt5==5.10.1 或 安装64位的Pyhon解释器

  5. ZYNQ. DMA基本用法

    DMA环路测试 vivadoblock zynq7 + dma +fifo sdk 中可以导入 demo demo 中 默认都是 一个字节8bit数据 的测试程序. 如果是其他长度的数据,不仅要修改数 ...

  6. 【技巧总结】理解XXE从基础到盲打

    原文:http://agrawalsmart7.com/2018/11/10/Understanding-XXE-from-Basic-to-Blind.html 这篇文章中将讨论以下问题. XXE是 ...

  7. 关于iTerm2中颜色配置及快捷键使用技巧(亲测)

    https://github.com/mbadolato/iTerm2-Color-Schemes http://chriskempson.com/projects/base16 (同事用的) 按照g ...

  8. APK方法数超过65535及MultiDex解决方案

    以下参考自官方文档配置方法数超过 64K 的应用 随着 Android 平台的持续成长,Android 应用的大小也在增加.当您的应用及其引用的库达到特定大小时,您会遇到构建错误,指明您的应用已达到 ...

  9. 启动tomcat的时候爆出如下错误

    The JRE_HOME environment variable is not defined correctly This environment 解决办法: https://blog.csdn. ...

  10. django startproject xxx:报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 13: ordinal not in range(128)

    django startproject xxx:报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 13: o ...