QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#760879 | #9570. Binary Tree | Hashbin | RE | 1ms | 3804kb | C++23 | 2.2kb | 2024-11-18 19:56:28 | 2024-11-18 19:56:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
struct node
{
int l, r;
};
int n;
cin >> n;
vector<node> a(n + 10);
vector<int> vis(n + 10, 0);
for (int i = 1; i <= n; i++)
{
int u, v;
cin >> u >> v;
a[i].l = u, a[i].r = v;
if (u != 0)
vis[u] = 1;
if (v != 0)
vis[v] = 1;
}
int pos;
for (int i = 1; i <= n; i++)
{
if (!vis[i])
{
pos = i;
break;
}
}
auto dfs = [&](auto dfs, int p) -> void
{
if (a[p].l != 0 && a[p].r != 0)
{
cout << "?" << ' ' << a[p].l << ' ' << a[p].r <<'\n';
cout.flush();
int k;
cin >> k;
if (k == 1)
{
cout << "!" << ' ' << p << '\n';
cout.flush();
return;
}
else if(k==0)
{
dfs(dfs,a[p].l);
}
else dfs(dfs,a[p].r);
}
if(a[p].l==0&&a[p].r!=0)
{
cout << "?" << ' ' << p << ' ' << a[p].r <<'\n';
cout.flush();
int k;
cin >> k;
if(k==0)
{
cout << "!" <<' '<<p<<'\n';
cout.flush();
return ;
}
else dfs(dfs,a[p].r);
}
if(a[p].r==0&&a[p].l!=0)
{
cout << "?" << ' ' << p << ' ' << a[p].l <<'\n';
cout.flush();
int k;
cin >> k;
if(k==0)
{
cout << "!" <<' '<<p<<'\n';
cout.flush();
return ;
}
else dfs(dfs,a[p].l);
}
if(a[p].l==0&&a[p].r==0)
{
cout << "!"<<' '<<p<<'\n';
cout.flush();
return ;
}
};
dfs(dfs,pos);
cout << '\n';
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3804kb
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 2 0 2
output:
? 1 2 ? 8 6 ? 5 4 ? 4 3