QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#731159 | #9570. Binary Tree | ucup-team5799 | RE | 0ms | 3656kb | C++20 | 4.6kb | 2024-11-10 00:14:29 | 2024-11-10 00:14:30 |
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 << "t: " << t << '\n';
// cout << "s: " << s << '\n';
// cout << "maxdep: " << maxdep << '\n';
vector<int> dis(n + 1, 0), dis1(n + 1, 0);
auto dfs2 = [&](auto&&dfs2, int u, int fa) -> void {
for (int v : G[u]) {
if (v == fa) continue;
dis[v] = dis[u] + 1;
dfs2(dfs2, v, u);
}
};
dfs2(dfs2, t, t);
dis1 = dis;
dis[s] = 0;
dfs2(dfs2, s, s);
for (int i = 1;i <= n;i++) {
if (dis[i] + dis1[i] == maxdep && dis[i] == maxdep / 2) {
root = i;
break;
}
}
return root;
};
int root = getCenter();
// cout << "root: " << 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: 0ms
memory: 3656kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0 2
output:
? 1 5 ? 2 4 ! 3 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Runtime Error
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 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 2 1 0 5 4 5 3 1 0 0 0 0 0 0 1 1 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 0 5 3 0 5 1 0 0 0 0 4 0 2 0 5 5 0 0 0 0 0 3 0 2 4 1 2 3 3 0 1 0 0 0 1 2 2 0 0 0 2 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 1 8 ? 5 4 ? 4 3 ! 4 ? 5 3 ? 1 4 ? 3 2 ! 2 ? 5 8 ? 4 2 ? 8 6 ! 8 ? 4 5 ? 1 3 ! 2 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 1 3 ! 1 ? 1 2 ? 5 3 ! 3 ? 3 2 ! 1 ? 1 2 ! 2 ? 2 3 ! 3 ? 1 9 ? 6 5 ? 4 3 ! 6 ? 1 2 ! 1 ? 5 9 ? 4 8 ? 5 3 ! 3 ? 3 10 ? 6 2 ? 10 4 ! 10 ? 8 2 ? 3 4 ? 6 5 ! 5 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 6 2 ? 9 5 ?...