QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#760879#9570. Binary TreeHashbinRE 1ms3804kbC++232.2kb2024-11-18 19:56:282024-11-18 19:56:28

Judging History

你现在查看的是最新测评结果

  • [2024-11-18 19:56:28]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3804kb
  • [2024-11-18 19:56:28]
  • 提交

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

result: