QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#788745 | #9570. Binary Tree | johnsmith0x3f | WA | 1ms | 3732kb | C++17 | 1.5kb | 2024-11-27 18:07:44 | 2024-11-27 18:07:50 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int rt, fa[N], ls[N], rs[N], siz[N];
inline int big(const int &v) {
return max(siz[rt] - siz[v], max(siz[ls[v]], siz[rs[v]]));
}
void getSize(int v) {
if(v == 0) return;
getSize(ls[v]), getSize(rs[v]), siz[v] = siz[ls[v]] + siz[rs[v]] + 1;
}
inline void solve() {
int n;
cin >> n;
for(int i = 1; i <= n; ++i) fa[i] = ls[i] = rs[i] = 0;
for(int i = 1; i <= n; ++i) {
cin >> ls[i] >> rs[i];
if(ls[i]) fa[ls[i]] = i;
if(rs[i]) fa[rs[i]] = i;
}
for(int i = 1; i <= n; ++i)
if(fa[i] == 0) rt = i;
while(1) {
for(int i = 1; i <= n; ++i) siz[i] = 0;
getSize(rt);
if(siz[rt] == 1) break;
int c = 1;
for(int i = 1; i <= n; ++i)
if(big(i) < big(c)) c = i;
if(ls[c] == 0 || rs[c] == 0 || siz[c] * 2 == siz[rt]) {
int u = fa[c], v = max(ls[c], rs[c]);
if(u == 0) u = c;
if(v == 0) v = c;
cout << "? " << u << ' ' << v << '\n', cout.flush();
int t;
cin >> t;
fa[c] = 0;
if(ls[u] == c) ls[u] = 0;
if(rs[u] == c) rs[u] = 0;
fa[v] = 0;
if(ls[c] == v) ls[c] = 0;
if(rs[c] == v) rs[c] = 0;
if(t == 1) rt = c;
if(t == 2) rt = v;
}
else {
int u = ls[c], v = rs[c];
cout << "? " << u << ' ' << v << '\n', cout.flush();
int t;
cin >> t;
fa[u] = fa[v] = 0;
ls[c] = rs[c] = 0;
if(t == 0) rt = u;
if(t == 2) rt = v;
}
}
cout << "! " << rt << '\n', cout.flush();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3692kb
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: 1ms
memory: 3732kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 2 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:
? 8 6 ? 8 3 ? 8 5 ! 5 ? 7 2 ? 7 8 ? 8 6 ! 6 ? 5 8 ? 4 2 ? 8 6 ! 8 ? 4 5 ? 3 1 ! 2 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 1 3 ! 1 ? 2 4 ? 1 5 ! 5 ? 2 3 ! 1 ? 1 2 ! 2 ? 2 3 ! 3 ? 9 7 ? 4 3 ? 6 5 ! 7 ? 1 2 ! 1 ? 5 9 ? 4 8 ? 5 3 ! 3 ? 3 10 ? 6 2 ? 10 4 ! 10 ? 3 4 ? 1 7 ? 8 2 ! 2 ? 2 1 ! 1 ? 3 6 ? 5 1 ? 7 4
result:
wrong answer Too many queries (test case 17)