QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#717807 | #9570. Binary Tree | ucup-team5217 | WA | 1ms | 3736kb | C++23 | 2.5kb | 2024-11-06 18:59:55 | 2024-11-06 18:59:55 |
Judging History
answer
/**
* @file 9570.cpp
* @author Macesuted ([email protected])
* @date 2024-11-06
*
* @copyright Copyright (c) 2024
*
*/
#include <bits/stdc++.h>
using namespace std;
bool mem1;
typedef pair<int, int> pii;
vector<bool> ban;
vector<int> siz;
vector<vector<int>> graph;
int lim;
int query(int x, int y) {
assert(lim--);
cout << "? " << x << ' ' << y << endl;
int ret;
cin >> ret;
return ret;
}
void getSiz(int p, int fa = -1) {
if (ban[p]) return siz[p] = 0, void();
siz[p] = 1;
for (auto i : graph[p])
if (i != fa) getSiz(i, p), siz[p] += siz[i];
return;
}
pii fndRoot(int p, int n, int fa = -1) {
pii cur = {n - siz[p], p}, ans = {n, 0};
for (auto i : graph[p])
if (i != fa) cur.first = max(cur.first, siz[i]), ans = min(ans, fndRoot(i, n, p));
return min(ans, cur);
}
int solve(int p) {
getSiz(p), p = fndRoot(p, siz[p]).second;
vector<int> neigh;
for (auto i : graph[p])
if (!ban[i]) neigh.push_back(i);
if (neigh.empty()) return p;
if (neigh.size() == 1) return query(p, neigh[0]) == 1 ? p : neigh[0];
if (neigh.size() == 2) {
int ret = query(neigh[0], neigh[1]);
if (ret == 1) return ban[p] = true, solve(neigh[0]);
if (ret == 3) return ban[p] = true, solve(neigh[1]);
return p;
}
getSiz(p);
sort(neigh.begin(), neigh.end(), [&](int a, int b) { return siz[a] > siz[b]; });
int ret = query(neigh[0], neigh[1]);
if (ret == 1) return ban[p] = true, solve(neigh[0]);
if (ret == 3) return ban[p] = true, solve(neigh[1]);
return ban[neigh[0]] = ban[neigh[1]] = true, solve(p);
}
void solve(void) {
int n;
cin >> n, lim = log2(n);
ban.clear(), ban.resize(n + 1);
siz.clear(), siz.resize(n + 1);
graph.clear(), graph.resize(n + 1);
for (int i = 1, cl, cr; i <= n; i++) {
cin >> cl >> cr;
if (cl) graph[i].push_back(cl), graph[cl].push_back(i);
if (cr) graph[i].push_back(cr), graph[cr].push_back(i);
}
int ans = solve(1);
cout << "! " << ans << endl;
return;
}
bool mem2;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif
int _ = 1;
cin >> _;
while (_--) solve();
#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3736kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1
output:
? 3 1 ! 1
result:
wrong answer There are 2 candidates. (test case 1)