传送门

首先如果一开始就找到了一个叶子,那么暴力去递归找它的父亲,每次随机一个方向(除了已知的儿子)走深度次,如果走到了一个叶子就不是这个方向

(设根的深度为 \(1\))这样子最后到达深度为 \(3\) 的点需要花费 \(11\) 次

注意到此时只有与该点距离不超过 \(2\) 的点可能是根,这样的没有询问过的点不超过 \(6\) 个

所以只要询问 \(5\) 次,一共 \(16\) 次

如果一开始不是叶子,那么尝试 \(dfs\) 到一个叶子,最后再套用上面的做法

注意每次随机一个方向的时候要判断之前是否已经有一条长度为深度的链(也可以优先选择询问过的点)

# include <bits/stdc++.h>
using namespace std;
typedef long long ll; int h, pre[233], vis[233], n, flg, rt, leaf, actur[233], tot;
vector <int> son[233]; inline void Ask(int u) {
if (vis[u]) return;
int cnt, i, v;
vis[u] = 1, printf("? %d\n", u), fflush(stdout);
scanf("%d", &cnt);
for (i = 0; i < cnt; ++i) scanf("%d", &v), son[u].push_back(v);
if (son[u].size() == 2) {
rt = u, flg = 1;
return;
}
} void Dfs(int cur) {
if (leaf || flg) return;
int i, cnt;
Ask(cur), cnt = son[cur].size();
if (cnt == 1) {
leaf = cur;
return;
}
if (leaf || flg) return;
for (i = 0; i < cnt; ++i)
if (!vis[son[cur][i]]) {
pre[son[cur][i]] = cur, Dfs(son[cur][i]);
return;
}
} inline int Check(int u, int ff, int d) {
if (d < 0) return 0;
Ask(u);
if (flg) return 1;
if (son[u].size() == 1) return d ? 0 : actur[u] = 1;
int i;
for (i = 0; i < 3; ++i)
if ((son[u][i] ^ ff) && vis[son[u][i]]) {
if (Check(son[u][i], u, d - 1)) return actur[u] = 1;
return 0;
}
for (i = 0; i < 3; ++i)
if ((son[u][i] ^ ff) && !vis[son[u][i]]) {
if (Check(son[u][i], u, d - 1)) return actur[u] = 1;
return 0;
}
} inline int Getfa(int x, int nh) {
Ask(x);
if (son[x].size() == 1) return son[x][0];
if (flg) return 0;
int p1, p2, i;
for (i = 0; i < 3; ++i) if (actur[son[x][i]]) break;
if (i == 0) p1 = 1, p2 = 2;
else if (i == 1) p1 = 0, p2 = 2;
else p1 = 0, p2 = 1;
p1 = son[x][p1], p2 = son[x][p2];
if (vis[p2]) swap(p1, p2);
return Check(p1, x, h - nh - 1) ? p2 : p1;
} void Dfs2(int u, int d) {
if (d > 2 || flg) return;
if (tot == 6) {
rt = u, flg = 1;
return;
}
Ask(u), ++tot;
if (flg) return;
int i, cnt = son[u].size();
for (i = 0; i < cnt; ++i)
if (!actur[son[u][i]]) Dfs2(son[u][i], d + 1);
} inline void Solve() {
int cur, i, nh;
memset(pre, 0, sizeof(pre));
memset(vis, 0, sizeof(vis));
memset(actur, 0, sizeof(actur));
scanf("%d", &h), n = (1 << h) - 1, leaf = flg = 0, nh = h;
for (i = 1; i <= n; ++i) son[i].clear();
cur = rand() % n + 1, Dfs(cur);
if (flg) {
printf("! %d\n", rt), fflush(stdout);
return;
}
cur = leaf, actur[cur] = 1;
while (nh > 3) {
cur = Getfa(cur, nh), --nh, actur[cur] = 1;
if (flg) {
printf("! %d\n", rt), fflush(stdout);
return;
}
}
tot = 0, Dfs2(cur, 0);
printf("! %d\n", rt), fflush(stdout);
} int main() {
srand(time(NULL));
int test;
scanf("%d", &test);
while (test--) Solve();
return 0;
}

Codeforces 750 F:New Year and Finding Roots的更多相关文章

  1. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  2. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  3. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  4. Codeforces 1214 F G H 补题记录

    翻开以前打的 #583,水平不够场上只过了五题.最近来补一下题,来记录我sb的调试过程. 估计我这个水平现场也过不了,因为前面的题已经zz调了好久-- F:就是给你环上一些点,两两配对求距离最小值. ...

  5. codeforces 277.5 div2 F:组合计数类dp

    题目大意: 求一个 n*n的 (0,1)矩阵,每行每列都只有两个1 的方案数 且该矩阵的前m行已知 分析: 这个题跟牡丹江区域赛的D题有些类似,都是有关矩阵的行列的覆盖问题 牡丹江D是求概率,这个题是 ...

  6. Codeforces 797 F Mice and Holes

    http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 ...

  7. Codeforces 622 F. The Sum of the k-th Powers

    \(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i ...

  8. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

  9. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

随机推荐

  1. node 无脑生成小程序二维码图

    RT 新建createwxaqrcode.js: const request = require('request') const fs = require('fs') // eg:生成购物车列表圆形 ...

  2. MySQL 高级查询操作

    目录 MySQL 高级查询操作 一.预告 二.简单查询 三.显示筛选 四.存储过程 五.查询语句 1.作为变量 2.函数调用 3.写入数据表 备注 附表一 附表二 相关文献 博客提示 MySQL 高级 ...

  3. Tomcat启动内存设置

    Tomcat启动内存设置 Tomcat的启动分为startupo.bat启动和注册为windows服务的启动,下面一一说明. 1.startup.bat启动 在tomcat_home/bin目录下找到 ...

  4. 【BZOJ4184】shallot 线性基

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4184 此题如果我们不考虑删除元素这一个操作,那么就是一道裸的线性基题. 但是此题会删除 ...

  5. 【poj3252】 Round Numbers (数位DP+记忆化DFS)

    题目大意:给你一个区间$[l,r]$,求在该区间内有多少整数在二进制下$0$的数量$≥1$的数量.数据范围$1≤l,r≤2*10^{9}$. 第一次用记忆化dfs写数位dp,感觉神清气爽~(原谅我这个 ...

  6. Oracle 数据库维护管理之--数据库基本信息表管理与优化参考1

    1.查看当前系统中的会话(如果权限不足,请使用sys或者system用户登录): select * from v$session t; 2.查看此会话下正在执行的sql语句:select sql_te ...

  7. Elasticsearch四种常见的相关度分数优化方法

    **1.boost方式 ** 简单粗暴,最常用. 需求:查询出title和content中包含java spark的document 方式1: GET /forum/article/_search { ...

  8. addEventListener和attachEvent的区别 分类: JavaScript 2015-05-12 19:03 702人阅读 评论(0) 收藏

    addEventListener共有3个参数,如下所示:element.addEventListener(type,listener,useCapture); 参数 参数说明 element 要绑定事 ...

  9. iphoneX 适配

    1.iphoneX的尺寸  375*812   上边有44px的危险区,下边有34px的危险区,剩下的是安全区. 2.viewport设值cover <meta name="viewp ...

  10. spring-data-redis配制

    1:单redis模式下 properties文件 配制 #JedisPoolConfig的参数 #最大连接数 redis.pool.maxTotal= #最大空闲时间 redis.pool.maxId ...