QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#732981 | #9570. Binary Tree | lqh2024# | WA | 4ms | 3660kb | C++20 | 2.8kb | 2024-11-10 16:44:01 | 2024-11-10 16:44:02 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
void QAQ() {
int n;
cin >> n ;
vector adj(n + 1, vector<int>(2));
vector<int> sz(n + 1, 1);
set<int> t;
for (int i = 1; i <= n; i++) {
cin >> adj[i][0] >> adj[i][1];
t.emplace(adj[i][0]), t.emplace(adj[i][1]);
}
int rt = 0, ac = -1;
for (int i = 1; i <= n; i++) {
if (!t.count(i)) {
rt = i;
break;
}
}
auto getac = [&](int ac = -1, int res = numeric_limits<int>::max()) {
auto dfs = [&](auto && dfs, int u, int fa) -> void {
for (auto & v : adj[u]) if (v != fa && v) {
dfs(dfs, v, u);
sz[u] += sz[v];
}
};
sz = vector<int>(n + 1, 1);
dfs(dfs, rt, 0);
auto get = [&](auto && get, int u, int fa) -> void {
int t = sz[rt], sum = 0;
for (auto & v : adj[u]) if (v != fa && v) {
get(get, v, u);
t -= sz[v], sum = max(sum, sz[v]);
}
if (abs(t - sum) < res) {
ac = u;
res = abs(t - sum);
}
};
get(get, rt, 0);
return pair{ac, sz[rt]};
};
auto qeury = [&](int u, int v, int t = 0) {
cout << "? " << u << " " << v << endl;
cin >> t;
return t;
};
auto ask = [&](int x) {
cout << "! " << x << endl;
};
for (int sz; ; ) {
tie(ac, sz) = getac();
// cerr << "ac " << ac << " " << sz << "\n";
if (sz <= 1) {
ask(ac);
return;
} else if (sz <= 2) {
auto t = qeury(rt, adj[rt][0] ^ adj[rt][1]);
if (t == 0) return ask(rt);
else if (t == 2) return ask(adj[rt][0] ^ adj[rt][1]);
else assert(0);
} else {
auto v1 = adj[ac][0], v2 = adj[ac][1];
if (v1 && v2) {
auto t = qeury(v1, v2);
adj[ac][0] = adj[ac][1] = 0;
if (t == 0) {
rt = v1;
} else if (t == 1) {
} else {
rt = v2;
}
} else {
int v = v1 ^ v2;
auto t = qeury(ac, v);
adj[ac][0] = adj[ac][1] = 0;
if (t == 0) {
} else if (t == 2) {
rt = v;
} else {
assert(0);
}
}
}
}
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int t = 1;
cin >> t;
for (cout << fixed << setprecision(12); t--; QAQ());
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3660kb
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
Wrong Answer
time: 4ms
memory: 3624kb
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 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 1 2 5 4 5 3 1 0 0 0 0 0 0 1 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 2 5 3 0 5 1 0 0 0 0 4 0 0 0 5 5 0 0 0 0 0 3 0 2 4 2 2 3 3 0 1 0 0 0 0
output:
? 8 6 ? 5 4 ? 4 3 ! 4 ? 7 3 ? 1 4 ? 3 2 ! 3 ? 5 8 ? 4 2 ? 8 6 ! 6 ? 4 5 ? 3 1 ! 3 ? 5 6 ? 1 4 ! 4 ? 5 1 ? 5 4 ! 5 ? 2 4 ? 4 3 ! 3 ? 1 3 ? 2 1
result:
wrong answer Too many queries (test case 8)