存活千年
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
更新
直接将下发文件贴在了题面
题目背景


因为椎名需要写作业的时间而美美总是来黏着她,所以椎名决定给美美出一道题目供她打发时间。
但是椎名不能把题目出得太难,否则会把美美急哭。
所以椎名想了一会,出了一道很简单但足够难倒美美的题目了。
题目描述
这是一道交互题。
椎名有一棵包含 个节点的树,节点编号为 ,其中 号节点为根。每条边的长度均为 。树中隐藏着一个目标节点 (),美美需要通过 次询问来确定这个目标节点。
每次询问,美美可以指定一个节点 ,椎名的交互库会返回目标节点 到 的距离(即树上最短路径的边数)。
需要注意的是,美美不知道树的具体结构,只能通过询问获得距离信息,她需要设计策略,在不超过 次询问内确定目标节点。
美美需要实现一个函数 findTarget,其原型如下:
int findTarget(int n);
- 参数 表示树的节点个数。
- 函数返回所求目标节点的编号。
美美可以调用交互库提供的函数:
int query(int u);
query(u):询问编号为 的节点到目标节点的距离。- 必须是 之间的整数。
- 函数返回值为编号为 的节点到目标节点的距离。
- 如果 超出范围,交互库会立即终止程序并报告错误。
- 如果调用次数超过 次,交互库也会终止程序并报告错误。
输入格式
本题为交互题,不需要处理输入。
输入数据由交互库读取,不会直接传递给选手程序。
输出格式
提交的代码不需要输出任何内容,只需在 findTarget 中返回目标节点的编号即可。
样例
假设 ,目标节点为 ,树的结构为:
3
1 2
1 3
即 之间有边。
选手的 findTarget 函数可能按如下方式交互:
-
调用
query(1),返回 (因为节点 到节点 的距离为 )。 -
调用
query(2),返回 (目标节点就是 )。 -
函数返回 。
-
该样例中使用了 次询问,未超过 次。
提示
你需要实现 findTarget 函数,并包含头文件 findTarget.h。下面给出一个示例代码:
#include "findTarget.h"
int findTarget(int n) {
// 实现你的算法
// 你可以通过 query(u) 来询问距离
// 最后返回目标节点编号
// 例如:
int ans = 1;
// 这里应使用查询策略
// 比如这样(注意这个策略显然是错误的):
if(query(114514)==n-2) ans=n-1;
return ans;
}
注意事项
-
你的程序必须包含头文件 findTarget.h。
-
不要自行实现
main函数,否则会导致编译错误。 -
保证
query的调用次数不超过 。 -
输入树的结构由交互库读取,你无法直接获得。
-
为防止通过恶意输入破坏交互库,交互库会对查询节点范围和次数进行严格检查,一旦违反即终止程序。
本题附加文件中有交互库 grader.cpp 及头文件 findTarget.h,同时提供了一组样例输入输出 sample.in/sample.ans。
请注意实际评测中交互库与下发的交互库存在差异,请勿尝试通过观察下发交互库的代码结构以非法手段通过此题。
如果你不知道如何在本地调试函数交互题代码,参见讨论,对函数交互题存在疑问也可在该讨论中询问。
数据范围
。
附加文件
处于某些原因附加文件无法提供,直接在此处发放。
sample.in
3
1 2
1 3
sample.ans
AC-nya!
findTarget.h
// findTarget.h
#ifndef FINDTARGET_H
#define FINDTARGET_H
int query(int u);
int findTarget(int n);
#endif
findtarget.cpp
// findtarget.cpp(交互库实现)
#include "findTarget.h"
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cassert>
#include <queue>
#include <functional>
#include <algorithm>
using namespace std;
static int n, target;
static vector<vector<int>> g;
static vector<int> depth;
static vector<vector<int>> up;
static int LOG;
static int query_cnt = 0;
void preprocess_lca() {
LOG = 0;
while ((1 << LOG) <= n) ++LOG;
up.assign(n + 1, vector<int>(LOG + 1, 0));
depth.assign(n + 1, 0);
function<void(int, int)> dfs = [&](int u, int p) {
up[u][0] = p;
for (int i = 1; i <= LOG; ++i) {
up[u][i] = up[up[u][i - 1]][i - 1];
}
for (int v : g[u]) {
if (v != p) {
depth[v] = depth[u] + 1;
dfs(v, u);
}
}
};
dfs(1, 0);
}
int lca(int a, int b) {
if (depth[a] < depth[b]) swap(a, b);
int diff = depth[a] - depth[b];
for (int i = 0; i <= LOG; ++i) {
if (diff >> i & 1) a = up[a][i];
}
if (a == b) return a;
for (int i = LOG; i >= 0; --i) {
if (up[a][i] != up[b][i]) {
a = up[a][i];
b = up[b][i];
}
}
return up[a][0];
}
int dist(int u, int v) {
int l = lca(u, v);
return depth[u] + depth[v] - 2 * depth[l];
}
int query(int u) {
if (u < 1 || u > n) {
cerr << "Invalid query: node " << u << " out of range [1, " << n << "]" << endl;
exit(1);
}
if (++query_cnt > n - 1) {
cerr << "Too many queries: " << query_cnt << " > " << n - 1 << endl;
exit(1);
}
return dist(u, target);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
g.resize(n + 1);
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
preprocess_lca();
srand(2023);
target = rand() % n + 1;
int ans = findTarget(n);
if (ans == target) {
cout << "AC-nya!";
} else {
cout << "WA-nya!";
}
return 0;
}