QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#738068 | #9570. Binary Tree | guanghere | WA | 3ms | 3636kb | C++14 | 946b | 2024-11-12 17:38:10 | 2024-11-12 17:38:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std; const int N = 1e5+10;
int main(){
int t, n, x, o; cin >> t;
static int l[N], r[N]; static bool a[N];
while(t--){
cin >> n;
for(int i = 1; i <= n; i++)
cin >> l[i] >> r[i], a[l[i]] = a[r[i]] = 1;
for(int i = 1; i <= n; i++) if(!a[i]) {x = i; break;}
while(1){
if(l[x] && r[x]){
cout << "? " << l[x] << ' ' << r[x] << '\n';
cout.flush(); cin >> o;
if(o == 1) break; else x = (!o ? l[x] : r[x]);
}
else if(l[x] && !r[x]){
cout << "? " << l[x] << ' ' << x << '\n';
cout.flush(); cin >> o;
if(o == 2) break; else x = l[x];
}
else if(!l[x] && r[x]){
cout << "? " << r[x] << ' ' << x << '\n';
cout.flush(); cin >> o;
if(o == 2) break; else x = r[x];
}
else break;
}
cout << "! " << x << '\n'; cout.flush();
for(int i = 1; i <= n; i++) l[i] = r[i] = a[i] = 0;
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3636kb
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 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 3ms
memory: 3612kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 0 2
output:
? 2 1 ? 8 6 ? 5 4 ? 3 4
result:
wrong answer Too many queries (test case 1)