QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#730141 | #9570. Binary Tree | ucup-team3160# | RE | 0ms | 14088kb | C++14 | 2.7kb | 2024-11-09 18:51:16 | 2024-11-09 18:51:24 |
Judging History
answer
#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "lib.h"
#endif
#define rep(i, min, max) for(int i = (min); i <= (max); ++i)
#define nrep(i, max, min) for(int i = (max); i >= (min); --i)
#define reads(str) (scanf("%s", str + 1), strlen(str + 1))
#define case() int Ts = read(); rep(T, 1, Ts)
#define putf(flag) puts((flag) ? "Yes" : "No")
#define put(x) printf("%d ", x)
#define putl(x) printf("%lld ", x)
#define endl() putchar('\n')
using namespace std;
typedef long long ll;
inline int read()
{
int now=0; bool nev=false; char c=getchar();
while(c<'0' || c>'9') { if(c=='-') nev=true; c=getchar(); }
while(c>='0' && c<='9') { now=(now<<1)+(now<<3)+(c&15); c=getchar(); }
return nev?-now:now;
}
const int N = 1e6 + 10;
int n;
int ls[N], rs[N], vis[N], fa[N];
int siz[N];
int deg[N];
int ask(int x, int y) {
printf("? %d %d\n", x, y); fflush(stdout);
return read();
}
int p = 0, mn = 1e9;
void dfs(int x) {
if(vis[x] || !x) { siz[x] = 0; return; }
int l = ls[x], r = rs[x];
dfs(l), dfs(r);
siz[x] = 1 + siz[l] + siz[r];
int cur = max({siz[l], siz[r], n - siz[x] + 1});
if(cur <= mn) p = x, mn = cur;
}
int main() {
case() {
n = read();
rep(i, 1, n) ls[i] = read(), rs[i] = read();
rep(i, 0, n) deg[i] = 0, vis[i] = 0;
rep(i, 1, n) deg[ls[i]] ++, deg[rs[i]] ++, fa[ls[i]] = i, fa[rs[i]] = i;
int rt = 0;
rep(i, 1, n) if(deg[i] == 0) rt = i;
int last = 1e9;
// ok();
while(1) {
p = 0, mn = 1e9;
dfs(rt);
// print(rt, siz[rt]);
fflush(stdout);
// assert(siz[rt] <= last / 2);
if(siz[rt] == 1) {
printf("! %d\n", rt); fflush(stdout);
break;
}
int l = ls[p], r = rs[p];
// print(rt, p, l, r, mn);
if(l != 0 && r != 0) {
int op = ask(l, r);
if(op == 0) rt = l;
if(op == 2) rt = r;
if(op == 1) vis[l] = vis[r] = 1;
} else {
// assert(l || r);
int son = l | r, fa = ::fa[p];
if(mn <= siz[rt] / 2) {
int op = ask(p, son);
if(op == 0) vis[son] = 1;
if(op == 2) rt = son;
} else {
assert(siz[p] == siz[rt] / 2 + 1);
int op = ask(fa, son);
if(op == 0) vis[p] = 1;
if(op == 2) rt = son;
if(op == 1) vis[son] = 1, rt = p;
}
}
last = siz[rt];
}
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 14088kb
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
Runtime Error
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 2 1 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2
output:
? 8 6 ? 5 4 ? 8 3 ! 4 ? 7 3