QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#725016 | #9570. Binary Tree | Time_stop | WA | 1ms | 3652kb | C++14 | 1.5kb | 2024-11-08 15:54:21 | 2024-11-08 15:54:23 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int M=2e5+5;
int n;
struct no{
int l,r;
}tree[M];
int fa[M];
void solve(){
cin>>n;
for(int i=1;i<=n;i++){
tree[i].l=tree[i].r=fa[i]=0;
}
for(int i=1;i<=n;i++){
cin>>tree[i].l>>tree[i].r;
fa[tree[i].l]=i; fa[tree[i].r]=i;
}
int root;
for(int i=1;i<=n;i++){
if(!fa[i]) {
root=i; break;
}
}
while(1){
if(tree[root].l==tree[root].r&&tree[root].l==0){
cout<<"! "<<root<<endl;
cout.flush();
break;
}
int u,v,tmp;
if(tree[root].l==0||tree[root].r==0){
u=root;
if(tree[root].l!=0) v=tree[root].l;
else v=tree[root].r;
cout<<"? "<<u<<" "<<v<<endl;
cin>>tmp;
if(tmp==0){
cout<<"! "<<u<<endl;
cout.flush();
break;
}
else if(tmp==2){
root=v; continue;
}
} else {
int u=tree[root].l,v=tree[root].r;
cout<<"? "<<u<<" "<<v<<endl;
cin>>tmp;
if(tmp==1){
cout<<"! "<<root<<endl;
cout.flush();
break;
} else if(tmp==0){
root=u; continue;
} else if(tmp==2){
root=v; continue;
}
}
}
}
int main(){
int t;cin>>t;
while(t--){
solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3620kb
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
Wrong Answer
time: 0ms
memory: 3652kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 0 2
output:
? 1 2 ? 8 6 ? 5 4 ? 4 3
result:
wrong answer Too many queries (test case 1)