QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#778623#9570. Binary TreeClarusRE 1ms3536kbC++203.5kb2024-11-24 15:31:562024-11-24 15:31:57

Judging History

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

  • [2024-11-24 15:31:57]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3536kb
  • [2024-11-24 15:31:56]
  • 提交

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

    std::vector<int> vis(g.size());

    auto mark = [&](int x) -> void
    {
        vis[x] = true;
        vis[x ^ 1] = true;
    };

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

    auto dfs1 = [&](auto &&self, int x, int fx) -> int
    {
        size[x] = 1;
        for (auto i : adj[x])
        {
            int y = g[i];
            if (vis[i] || 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 i : adj[x])
        {
            int y = g[i];
            if (vis[i] || 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;
        }
    };
    auto work = [&](int root) -> void
    {
        m = dfs1(dfs1, root, -1);
        dfs2(dfs2, root, -1);
    };

    while (true)
    {
        work(root);
        std::cerr << "m = " << m << std::endl;
        if (m == 1)
        {
            put(root);
            break;
        }

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

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: 3536kb

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: