QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#733162#9570. Binary Treelqh2024#WA 1ms3592kbC++204.3kb2024-11-10 17:29:292024-11-10 17:29:32

Judging History

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

  • [2024-11-10 17:29:32]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3592kb
  • [2024-11-10 17:29:29]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long

void QAQ() {
    int n;
    cin >> n ;

    vector adj(n + 1, vector<int>(2));
    vector<int> sz(n + 1, 1), pa(n + 1);
    vector<array<pair<int, int>, 3>> t_sz(n + 1);
    set<int> t;

    for (int i = 1; i <= n; i++) {
        cin >> adj[i][0] >> adj[i][1];
        t.emplace(adj[i][0]), t.emplace(adj[i][1]);
    }
    int rt = 0, ac = -1;
    for (int i = 1; i <= n; i++) {
        if (!t.count(i)) {
            rt = i;
            break;
        }
    }

    auto getac = [&](int ac = -1, int res = numeric_limits<int>::max()) {
        auto dfs = [&](auto && dfs, int u, int fa) -> void {
            pa[u] = fa;
            for (auto & v : adj[u]) if (v != fa && v) {
                dfs(dfs, v, u);
                sz[u] += sz[v];
            }
        };

        sz = vector<int>(n + 1, 1), pa = vector<int>(n + 1);
        t_sz.assign(n + 1, array<pair<int, int>, 3>());

        dfs(dfs, rt, 0);

        auto get = [&](auto && get, int u, int fa) -> void {
            int t = sz[rt], sum = 0, cnt = 1;
            for (auto & v : adj[u]) if (v != fa && v) {
                get(get, v, u);
                t -= sz[v], sum = max(sum, sz[v]);
                t_sz[u][cnt++] = {v, sz[v]};
            }
            t_sz[u][0] = {fa, max(0ll, sum - 1)};
            if (abs(t - sum) < res) {
                ac = u;
                res = abs(t - sum);
            }
        };

        get(get, rt, 0);

        return pair{ac, sz[rt]};
    };

    auto qeury = [&](int u, int v, int t = 0) {
        cout << "? " << u << " " << v << endl;
        cin >> t;
        return t;
    };

    auto ask = [&](int x) {
        cout << "! " << x << endl;
    };

    for (int sz; ; ) {
        tie(ac, sz) = getac();
        // cerr << "ac " << ac << " " << sz << "\n";
        if (sz <= 1) {
            ask(ac);
            return;
        } else if (sz <= 2) {
            auto t = qeury(rt, adj[rt][0] ^ adj[rt][1]);
            if (t == 0) return ask(rt);
            else if (t == 2) return ask(adj[rt][0] ^ adj[rt][1]);
            else assert(0);
        } else {
            auto v1 = adj[ac][0], v2 = adj[ac][1];
            if (v1 && v2) {
                sort(t_sz[ac].begin(), t_sz[ac].end(), [&](auto & x, auto & y) {
                    return x.second > y.second;
                });
                v1 = t_sz[ac][0].first, v2 = t_sz[ac][1].first;
                auto t = qeury(v1, v2);

                if (v1 == pa[ac]) {
                    if (adj[v1][0] == ac) adj[v1][0] = 0;
                    if (adj[v1][1] == ac) adj[v1][1] = 0; 
                } else {
                    if (adj[ac][0] == v1) adj[ac][0] = 0;
                    if (adj[ac][1] == v1) adj[ac][0] = 0;
                }

                if (v2 == pa[ac]) {
                    if (adj[v2][0] == ac) adj[v2][0] = 0;
                    if (adj[v2][1] == ac) adj[v2][1] = 0; 
                } else {
                    if (adj[ac][0] == v2) adj[ac][0] = 0;
                    if (adj[ac][1] == v2) adj[ac][0] = 0;
                }

                if (t == 0) {
                    if (v1 == pa[ac]) {

                    } else {
                        rt = v1;
                    }
                } else if (t == 2) {
                    if (v2 == pa[ac]) {

                    } else {
                        rt = v2;
                    }
                } else {
                    if (t_sz[ac].back().first == pa[ac]) {

                    } else {
                        rt = ac;
                    }
                }
            } else {
                int v = v1 ^ v2, tt = pa[ac];

                auto t = qeury(tt, v);
                if (adj[tt][0] == ac) adj[tt][0] = 0;
                if (adj[tt][1] == ac) adj[tt][1] = 0;
                adj[ac][0] = adj[ac][1] = 0;

                if (t == 0) {

                } else if (t == 2) {
                    rt = v;
                } else {
                    return ask(ac);
                }
            }
        }
    }
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    int t = 1;
    cin >> t;
    for (cout << fixed << setprecision(12); t--; QAQ());
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3592kb

input:

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

output:

? 1 5
? 2 0

result:

wrong answer Integer parameter [name=v] equals to 0, violates the range [1, 5] (test case 1)