QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#772734#9570. Binary TreeRUOHUIRE 0ms0kbC++203.4kb2024-11-22 21:27:312024-11-22 21:27:31

Judging History

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

  • [2024-11-22 21:27:31]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-22 21:27:31]
  • 提交

answer

#include "bits/stdc++.h"
#define int long long
#define ull unsigned long long
#define PII pair<int, int>
#define TIII tuple<int, int, int>
#define LL __int128
#define eps 1e-6
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
constexpr int N = 2e6 + 10, M = 2e6 + 10, mod = 1e9 + 7, inf = 1e18;
int n, m;

void solve()
{
    cin >> n;
    vector<vector<int>> adj(n + 1, vector<int>());
    vector<int> size(n + 1), p(n + 1);
    vector<int> deg(n + 1);
    for (int i = 1; i <= n; i++)
    {
        int L, R;
        cin >> L >> R;
        if (L)
        {
            adj[i].emplace_back(L), adj[L].emplace_back(i);
            deg[i]++, deg[L]++, p[L] = i;
        }
        if (R)
        {
            adj[i].emplace_back(R), adj[R].emplace_back(i);
            deg[i]++, deg[R]++, p[R] = i;
        }
    }
    vector<int> vis(n + 1);
    auto get = [&](int u, int n, int fa)
    {
        int root = 0;
        function<void(int, int)> dfs1 = [&](int u, int fa)
        {
            if(vis[u]) return;
            size[u] = 1;
            bool pass = true;
            for (auto &v : adj[u])
            {
                if (v == fa)
                    continue;
                dfs1(v, u);
                size[u] += size[v];
                if (size[v] > n / 2)
                    pass = false;
            }
            if (n - size[u] > n / 2)
                pass = false;
            if (pass)
                root = u;
        };
        dfs1(u, fa);
        return root;
    };
    auto ask = [&](int a, int b) -> int
    {
        int ans;
        cout << "? " << a << " " << b << endl;
        cin >> ans;
        return ans;
    };
    int pre = 1;
    for (int i = 1; i <= n; i++)
    {
        if (!p[i])
            pre = i;
    }
    size[pre] = n;
    int fa = 0;
    while (1)
    {
        int root = get(pre, size[pre], fa);
        if (deg[root] == 1)
        {
            if (ask(root, adj[root][0]) == 0)
            {
                cout << "! " << root << endl;
                return;
            }
            else
            {
                cout << "! " << adj[root][0] << endl;
                return;
            }
        }
        if (deg[root] == 2)
        {
            int a = adj[root][0], b = adj[root][1];
            int ans = ask(a, b);
            if (ans == 0)
            {
                cout << "! " << root << endl;
                return;
            }
            pre = (ans == 0 ? a : b);
        }
        if (deg[root] == 3)
        {
            vector<int> vec{adj[root][0], adj[root][1], adj[root][3]};
            sort(vec.begin(), vec.end(), [&](int i, int j)
                 { return size[i] > size[j]; });
            int a = vec[0], b = vec[1];
            int ans = ask(a, b);
            if (ans == 0)
            {
                cout << "! " << root << endl;
                return;
            }
            pre = (ans == 0 ? a : b);
        }
    }
}

signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cout << fixed << setprecision(2);

#ifndef ONLINE_JUDGE
    freopen("in.txt", "rt", stdin);
    freopen("out.txt", "wt", stdout);
#endif

    int Cases = 1;
    cin >> Cases;
    while (Cases--)
    {
        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
? 4 3
! 3

result: