QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#733665#9570. Binary Treeucup-team5799RE 1ms3448kbC++203.6kb2024-11-10 20:23:362024-11-10 20:23:37

Judging History

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

  • [2024-11-10 20:23:37]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3448kb
  • [2024-11-10 20:23:36]
  • 提交

answer

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

void RES(int x) {
    cout << "! " << x << endl;
}

void ASK(int u, int v) {
    cout << "? " << u << " " << v << endl;
}

int GET() {
    int op; cin >> op;
    return op;
}

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

    auto removeit = [&](vector<int>& arr, int x) {
        int pos = -1;
        for (int i = 0;i < arr.size();i++) {
            if (arr[i] == x) {
                pos = i;
                break;
            }
        }
        if (pos != -1) {
            arr.erase(arr.begin() + pos);
        }
    };

    auto dfs = [&](auto&&dfs, int st, int ps) -> void {
        int m = 0;
        auto cnt = [&](auto&&cnt, int u, int fa) -> void {
            m++;
            for (int v : G[u]) {
                if (v == fa) continue;
                cnt(cnt, v, u);
            }
        };
        cnt(cnt, st, ps);

        // cout << "m: " << m << endl;

        vector<int> sz(n + 1, 0), w(n + 1, 0), centroid(2, 0);
        auto getCentroid = [&](auto&&getCentroid, int u, int fa) -> void {
            sz[u] = 1;
            w[u] = 0;
            for (int v : G[u]) {
                if (v == fa) continue;
                getCentroid(getCentroid, v, u);
                sz[u] += sz[v];
                w[u] = max(w[u], sz[v]);
            }
            w[u] = max(w[u], m - sz[u]);
            // cout << u << ": " << w[u] << endl;
            if (w[u] <= m / 2) centroid[centroid[0] != 0] = u;
        };
        getCentroid(getCentroid, st, ps);
        int root = centroid[0];
        if (G[root].size() == 1 && centroid[1] != 0) {
            root = centroid[1];
        }

        // cout << "root: " << root << " " << G[root].size() << endl;
        auto& son = G[root];
        if (son.size() == 0) {
            RES(root);
            return;
        }
        else if (son.size() == 1) {
            ASK(root, son[0]);
            int op = GET();
            if (op == 0) RES(root);
            else RES(son[0]);
            return;
        }
        else if (son.size() == 2) {
            int lson = son[0], rson = son[1];
            removeit(G[lson], root);
            removeit(G[rson], root);
            // remove(G[lson].begin(), G[lson].end(), root);
            // remove(G[rson].begin(), G[rson].end(), root);
            G[root].clear();
            ASK(lson, rson);
            int op = GET();
            if (op == 1) {
                RES(root);
                return;
            }
            else if (op == 0) dfs(dfs, lson, root);
            else dfs(dfs, rson, root);
        }
        else if (son.size() == 3) {
            int lson = son[0], rson = son[1], tson = son[2];
            removeit(G[lson], root);
            removeit(G[rson], root);
            // remove(G[lson].begin(), G[lson].end(), root);
            // remove(G[rson].begin(), G[rson].end(), root);
            G[root].clear();
            G[root].push_back(tson);
            ASK(lson, rson);
            int op = GET();
            if (op == 1) {
                dfs(dfs, root, root);
            }
            else if (op == 0) dfs(dfs, lson, root);
            else dfs(dfs, rson, root);
        }
    };
    dfs(dfs, 1, 1);
}

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

詳細信息

Test #1:

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

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

output:

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

result: