QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#732442 | #9570. Binary Tree | SaltyJellY# | WA | 4ms | 5816kb | C++14 | 3.2kb | 2024-11-10 14:32:58 | 2024-11-10 14:32:58 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, cnt, T;
int head[N], dep[N], pre[N];
bool del[N];
struct Edge{
int to, nxt;
}e[N << 1];
void add(int u, int v) {
e[++cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
void get_root(int now, int fa) {
if(now == -1) return;
for(int i = head[now]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fa || del[y]) continue;
dep[y] = dep[now] + 1;
get_root(y, now);
}
}
void get_d(int now, int fa) {
if(now == -1) return;
for(int i = head[now]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fa || del[y]) continue;
dep[y] = dep[now] + 1;
pre[y] = now;
get_d(y, now);
}
}
void solve() {
scanf("%d", &n);
cnt = 0;
for(int i = 1; i <= n; ++i) head[i] = del[i] = 0;
for(int i = 1; i <= n; ++i) {
int lson, rson;
scanf("%d%d", &lson, &rson);
if(lson) {
add(i, lson); add(lson, i);
}
if(rson) {
add(i, rson); add(rson, i);
}
}
int lgp = 0, rt = 1;
while(1) {
//printf("%d:\n", lgp);
lgp++;
for(int i = 1; i <= n; ++i) {
dep[i] = pre[i] = 0;
}
get_root(rt, 0);
int S = -1, maxn = 0, T = -1;
for(int i = 1; i <= n; ++i) {
if(dep[i] > maxn) {
maxn = dep[i];
S = i;
}
}
if(S == -1) {
printf("! %d\n",rt);
fflush(stdout);
//break;
return;
}
for(int i = 1; i <= n; ++i) dep[i] = 0;
get_d(S, 0);
maxn = 0;
for(int i = 1; i <= n; ++i) {
if(dep[i] > maxn) {
maxn = dep[i];
T = i;
}
}
if(T == -1) {
printf("! %d\n", S);
fflush(stdout);
return;
}
printf("? %d %d\n", T, S);
fflush(stdout);
vector<int>v;
while(T != S) {
v.push_back(T);
T = pre[T];
}
v.push_back(S);
// for(int i = 0; i < v.size(); ++i) {
// printf("%d ", v[i]);
// }puts("");
int res;
scanf("%d", &res);
if(res == 0) {
int mid = v.size() / 2;
int i = v.size() - 1, j = 0;
for(i = v.size() - 1, j = 1; j <= mid; --i, ++j) {
del[v[i]] = 1;
// printf("del:%d\n", v[i]);
}
rt = v[i];
}
else if(res == 2) {
int mid = v.size() / 2;
int i = 0, j = 0;
for(i = 0, j = 1; j <= mid; ++i, ++j) {
del[v[i]] = 1;
}
rt = v[i];
}
else {
rt = v[v.size() / 2];
for(int i = 0; i < v.size(); ++i) {
if(v[i] != rt) del[v[i]] = 1;
}
}
//printf("rt: %d\n", rt);
}
}
signed main() {
scanf("%d", &T);
while(T--) solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 5784kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 0 1 2 0 2 0 0 2
output:
? 1 4 ? 5 1 ! 2 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 4ms
memory: 5816kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 2 2
output:
? 7 3 ? 5 3 ? 8 5 ! 5 ? 1 6 ? 7 1 ? 4 1 ? 2 1
result:
wrong answer Too many queries (test case 2)