QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#731119#9570. Binary Treeucup-team5799RE 1ms3528kbC++204.3kb2024-11-09 23:59:192024-11-09 23:59:21

Judging History

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

  • [2024-11-09 23:59:21]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3528kb
  • [2024-11-09 23:59:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve() {
    int n; cin >> n;
    vector<vector<int>> G(n + 1);
    for (int i = 1;i <= n;i++) {
        int a, b; cin >> a >> b;
        if (a) {
            G[a].push_back(i);
            G[i].push_back(a);
        }
        if (b) {
            G[b].push_back(i);
            G[i].push_back(b);
        }
    }

    if (n == 2) {
        cout << "? 1 2" << endl;
        int op; cin >> op;
        if (op == 0) cout << "! 1" << endl;
        else cout << "! 2" << endl;
        return;
    }

    auto getCenter = [&]() {
        int s = 0, maxdep = 0;
        auto dfs1 = [&](auto&&dfs1, int u, int fa, int dep) -> void {
            if (dep > maxdep) {
                maxdep = dep;
                s = u;
            }
            for (int v : G[u]) {
                if (v == fa) continue;
                dfs1(dfs1, v, u, dep + 1);
            }
        };
        dfs1(dfs1, 1, 1, 0);
        int t = s; maxdep = 0;
        dfs1(dfs1, t, t, 0);
        int root = 0;
        // cout << "maxdep: " << maxdep << '\n';
        auto dfs2 = [&](auto&&dfs2, int u, int fa, int dep) -> void {
            if (root) return;
            if (dep == maxdep / 2) {
                root = u;
                return;
            }
            for (int v : G[u]) {
                if (v == fa) continue;
                dfs2(dfs2, v, u, dep + 1);
            }
        };
        dfs2(dfs2, t, t, 0);
        return root;
    };
    
    int root = getCenter();
    // cout << root << '\n';

    vector<vector<int>> son(n + 1);
    auto dfs = [&](auto&&dfs, int u, int fa) -> void {
        for (int v : G[u]) {
            if (v == fa) continue;
            son[u].push_back(v);
            dfs(dfs, v, u);
        }
    };
    dfs(dfs, root, root);

    auto checkChain = [&](auto&&checkChain, vector<int>& arr, int l, int r) -> bool {
        if (l == r) {
            cout << "! " << arr[l] << endl;
            return true;
        }
        cout << l << " " << r << '\n';
        cout << "? " << arr[l] << " " << arr[r] << endl;
        int op; cin >> op;
        if (op == 1) {
            cout << "! " << arr[(l + r) >> 1] << endl;
            return true;
        }
        else if (op == 0) {
            int len = r - l + 1;
            int mid = ((l + r) >> 1) + ((len & 1) ?  - 1 : 0);     // 3 4 5 6 7
            return checkChain(checkChain, arr, l, mid);
        }
        else {
            int len = r - l + 1;
            int mid = ((l + r) >> 1) + 1;
            return checkChain(checkChain, arr, mid, r);
        }
        return false;
    };

    auto check = [&](auto&&check, int st) -> void {
        vector<int> chain;
        int cur = st;
        while (son[cur].size() == 1) {
            chain.push_back(cur);
            cur = son[cur][0];
        }
        chain.push_back(cur);
        if (son[cur].size() == 0) {
            checkChain(checkChain, chain, 0, int(chain.size()) - 1);
            return;
        }
        else if (son[cur].size() == 2) {
            int lson = son[cur][0], rson = son[cur][1];
            cout << "? " << lson << " " << rson << endl;
            int op; cin >> op;
            if (op == 1) {
                checkChain(checkChain, chain, 0, int(chain.size()) - 1);
                return;
            }
            else if (op == 0) check(check, lson);
            else check(check, rson);
        }
    };

    if (G[root].size() == 2) {
        int lson = G[root][0];
        int rson = G[root][1];
        cout << "? " << lson << " " << rson << endl;
        int op; cin >> op;
        if (op == 1) {
            cout << root << endl;
            return;
        }
        else if (op == 0) check(check, lson);
        else if (op == 2) check(check, rson);
    }
    else if (G[root].size() == 3) {
        int lson = G[root][0];
        int rson = G[root][1];
        int tmp = G[root][2];
        cout << "? " << lson << " " << rson << endl;
        son[root].clear();
        son[root].push_back(tmp);
        int op; cin >> op;
        if (op == 1) check(check, root);
        else if (op == 0) check(check, lson);
        else check(check, rson);
    }
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

詳細信息

Test #1:

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

input:

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

output:

? 2 4
? 1 5
! 2
? 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
0
2

output:

? 2 5
? 1 6
0 1
? 6 7

result: