传送门

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

(设根的深度为 \(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. 前端ajax传数据成功发送,但后端接收不到

    前几天遇到这样的问题,找了好久,是在ajax     contentType属性设置的问题. contentType默认是application/x-www-form-urlencoded    但是 ...

  2. javaweb 项目的异常处理

    首先关于异常的分类: java 中关于异常的分类情况是:throwable 是所有异常和错误的基类,下面在分为Error 和 Exception: 简单的异常体系结构如下图所示: 其中Exceptio ...

  3. gitlab改变服务器ip

    一./home/git/gitlab/config/unicorn.rb 其中的ip需要修改,否则unicorn无法启动. 二./home/git/gitlab-shell/config.yml 其中 ...

  4. SQL与NOSQL

    一:关系型数据库 1.概念: 采用了关系模型来组织数据的数据库.简单讲,关系模型就是二维表格模型.二维表格在              数据库中我们称之为记录,列在数据库中我们成为字段. 2举例: M ...

  5. java基本语法二

    1 运算符 1.1 运算符的概念 运算符是一种特殊的符号,用以表示数据的运算.赋值和比较等. 在java语言中,运算符有如下的分类: ①算术运算符. ②赋值运算符. ③比较运算符(关系运算符). ④逻 ...

  6. ERROR:org.apache.hadoop.hbase.PleaseHoldException: Master is initializing 解决方案

    我尝试的过程如下 1. 时间没有同步 用date命令看一下每个机器 如果时间差距大 说明确实有问题 ** 配置时间服务器             ** 检查时区                 $ d ...

  7. apache URL重写 标志表 以及 错误解决方法

    Apache mod_rewrite规则重写的标志一览 1) R[=code](force redirect) 强制外部重定向 强制在替代字符串加上http://thishost[:thisport] ...

  8. postman—集成到jenkins

    前言 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. 将postman导出的脚本,持续集成到 ...

  9. gulp4.0 前端构建脚手架

    最近看了下gulp4.0的升级,感觉和3.0相比变化还是比较大的,很多3.0的写法和插件会出现一些莫名其妙的变化,详细的变化就先不说了,这里我直接把我配置好的代码拿过来吧,方便各位可以更好的学习和使用 ...

  10. java学习--Reflection反射机制

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制. ...