Codeforces 750 F:New Year and Finding Roots
传送门
首先如果一开始就找到了一个叶子,那么暴力去递归找它的父亲,每次随机一个方向(除了已知的儿子)走深度次,如果走到了一个叶子就不是这个方向
(设根的深度为 \(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的更多相关文章
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 1214 F G H 补题记录
翻开以前打的 #583,水平不够场上只过了五题.最近来补一下题,来记录我sb的调试过程. 估计我这个水平现场也过不了,因为前面的题已经zz调了好久-- F:就是给你环上一些点,两两配对求距离最小值. ...
- codeforces 277.5 div2 F:组合计数类dp
题目大意: 求一个 n*n的 (0,1)矩阵,每行每列都只有两个1 的方案数 且该矩阵的前m行已知 分析: 这个题跟牡丹江区域赛的D题有些类似,都是有关矩阵的行列的覆盖问题 牡丹江D是求概率,这个题是 ...
- Codeforces 797 F Mice and Holes
http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test 1.5 ...
- 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 ...
- Codeforces 379 F. New Year Tree
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
随机推荐
- 使用SecureCRT的SFTP传输文件
使用SecureCRT的SFTP传输文件 使用 FileZilla 上传项目更新,因为软件缓存没处理好,三个文件花了三个小时~~ 找一种缓存干扰最小的方式上传文件. 1.在使用 SecureCRT 连 ...
- eclipse代码中使用到Launcher获取类加载器,找不到启动器类。
解决:移除系统依赖的jar包,重新导入. 只需要在project build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了 ...
- Eclipse启动和手动启动tomcat访问localhost:8080显示404问题总结
前言:建议对tomcat的文件结构和相关属性有较多了解.本文以eclipse的DynamicWebProject为讲解对象. 目录: eclipse添加tomcat关联注意点 tomcat404问题归 ...
- 【GDOI2015】 推箱子 状态压缩+bfs
请注意$8$是一个美妙的数字 考虑到$8\times 8=64$,而一个unsigned long long是$64$位的,所以考虑用一个$01$状态存储箱子.考虑到箱子能转动,那么四种情况都存一下就 ...
- 【bzoj4259】 残缺的字符串 FFT
又是一道FFT套路题 思路可以参考bzoj4503,题解 我们对串S和串T中出现的*处全部赋值为0. 反正最终的差异度式子大概就是 $C[i]=\sum_{j=0}^{|T|-1}S[i+j]T[j] ...
- 用Python玩转数据第六周——高级数据处理与可视化
1.matplotlib中有两个模块,pyplot和pylab import matplotlib.pyplot as plt ///plt.plot(x,y) import pylab as pl ...
- 详解XMLHttpRequest的跨域资源共享
0x00 背景 在Browser Security-同源策略.伪URL的域这篇文章中提到了浏览器的同源策略,其中提到了XMLHttpRequest严格遵守同源策略,非同源不可请求.但是,在实践当中,经 ...
- Android 开发工具类 16_NotificationActivity
在前台运行的 Activity 可以通过Dialog.Toast 向用户发出提示信息,而后台运行的程序,如下载.收到信息等 Service 应用,则需要使用 Notification(通知)向用户发出 ...
- WPF开发的彩票程序(练手好例子)
前言 WPF是.NET最新的界面开发库,开发界面非常灵活!但是学习WPF难度也非常大. 应朋友之邀,编写了一个小程序.程序虽小,五脏俱全,WPF开发的灵活性可窥见一斑. 对于新手学习有很好的借鉴意义, ...
- 一条命令在Centos7中换163 yum源、安装python3并与python2共存、使用豆瓣pip源加速
打开一个Terminal: 换yum源: mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup & ...