QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#778891#9570. Binary TreeClarusRE 1ms3600kbC++203.1kb2024-11-24 16:42:012024-11-24 16:42:01

Judging History

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

  • [2024-11-24 16:42:01]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3600kb
  • [2024-11-24 16:42:01]
  • 提交

answer

#include <bits/stdc++.h>

using LL = long long;

constexpr int inf = 1E9;

int ask(int a, int b)
{
    std::cout << "? " << a + 1 << ' ' << b + 1 << std::endl;
    int x;
    std::cin >> x;
    return x;    
}
void put(int x)
{
    std::cout << "! " << x + 1 << std::endl;
}

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

    std::vector<std::vector<int>> adj(n);
    for (int i = 0; i < n; i++)
    {
        int v1, v2;
        std::cin >> v1 >> v2;
        v1--, v2--;
        if (v1 >= 0)
        {
            adj[i].emplace_back(v1);
            adj[v1].emplace_back(i);
        }
        if (v2 >= 0)
        {
            adj[i].emplace_back(v2);
            adj[v2].emplace_back(i);
        }
    }

    std::vector<int> vis(n), size(n);
    int cent = -1, root = 0;
    int m = -1;

    auto dfs1 = [&](auto &&self, int x, int fx) -> int
    {
        size[x] = 1;
        for (auto y : adj[x])
        {
            if (vis[y] || y == fx)
            {
                continue;
            }
            size[x] += self(self, y, x);
        }
        return size[x];
    };
    auto dfs2 = [&](auto &&self, int x, int fx) -> void
    {
        int val = 0;
        for (auto y : adj[x])
        {
            if (vis[y] || y == fx)
            {
                continue;
            }
            self(self, y, x);
            val = std::max(val, size[y]);
        }
        val = std::max(val, m - size[x]);
        if (val <= m / 2)
        {
            cent = x;
        }
    };

    while (true)
    {
        m = dfs1(dfs1, root, -1);
        dfs2(dfs2, root, -1);
        if (m == 1)
        {
            assert(root == cent);
            put(root);
            break;
        }

        std::vector<int> e;
        for (auto y : adj[cent])
        {
            if (vis[y])
            {
                continue;
            }
            e.push_back(y);
        }
        assert(e.size() > 0);
        if (e.size() == 1)
        {
            int q = ask(cent, e[0]);
            assert(q != 1);
            if (q == 0)
            {
                // root = cent;
                // vis[e[0]] = true;
                put(cent);
                return;
            }
            else
            {
                // root = e[0];
                // vis[cent] = true;
                put(e[0]);
                return;
            }
        }
        else
        {
            int q = ask(e[0], e[1]);
            if (q == 0)
            {
                root = e[0];
                vis[cent] = true;
            }
            else if (q == 2)
            {
                root = e[1];
                vis[cent] = true;
            }
            else
            {
                root = cent;
                vis[e[0]] = true;
                vis[e[1]] = true;
            }
        }
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int T;
    std::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: 3600kb

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

output:

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

result: