QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#731154#9570. Binary Treeucup-team5799RE 0ms3660kbC++204.6kb2024-11-10 00:13:022024-11-10 00:13:03

Judging History

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

  • [2024-11-10 00:13:03]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3660kb
  • [2024-11-10 00:13:02]
  • 提交

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 << "t: " << t << '\n';
        // cout << "s: " << s << '\n';
        // cout << "maxdep: " << maxdep << '\n';
        vector<int> dis(n + 1, 0), dis1(n + 1, 0);
        auto dfs2 = [&](auto&&dfs2, int u, int fa) -> void {
            for (int v : G[u]) {
                if (v == fa) continue;
                dis[v] = dis[u] + 1;
                dfs2(dfs2, v, u);
            }
        };
        dfs2(dfs2, t, t);
        dis1 = dis;
        dis[s] = 0;
        dfs2(dfs2, s, s);
        for (int i = 1;i <= n;i++) {
            if (dis[i] + dis1[i] == maxdep && dis[i] == maxdep / 2) {
                root = i;
                break;
            }
        }
        return root;
    };
    
    int root = getCenter();
    // cout << "root: " << 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: 0ms
memory: 3660kb

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

output:

? 1 8
? 5 4
? 4 3
! 4
? 5 3
? 1 4
? 3 2
! 2
? 5 8
? 4 2
? 8 6
! 8
? 4 5
? 1 3
! 2
? 5 6
? 1 4
! 1
? 5 1
? 1 3
! 1
? 1 2
? 5 3
! 3
? 3 2
1

result: