QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#779166#9570. Binary TreeClarusWA 3ms3552kbC++203.5kb2024-11-24 17:46:192024-11-24 17:46:20

Judging History

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

  • [2024-11-24 17:46:20]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3552kb
  • [2024-11-24 17:46:19]
  • 提交

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);

        std::vector<int> e;
        for (auto y : adj[cent])
        {
            if (vis[y])
            {
                continue;
            }
            e.emplace_back(y);
        }

        if (e.size() == 0)
        {
            put(root);
            return;
        }
        else if (e.size() == 1)
        {
            int q = ask(cent, e[0]);
            assert(q != 1);
            if (q == 0)
            {
                root = cent;
                vis[e[0]] = true;
            }
            else
            {
                root = e[0];
                vis[cent] = true;
            }
        }
        else
        {
            if (e.size() == 3)
            {
                std::array<int, 3> r = {0, 1, 2};
                std::array<int, 3> s = {size[e[0]], size[e[1]], size[e[2]]};
                int i = std::max_element(s.begin(), s.end()) - s.begin();
                s[i] = m - s[i] + 1;
                std::sort(r.begin(), r.end(), 
                    [&](int i, int j)
                    {
                        return s[i] > s[j];
                    });
                auto _e = e;
                _e[0] = e[r[0]];
                _e[1] = e[r[1]];
                e = _e;
            }
            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;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3552kb

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 3500kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
0
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
0
1
2
5
4 5
3 1
0 0
0 0
0 0
0
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:

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

result:

wrong answer Too many queries (test case 936)