QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#731126 | #9570. Binary Tree | ucup-team5799 | TL | 1ms | 3504kb | C++20 | 4.3kb | 2024-11-10 00:01:00 | 2024-11-10 00:01:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n; cin >> n;
vector<vector<int>> G(n + 1);
for (int i = 1;i <= n;i++) {
int a, b; cin >> a >> b;
if (a) {
G[a].push_back(i);
G[i].push_back(a);
}
if (b) {
G[b].push_back(i);
G[i].push_back(b);
}
}
if (n == 2) {
cout << "? 1 2" << endl;
int op; cin >> op;
if (op == 0) cout << "! 1" << endl;
else cout << "! 2" << endl;
return;
}
auto getCenter = [&]() {
int s = 0, maxdep = 0;
auto dfs1 = [&](auto&&dfs1, int u, int fa, int dep) -> void {
if (dep > maxdep) {
maxdep = dep;
s = u;
}
for (int v : G[u]) {
if (v == fa) continue;
dfs1(dfs1, v, u, dep + 1);
}
};
dfs1(dfs1, 1, 1, 0);
int t = s; maxdep = 0;
dfs1(dfs1, t, t, 0);
int root = 0;
// cout << "maxdep: " << maxdep << '\n';
auto dfs2 = [&](auto&&dfs2, int u, int fa, int dep) -> void {
if (root) return;
if (dep == maxdep / 2) {
root = u;
return;
}
for (int v : G[u]) {
if (v == fa) continue;
dfs2(dfs2, v, u, dep + 1);
}
};
dfs2(dfs2, t, t, 0);
return root;
};
int root = getCenter();
// cout << root << '\n';
vector<vector<int>> son(n + 1);
auto dfs = [&](auto&&dfs, int u, int fa) -> void {
for (int v : G[u]) {
if (v == fa) continue;
son[u].push_back(v);
dfs(dfs, v, u);
}
};
dfs(dfs, root, root);
auto checkChain = [&](auto&&checkChain, vector<int>& arr, int l, int r) -> bool {
if (l == r) {
cout << "! " << arr[l] << endl;
return true;
}
// cout << l << " " << r << '\n';
cout << "? " << arr[l] << " " << arr[r] << endl;
int op; cin >> op;
if (op == 1) {
cout << "! " << arr[(l + r) >> 1] << endl;
return true;
}
else if (op == 0) {
int len = r - l + 1;
int mid = ((l + r) >> 1) + ((len & 1) ? - 1 : 0); // 3 4 5 6 7
return checkChain(checkChain, arr, l, mid);
}
else {
int len = r - l + 1;
int mid = ((l + r) >> 1) + 1;
return checkChain(checkChain, arr, mid, r);
}
return false;
};
auto check = [&](auto&&check, int st) -> void {
vector<int> chain;
int cur = st;
while (son[cur].size() == 1) {
chain.push_back(cur);
cur = son[cur][0];
}
chain.push_back(cur);
if (son[cur].size() == 0) {
checkChain(checkChain, chain, 0, int(chain.size()) - 1);
return;
}
else if (son[cur].size() == 2) {
int lson = son[cur][0], rson = son[cur][1];
cout << "? " << lson << " " << rson << endl;
int op; cin >> op;
if (op == 1) {
checkChain(checkChain, chain, 0, int(chain.size()) - 1);
return;
}
else if (op == 0) check(check, lson);
else check(check, rson);
}
};
if (G[root].size() == 2) {
int lson = G[root][0];
int rson = G[root][1];
cout << "? " << lson << " " << rson << endl;
int op; cin >> op;
if (op == 1) {
cout << root << endl;
return;
}
else if (op == 0) check(check, lson);
else if (op == 2) check(check, rson);
}
else if (G[root].size() == 3) {
int lson = G[root][0];
int rson = G[root][1];
int tmp = G[root][2];
cout << "? " << lson << " " << rson << endl;
son[root].clear();
son[root].push_back(tmp);
int op; cin >> op;
if (op == 1) check(check, root);
else if (op == 0) check(check, lson);
else check(check, rson);
}
}
int main() {
int t; cin >> t;
while (t--) solve();
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3504kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 0 1 2 0 2 0 0 2
output:
? 2 4 ? 1 5 ! 2 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Time Limit Exceeded
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 2 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 1 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0
output:
? 2 5 ? 1 6 ? 6 7 ! 6 ? 5 3 ? 1 4 ? 3 2 ! 2