QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#758967#9570. Binary TreeJ1angZ1RE 0ms3576kbC++203.3kb2024-11-17 20:43:292024-11-17 20:43:29

Judging History

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

  • [2024-11-17 20:43:29]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3576kb
  • [2024-11-17 20:43:29]
  • 提交

answer

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

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 0
#endif

using i64 = long long;
#define int long long

void solve() {
    int n;
    cin >> n;
    vector<multiset<int>> adj(n);
    for (int i = 0; i < n; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        if (~u) {
            adj[i].insert(u);
            adj[u].insert(i);
        }
        if (~v) {
            adj[i].insert(v);
            adj[v].insert(i);
        }
    }
    vector<int> siz(n), maxs(n);
    auto getCog = [&](int st) {
        int tot = 0;
        auto getSize = [&](auto self, int x, int p) -> void {
            tot++;
            for (auto y : adj[x]) {
                if (y != p) {
                    self(self, y, x);
                }
            }
        };
        getSize(getSize, st, -1);

        int maxv = 1e9;
        array<int, 2> ans{-1, -1};
        auto dfs = [&](auto self, int x, int p) -> void {
            siz[x] = 1;
            for (auto y : adj[x]) {
                if (y == p) continue;
                self(self, y, x);
                siz[x] += siz[y];
                maxs[x] = max(maxs[x], siz[y]);
            }
            maxs[x] = max(maxs[x], tot - siz[x]);
            if (maxs[x] < maxv) maxv = maxs[x], ans[0] = x;
        };
        dfs(dfs, st, -1);

        return ans[0];
    };

    auto query = [&](int x, int y) {
        cout << "? " << x + 1 << " " << y + 1 << endl;
        int t;
        cin >> t;
        return t;
    };
    auto print = [&](int x) {
        cout << "! " << x + 1 << endl;
    };

    int now = 0;
    while (1) {
        if (adj[now].size() == 0) {
            print(now);
            return;
        }
        auto s = getCog(now);
        if (adj[s].size() == 2) {
            int s1 = *adj[s].begin();
            int s2 = *++adj[s].begin();

            int t = query(s1, s2);
            if (t == 1) {
                print(s);
                return;
            } else if (t == 0) {
                adj[s1].erase(s);
                now = s1;
            } else {
                adj[s2].erase(s);
                now = s2;
            }
        } else if (adj[s].size() == 3) {
            array<int, 3> arr{*adj[s].begin(), *++adj[s].begin(), *--adj[s].end()};
            sort(arr.begin(), arr.end(), [&](int u, int v) { return siz[u] > siz[v]; });
            auto [s1, s2, s3] = arr;

            int t = query(s1, s2);
            if (t == 1) {
                adj[s].erase(s1);
                adj[s].erase(s2);
                now = s;
            } else if (t == 0) {
                adj[s1].erase(s);
                now = s1;
            } else if (t == 2) {
                adj[s2].erase(s);
                now = s2;
            }
        } else if (adj[s].size() == 1) {
            int s1 = *adj[s].begin();
            int t = query(s, s1);
            if (t == 0) {
                print(s);
            } else if (t == 2) {
                print(s1);
            }
            return;
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3576kb

input:

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

output:

? 1 3
? 3 4
! 3
? 2 1
! 1

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

output:

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

result: