QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#759218#9570. Binary Treelonelywolf#RE 0ms3500kbC++203.3kb2024-11-17 23:14:242024-11-17 23:14:24

Judging History

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

  • [2024-11-17 23:14:24]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3500kb
  • [2024-11-17 23:14:24]
  • 提交

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 tot = n;
    int rt = 0;
    int mn = 1e18;

    vector<int> sz(n + 1);
    vector<int> 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, tot - 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)
    {
        if (ans != -1)
        {
            return;
        }
        tot = count(b.begin() + 1, b.end(), true);
        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)
            {
                ans = a[0];
            }
            return;
        }
        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: 100
Accepted
time: 0ms
memory: 3500kb

input:

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

output:

? 1 5
? 2 4
! 3
? 2 1
! 1

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
0
0
2
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
2
1
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
0
0
2
5
4 5
3 1
0 0
0 0
0 0
1
2
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
0
0
5
3 0
5 1
0 0
0 0
4 0
0
2
5
5 0
0 0
0 0
3 0
2 4
1
0
3
3 0
1 0
0 0
2
2
2 0
0 0
0
3
2 3
0 0
0 0
2
10
2 8
9 7
0 0
...

output:

? 2 5
? 2 7
? 1 2
! 2
? 5 3
? 1 4
? 3 2
! 3
? 1 6
? 1 7
? 5 1
! 1
? 4 5
? 3 1
! 1
? 5 6
? 1 4
! 1
? 5 1
? 4 5
! 5
? 1 2
? 3 5
! 3
? 3 2
! 2
? 2 1
! 2
? 2 3
! 3
? 2 6
? 1 9
? 10 9
! 9
? 2 1
! 2
? 5 9
? 4 8
? 5 3
! 5
? 5 8
? 7 1
? 5 3
! 5
? 3 4
? 1 7
? 8 2
! 2
? 2 1
! 1
? 4 3
? 1 7
! 7
? 4 9
? 2 3
? 4...

result: