传送门

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

(设根的深度为 \(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. RF射频技术的原理

    [摘要]射频技术(RF)是Radio Frequency的缩写.较常见的应用有无线射频识别(Radio Frequency Identification,RFID),常称为感应式电子晶片或近接卡.感应 ...

  2. POJ 1067

    #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> usin ...

  3. POJ 1063

    #include <iostream> using namespace std; int main() { //freopen("acm.acm","r&qu ...

  4. js的基础知识笔记

    目录 一.基本数据类型 二.函数 三.面向对象 一.基本数据类型 1.使用var声明变量.使用;结尾.使用{}表示代码块.使用驼峰式命名 2.变量是弱类型的,即并不严格要求声明变量的类型,一个变量可以 ...

  5. 第6章—渲染web视图—SpringMVC+Thymeleaf 处理表单提交

    SpringMVC+Thymeleaf 处理表单提交 thymleaf处理表单提交的方式和jsp有些类似,也有点不同之处,这里操作一个小Demo,并说明: 1.demo的结构图如下所示: pom.xm ...

  6. VMware workstation 11 的下载

    不多说,直接上干货! VMWare Workstation 11的下载详细: 谷歌FQ,进入. 具体怎么达到可以FQ,见 FQ软件lantern-installer-beta.exe(推荐) 成功! ...

  7. GBDT多分类示例

    相当于每次都是用2分类,然后不停的训练,最后把所有的弱分类器来进行汇总 样本编号 花萼长度(cm) 花萼宽度(cm) 花瓣长度(cm) 花瓣宽度 花的种类 1 5.1 3.5 1.4 0.2 山鸢尾 ...

  8. MySQL查询时区分大小写

    在创建MySQL数据库时,下面这些参数可供我们选择:*_bin: 表示的是binary case sensitive collation,也就是说是区分大小写的 *_cs: case sensitiv ...

  9. java基本类型自动转换

    具体自动提升类型如上图所示.其中long->float的转换一开始让我感觉有点问题,因为long是64位的,而float却是32位的. 遂找寻答案,参考博客java中long到float的自动转 ...

  10. Eclipse svn 中文转成英文