QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#757985#9570. Binary Treeucup-team173#RE 1ms3648kbC++202.4kb2024-11-17 14:59:472024-11-17 14:59:47

Judging History

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

  • [2024-11-17 14:59:47]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3648kb
  • [2024-11-17 14:59:47]
  • 提交

answer

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

using ll = long long;

void solve() {
    int n;
    cin >> n;
    vector G(n + 1, vector<int>());
    for(int i = 1; i <= n; i++) {
        int l, r;
        cin >> l >> r;
        if(l) G[l].push_back(i), G[i].push_back(l);
        if(r) G[r].push_back(i), G[i].push_back(r);
    }
    vector<int> vis(n + 1), sz(n + 1);
    int mn, p, num;
    auto dfs1 = [&](auto self, int x, int fz) -> void {
        num++;
        for(int y : G[x]) if(y != fz && !vis[y]) {
            self(self, y, x);
        }
    };
    auto dfs2 = [&](auto self, int x, int fz) -> void {
        int mx = 0;
        sz[x] = 1;
        for(int y : G[x]) if(y != fz && !vis[y]) {
            self(self, y, x);
            sz[x] += sz[y];
            mx = max(mx, sz[y]);
        }
        mx = max(mx, num - sz[x]);
        if(mx < mn) {
            mn = mx, p = x;
        }
    };
    auto getRt = [&](int x) {
        mn = 1e9, num = 0;
        dfs1(dfs1, x, -1);
        dfs2(dfs2, x, -1);
        dfs2(dfs2, p, -1);
        return p;
    };
    p = 1;
    while(1) {
        int rt = getRt(p);
        if(sz[rt] == 1) {
            cout << "! " << rt << endl;
            break;
        }
        auto query = [&](int x, int y) {
            cout << "? " << x << ' ' << y << endl;
            int res; cin >> res; return res;
        };
        if(G[rt].size() == 1) {
            int son = G[rt][0];
            int res = query(rt, son);
            if(res == 0) { // rt < son
                p = rt, vis[son] = 1;
            } else if(res == 2) { // son < rt
                p = son, vis[rt] = 1;
            } else {
                assert(false);
            }
        } else {
            assert(G[rt].size() <= 3);
            sort(G[rt].begin(), G[rt].end(), [&](int x, int y) { return sz[x] > sz[y]; });
            int son1 = G[rt][0], son2 = G[rt][1];
            int res = query(son1, son2);
            if(res == 0) { // son1 < son2
                p = son1, vis[rt] = 1;
            } else if(res == 1) { // son1 == son2
                p = rt, vis[son1] = vis[son2] = 1;
            } else { // son1 > son2
                p = son2, vis[rt] = 1;
            }
        }
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    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: 1ms
memory: 3648kb

input:

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

output:

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

output:

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

result: