QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#759096#9570. Binary Treelonelywolf#RE 0ms0kbC++203.2kb2024-11-17 21:35:502024-11-17 21:35:50

Judging History

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

  • [2024-11-17 21:35:50]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-17 21:35:50]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define int long long

void solve()
{
    int n;
    cin >> n;

    vector adj(n + 1, vector<int>());
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        cin >> x >> y;
        if (x)
        {
            adj[i].push_back(x);
            adj[x].push_back(i);
        }
        if (y)
        {
            adj[i].push_back(y);
            adj[y].push_back(i);
        }
    }

    int rt = 0;
    int mn = 1e18;
    vector<int> sz(n + 1);
    vector<bool> b(n + 1, true);
    function<void(int, int)> dfs = [&](int x, int f)
    {
        sz[x] = 1;
        int mx = 0;
        for (auto y : adj[x])
        {
            if (y != f && b[y])
            {
                dfs(y, x);
                mx = max(mx, sz[y]);
                sz[x] += sz[y];
            }
        }
        mx = max(mx, n - sz[x]);
        if (mx < mn)
        {
            mn = mx;
            rt = x;
        }
    };

    function<void(int, int)> mark = [&](int x, int f)
    {
        b[x] = false;
        for (auto y : adj[x])
        {
            if (y != f && b[y])
            {
                mark(y, x);
            }
        }
    };

    int ans = -1;
    function<void(int)> work = [&](int x)
    {
        mn = 1e18;
        dfs(x, 0);
        vector<int> a;
        for (auto y : adj[rt])
        {
            if (b[y])
            {
                a.push_back(y);
            }
        }
        if (a.size() == 0)
        {
            ans = rt;
            return;
        }
        if (a.size() == 1)
        {
            cout << "? " << rt << " " << a[0] << endl;
            int ret;
            cin >> ret;
            if (ret == 0)
            {
                ans = rt;
            }
            else if (ret == 2)
            {
                b[rt] = false;
                work(a[0]);
            }
            else
            {
                assert(false);
            }
        }
        if (a.size() >= 2)
        {
            cout << "? " << a[0] << " " << a[1] << endl;
            int ret;
            cin >> ret;
            if (ret == 0)
            {
                b[rt] = false;
                for (int i = 0; i < a.size(); i++)
                {
                    if (i != 0)
                    {
                        mark(a[i], rt);
                    }
                }
                work(a[0]);
            }
            else if (ret == 1)
            {
                for (int i = 0; i < 2; i++)
                {
                    mark(a[i], rt);
                }
                work(rt);
            }
            else if (ret == 2)
            {
                b[rt] = false;
                for (int i = 0; i < a.size(); i++)
                {
                    if (i != 1)
                    {
                        mark(a[i], rt);
                    }
                }
                work(a[1]);
            }
        }
    };

    work(1);
    cout << "! " << ans << endl;
}

signed main()
{
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

2
5
0 0
1 5
2 4
0 0
0 0
1
2

output:

? 1 5
? 2 3
? 3 4

result: