题面
来源:FrontierCS 官方 statement.txt,与模型收到的题面相同。
This is an interactive problem.
You are given a tree of n
nodes with node 1
as its root node.
There is a hidden mole in one of the nodes. To find its position, you can pick an integer x
(1≤x≤n
) to make an inquiry to the jury. Next, the jury will return 1
when the mole is in subtree x
. Otherwise, the judge will return 0
. If the judge returns 0
and the mole is not in root node 1
, the mole will move to the parent node of the node it is currently on.
Use at most 160
operations to find the current node where the mole is located. If the number of operations is more than 160, you will get zero grade. Otherwise, your grade will be determined by the sum of the depth of the nodes in your query (the same node in two different queries will be counted twice). The depth of a node is the distance from the node to the root and the depth of the root is 0.
Input
Each test contains multiple test cases. The first line contains the number of test cases t
(1≤t≤100
). The description of the test cases follows.
Interaction
The first line of each test case contains one integer n
(2≤n≤5000
).
The following n−1
lines describe the edges of the tree. Each line contains two space-separated integers ui
and vi
(1≤ui,vi≤n
), indicating an edge between nodes ui
and vi
.
It is guaranteed that the input data represents a tree.
The interactor in this task is not adaptive. In other words, the node where the mole is located at first is fixed in every test case and does not change during the interaction.
To ask a query, you need to pick a vertex x
(1≤x≤n
) and print the line of the following form:
"? x"
After that, you receive:
0
if the mole is not in subtree x
;
1
if the mole is in subtree x
.
You can make at most 500
queries of this form for each test case. Apart from this condition, you need to try to minimize the sum of the depth of the nodes in your query.
Next, if your program has found the current node where the mole is located, print the line of the following form:
"! x"
Note that this line is not considered a query and is not taken into account when counting the number of queries asked.
After this, proceed to the next test case.
If you make more than 160
queries during an interaction, your program must terminate immediately, and you will receive the Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.
After printing a query or the answer for a test case, do not forget to output the end of line and flush the output. Otherwise, you will get the verdict Idleness Limit Exceeded. To do this, use:
fflush(stdout) or cout.flush() in C++;
System.out.flush() in Java;
flush(output) in Pascal;
stdout.flush() in Python;
see the documentation for other languages.
Example
InputCopy
2
2
1 2
1
6
1 2
1 3
1 4
4 5
5 6
0
0
1
OutputCopy
? 2
! 2
? 2
? 6
? 4
! 4
Note
In the first test case, the mole is in node 2
initially.
For the query "? 2", the jury returns 1
because the mole is in subtree 2
. After this query, the mole does not move.
The answer 2
is the current node where the mole is located, so the answer is considered correct.
In the second test case, the mole is in node 6
initially.
For the query "? 2", the jury returns 0
because the mole is not in subtree 2
. After this query, the mole moves from node 6
to node 5
.
For the query "? 6", the jury returns 0
because the mole is not in subtree 6
. After this query, the mole moves from node 5
to node 4
.
For the query "? 4", the jury returns 1
because the mole is in subtree 4
. After this query, the mole does not move.
The answer 4
is the current node where the mole is located, so the answer is considered correct.
Please note that the example is only for understanding the statement, and the queries in the example do not guarantee to determine the unique position of the mole.
## Scoring
Let x be D, the sum of query depths. R is the comparison depth sum stored with the test. The physical reference (the 60-point level) is a=R. Define the lower-band continuation:
L(x) = 60*max(0,(3*R-x)/(2*R)).
For positive x and positive physical reference a, define the improvement factor r=a/x. The physical reference has r=1 and earns 60 points. For a valid output on this branch, the points P are:
P = 100, if x=0;
P = L(x), if L(x)<=60;
P = min(100,60+ln(r)), otherwise.
Here ln is the natural logarithm. In the upper branch before the 100-point cap, r=exp(P-60): each additional point requires an e-fold improvement in the physical objective (division by e). If R=0, a valid D=0 receives 100 points and D>0 receives 0; the displayed lower formula is used only for R>0. All feasibility, correctness, and hard resource-limit checks remain required; invalid output receives 0.
Across test files, use the arithmetic mean of the file scores.
结果
Kimi K3.1 ProKimi
max
42.6
平均得分 ↑
- 42.6采样 1 · 部分分
- 31.1k平均输出 token(含思考) ↓
- 27.8输出速度 tokens/s ↑
- 1124 秒延迟 ↓
Kimi K3.1 Pro(high)Kimi high
high
42.6
平均得分 ↑
- 42.6采样 1 · 部分分
- 11.0k平均输出 token(含思考) ↓
- 23.5输出速度 tokens/s ↑
- 469 秒延迟 ↓
DeepSeek Flash V4.1-1017DeepSeek
auto(high)
42.3
平均得分 ↑
- 42.3采样 1 · 部分分
- 62.2k平均输出 token(含思考) ↓
- 156.0输出速度 tokens/s ↑
- 400 秒延迟 ↓
MiniMax M3.1 FlashMiniMax
auto(max)
42.6
平均得分 ↑
- 42.6采样 1 · 部分分
- 75.7k平均输出 token(含思考) ↓
- 70.0输出速度 tokens/s ↑
- 1083 秒延迟 ↓
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
Kimi K3.1 Pro · 采样 1 · 183 行
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; if (!(cin >> T)) return 0; while (T--) { int n; if (!(cin >> n)) return 0; vector<vector<int>> g(n + 1); for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } vector<int> parent(n + 1, 0), depth(n + 1, 0); vector<int> tin(n + 1), tout(n + 1), order; order.reserve(n); int timer = 0; function<void(int, int)> dfs = [&](int v, int p) { parent[v] = p; tin[v] = timer++; order.push_back(v); for (int u : g[v]) { if (u == p) continue; depth[u] = depth[v] + 1; dfs(u, v); } tout[v] = timer; }; dfs(1, 0); auto inSubtree = [&](int v, int x) { return tin[x] <= tin[v] && tin[v] < tout[x]; }; vector<int> candidates(n); iota(candidates.begin(), candidates.end(), 1); vector<int> sub(n + 1), endpoint(n + 1), excluded(n + 1); vector<int> cnt(n + 1), lastChild(n + 1); vector<char> inS(n + 1), group(n + 1), seen(n + 1); int queries = 0; while (true) { if (candidates.size() == 1) { cout << "! " << candidates[0] << endl; break; } if (queries >= 160) { cout << "! " << candidates[0] << endl; break; } const int m = (int)candidates.size(); fill(inS.begin(), inS.end(), 0); for (int v : candidates) inS[v] = 1; for (int v = 1; v <= n; ++v) { sub[v] = inS[v] ? 1 : 0; } for (auto it = order.rbegin(); it != order.rend(); ++it) { int v = *it; if (parent[v]) sub[parent[v]] += sub[v]; } fill(cnt.begin(), cnt.end(), 0); fill(lastChild.begin(), lastChild.end(), 0); fill(group.begin(), group.end(), 0); fill(endpoint.begin(), endpoint.end(), 0); int groupCount = 0; for (int v : candidates) { if (v == 1) continue; int u = parent[v]; if (!group[u]) { group[u] = 1; ++groupCount; } ++cnt[u]; lastChild[u] = v; } const bool rootCandidate = inS[1]; if (rootCandidate && !group[1]) { group[1] = 1; ++groupCount; } if (group[1]) { int e; if (rootCandidate || cnt[1] != 1) e = 1; else e = lastChild[1]; ++endpoint[e]; } for (int u = 2; u <= n; ++u) { if (!group[u]) continue; int e = (cnt[u] == 1 ? lastChild[u] : u); ++endpoint[e]; } excluded = endpoint; for (auto it = order.rbegin(); it != order.rend(); ++it) { int v = *it; if (parent[v]) excluded[parent[v]] += excluded[v]; } int bestX = -1; int bestWorst = INT_MAX; int bestDepth = INT_MAX; int bestSum = INT_MAX; for (int x = 2; x <= n; ++x) { int a = sub[x]; int b = groupCount - excluded[x]; if (a == m && b == 0) continue; int worst = max(a, b); int sum = a + b; if (worst < bestWorst || (worst == bestWorst && depth[x] < bestDepth) || (worst == bestWorst && depth[x] == bestDepth && sum < bestSum)) { bestX = x; bestWorst = worst; bestDepth = depth[x]; bestSum = sum; } } if (bestX == -1) { bestX = candidates[0]; for (int v : candidates) { if (depth[v] > depth[bestX]) bestX = v; } } cout << "? " << bestX << endl; ++queries; int answer; cin >> answer; if (answer != 0 && answer != 1) return 0; vector<int> next; next.reserve(candidates.size()); if (answer == 1) { for (int v : candidates) { if (inSubtree(v, bestX)) next.push_back(v); } } else { fill(seen.begin(), seen.end(), 0); for (int v : candidates) { if (inSubtree(v, bestX)) continue; int u = (v == 1 ? 1 : parent[v]); if (!seen[u]) { seen[u] = 1; next.push_back(u); } } } if (next.empty()) return 0; candidates.swap(next); } } return 0; }
Kimi K3.1 Pro(high) · 采样 1 · 137 行
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n; cin >> n; vector<vector<int>> g(n + 1); for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } int LOG = 1; while ((1 << LOG) <= n) ++LOG; vector<int> parent(n + 1), depth(n + 1), tin(n + 1), tout(n + 1); vector<vector<int>> up(LOG, vector<int>(n + 1)); vector<int> order; order.reserve(n); int timer = 0; function<void(int, int)> dfs = [&](int v, int p) { parent[v] = p; up[0][v] = p; for (int k = 1; k < LOG; ++k) up[k][v] = up[k - 1][up[k - 1][v]]; tin[v] = timer++; order.push_back(v); for (int to : g[v]) { if (to == p) continue; depth[to] = depth[v] + 1; dfs(to, v); } tout[v] = timer; }; parent[1] = 1; depth[1] = 0; dfs(1, 1); auto isAncestor = [&](int a, int b) { return tin[a] <= tin[b] && tout[b] <= tout[a]; }; auto lca = [&](int a, int b) { if (isAncestor(a, b)) return a; if (isAncestor(b, a)) return b; for (int k = LOG - 1; k >= 0; --k) { if (!isAncestor(up[k][a], b)) a = up[k][a]; } return parent[a]; }; vector<int> candidates(n); iota(candidates.begin(), candidates.end(), 1); while (candidates.size() > 1) { vector<int> positive(n + 1, 0), excluded(n + 1, 0); vector<int> groupLca(n + 1, 0); int mappedGroups = 0; for (int v : candidates) positive[v] = 1; for (int v : candidates) { int p = parent[v]; if (groupLca[p] == 0) { groupLca[p] = v; ++mappedGroups; } else { groupLca[p] = lca(groupLca[p], v); } } for (int p = 1; p <= n; ++p) { if (groupLca[p] != 0) ++excluded[groupLca[p]]; } for (int i = n - 1; i >= 1; --i) { int v = order[i]; positive[parent[v]] += positive[v]; excluded[parent[v]] += excluded[v]; } int bestX = 1; int bestWorst = (int)candidates.size() + 1; for (int x = 1; x <= n; ++x) { int negative = mappedGroups - excluded[x]; int worst = max(positive[x], negative); if (worst < bestWorst || (worst == bestWorst && depth[x] < depth[bestX])) { bestWorst = worst; bestX = x; } } cout << "? " << bestX << endl; int response; cin >> response; if (response == -1) return 0; vector<int> next; if (response == 1) { for (int v : candidates) { if (isAncestor(bestX, v)) next.push_back(v); } } else { for (int v : candidates) { if (!isAncestor(bestX, v)) next.push_back(parent[v]); } sort(next.begin(), next.end()); next.erase(unique(next.begin(), next.end()), next.end()); } candidates.swap(next); } cout << "! " << candidates[0] << endl; } return 0; }
DeepSeek Flash V4.1-1017 · 采样 1 · 165 行
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; const int SHALLOW = 8; while (T--) { int n; cin >> n; vector<vector<int>> adj(n + 1); for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } vector<int> parent(n + 1), depth(n + 1), tin(n + 1), tout(n + 1), order; order.reserve(n); int timer = 0; function<void(int, int)> dfs = [&](int v, int p) { parent[v] = p; tin[v] = timer++; order.push_back(v); for (int u : adj[v]) { if (u == p) continue; depth[u] = depth[v] + 1; dfs(u, v); } tout[v] = timer - 1; }; parent[1] = 1; depth[1] = 0; dfs(1, 1); vector<char> possible(n + 1, true); possible[0] = false; int m = n; int queryCount = 0; int answerNode = -1; while (m > 1 && queryCount < 160) { vector<int> cnt(n + 1, 0); for (int v = 1; v <= n; ++v) { cnt[v] = possible[v] ? 1 : 0; } for (int i = n - 1; i >= 0; --i) { int v = order[i]; if (v != 1) { cnt[parent[v]] += cnt[v]; } } int bestX = -1; if (queryCount < SHALLOW) { int bestDepth = INT_MAX; int bestC = -1; for (int v = 1; v <= n; ++v) { int c = cnt[v]; if (c >= 1 && c <= m - 1) { if (depth[v] < bestDepth || (depth[v] == bestDepth && c > bestC)) { bestDepth = depth[v]; bestC = c; bestX = v; } } } } else { int bestMax = INT_MAX; int bestDepth = INT_MAX; int bestC = -1; for (int v = 1; v <= n; ++v) { int c = cnt[v]; if (c >= 1 && c <= m - 1) { int mx = max(c, m - c); if (mx < bestMax || (mx == bestMax && depth[v] < bestDepth) || (mx == bestMax && depth[v] == bestDepth && c > bestC)) { bestMax = mx; bestDepth = depth[v]; bestC = c; bestX = v; } } } } if (bestX == -1) { for (int v = 1; v <= n; ++v) { if (possible[v]) { bestX = v; break; } } } cout << "? " << bestX << endl; int ans; cin >> ans; ++queryCount; vector<char> newPossible(n + 1, false); for (int v = 1; v <= n; ++v) { if (!possible[v]) continue; bool inside = (tin[bestX] <= tin[v] && tin[v] <= tout[bestX]); if (ans == 1) { if (inside) { newPossible[v] = true; } } else { if (!inside) { int p = parent[v]; newPossible[p] = true; } } } possible.swap(newPossible); m = 0; for (int v = 1; v <= n; ++v) { if (possible[v]) { ++m; answerNode = v; } } } if (m == 0) { answerNode = 1; } else if (m >= 1) { for (int v = 1; v <= n; ++v) { if (possible[v]) { answerNode = v; break; } } } cout << "! " << answerNode << endl; } return 0; }
MiniMax M3.1 Flash · 采样 1 · 245 行
#include <bits/stdc++.h> using namespace std; class InteractiveSolver { private: int n; vector<vector<int>> adj, children; vector<int> parent, depth, tin, tout, order, subtreeSize; vector<char> possible; int queryCount = 0; bool isInSubtree(int x, int v) const { return tin[x] <= tin[v] && tin[v] < tout[x]; } void buildTree() { parent.assign(n + 1, 0); depth.assign(n + 1, 0); tin.assign(n + 1, 0); tout.assign(n + 1, 0); subtreeSize.assign(n + 1, 1); children.assign(n + 1, {}); parent[1] = 1; vector<int> st; st.reserve(n); st.push_back(1); while (!st.empty()) { int v = st.back(); st.pop_back(); tin[v] = (int)order.size(); order.push_back(v); for (int u : adj[v]) { if (u == parent[v]) continue; parent[u] = v; depth[u] = depth[v] + 1; children[v].push_back(u); st.push_back(u); } } for (int i = n - 1; i > 0; --i) { int v = order[i]; subtreeSize[parent[v]] += subtreeSize[v]; } for (int v = 1; v <= n; ++v) { tout[v] = tin[v] + subtreeSize[v]; } } int ask(int x) { cout << "? " << x << endl; int response; if (!(cin >> response) || response == -1) { exit(0); } ++queryCount; return response; } void applyResponse(int x, int response) { if (response == 1) { for (int v = 1; v <= n; ++v) { if (possible[v] && !isInSubtree(x, v)) { possible[v] = false; } } } else { vector<char> nextPossible(n + 1, false); for (int v = 1; v <= n; ++v) { if (!possible[v] || isInSubtree(x, v)) continue; nextPossible[parent[v]] = true; } possible.swap(nextPossible); } } int countCandidates() const { int result = 0; for (int v = 1; v <= n; ++v) { result += possible[v]; } return result; } int findCandidate() const { for (int v = 1; v <= n; ++v) { if (possible[v]) return v; } return -1; } int chooseQuery() const { vector<int> candidateChildren(n + 1, 0); vector<int> hasCandidateChild(n + 1, 0); vector<int> subtreeCandidates(n + 1, 0); vector<int> subtreeParentGroups(n + 1, 0); for (int v = 2; v <= n; ++v) { if (possible[v]) { ++candidateChildren[parent[v]]; } } int parentGroups = 0; for (int v = 1; v <= n; ++v) { hasCandidateChild[v] = (candidateChildren[v] > 0); if (v != 1 && hasCandidateChild[v]) { ++parentGroups; } subtreeCandidates[v] = possible[v] ? 1 : 0; subtreeParentGroups[v] = hasCandidateChild[v]; } for (int i = n - 1; i > 0; --i) { int v = order[i]; int p = parent[v]; subtreeCandidates[p] += subtreeCandidates[v]; subtreeParentGroups[p] += subtreeParentGroups[v]; } int bestVertex = -1; int bestWorst = INT_MAX; int bestGain = -1; int bestDepth = INT_MAX; for (int x = 2; x <= n; ++x) { int yesCount = subtreeCandidates[x]; bool rootPresent = possible[1]; if (parent[x] == 1) { if (candidateChildren[1] - (possible[x] ? 1 : 0) > 0) { rootPresent = true; } } else { if (candidateChildren[1] > 0) { rootPresent = true; } } int excludedParent = 0; if (parent[x] != 1 && candidateChildren[parent[x]] == 1 && possible[x]) { excludedParent = 1; } int noCount = (rootPresent ? 1 : 0) + parentGroups - subtreeParentGroups[x] - excludedParent; if (noCount < 0) noCount = 0; if (yesCount == 0 && noCount == 0) continue; if (noCount == 0 && yesCount == countCandidates()) continue; int worst = max(yesCount, noCount); int gain = min(yesCount, noCount); if (bestVertex == -1 || worst < bestWorst || (worst == bestWorst && gain > bestGain) || (worst == bestWorst && gain == bestGain && depth[x] < bestDepth)) { bestVertex = x; bestWorst = worst; bestGain = gain; bestDepth = depth[x]; } } return bestVertex; } public: explicit InteractiveSolver(int n) : n(n), adj(n + 1), possible(n + 1, true) {} void addEdge(int u, int v) { adj[u].push_back(v); adj[v].push_back(u); } int solve() { buildTree(); possible[1] = true; while (true) { int candidates = countCandidates(); if (candidates == 1) { return findCandidate(); } if (queryCount >= 160) { exit(0); } int x = chooseQuery(); if (x == -1) { exit(0); } int response = ask(x); applyResponse(x, response); } } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; if (!(cin >> t)) return 0; while (t--) { int n; if (!(cin >> n)) return 0; InteractiveSolver solver(n); for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; solver.addEdge(u, v); } int answer = solver.solve(); cout << "! " << answer << endl; } return 0; }