QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#733078#9570. Binary Treelqh2024#WA 6ms3524kbC++202.9kb2024-11-10 17:05:402024-11-10 17:05:40

Judging History

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

  • [2024-11-10 17:05:40]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:3524kb
  • [2024-11-10 17:05:40]
  • 提交

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

        dfs(dfs, rt, 0);

        auto get = [&](auto && get, int u, int fa) -> void {
            int t = sz[rt], sum = 0;
            for (auto & v : adj[u]) if (v != fa && v) {
                get(get, v, u);
                t -= sz[v], sum = max(sum, sz[v]);
            }
            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) {
                auto t = qeury(v1, v2);
                adj[ac][0] = adj[ac][1] = 0;
                if (t == 0) {
                    rt = v1;
                } else if (t == 1) {
                
                } else {
                    rt = v2;
                }
            } 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: 100
Accepted
time: 1ms
memory: 3524kb

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
Wrong Answer
time: 6ms
memory: 3500kb

input:

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

output:

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

result:

wrong answer Too many queries (test case 17)