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 ...
随机推荐
- 读DEDECMS找后台目录有感
本文作者:红日安全团队——Mochazz 早上看了先知论坛的这篇文章:解决DEDECMS历史难题–找后台目录 不得不说作者思路确实巧妙,作者巧妙的利用了Windows FindFirstFile和织梦 ...
- 2 rocketmq mqadmin 的用法详解
参考文档 http://jameswxx.iteye.com/blog/2091971 1.1. 控制台使用 RocketMQ 提供有控制台及一系列控制台命令,用于管理员对主题,集群,broker 等 ...
- git 删除 repository
git 删除 repository 打开版本库,选择要删除的repository 点击Settings 找到删除选项 输入repository name,点击delet…… 删除本地仓库 执行git ...
- POJ 1183
#include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...
- 转:android studio 一直卡在Gradle:Build Running的解决办法
在使用AS开发安卓应用程序的时候经常会遇到Gradle build running一直在运行甚至卡死的情况,解决方法如下: 方法1: 1.在C:\User\<用户名>\.gradle 目录 ...
- 剑指offer五十四之字符流中第一个不重复的字符
一.题目 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...
- 剑指offer二十九之最小的K个数
一.题目 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 二.思路 详解代码. 三.代码 import java.util. ...
- 按照Right-BICEP要求对实验二进行测试
我的代码实现的功能很简单,在最基本的功能上,包括有无括号(0/1),有无负数(0/1),有无乘除法(0/1)验证程序的正确性,测试用例为8个,2^3个,也就覆盖了所有的代码路径. 测试计划: 基本功能 ...
- android GridLayout布局
android4.0版本后新增了一个GridLayout,它使用虚细线将布局划分为行.列和单元格,也支持一个控件在行.列上都有交错排列,其实用方法和LinearLayout,Relativelayou ...
- 浅谈Android Studio中项目结构中project模式的各个文件和文件夹
致敬郭霖,这些知识是从第一行代码第二版中直接码下来的,谢谢他,注意每个条目前是否有. 1..gradle和.idea 这两个目录下放置的都是Android Studio自动生成的一些文件,我们无需关心 ...