QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#766699#9570. Binary Treemoonshine_sakeRE 1ms3588kbC++173.1kb2024-11-20 18:16:502024-11-20 18:17:00

Judging History

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

  • [2024-11-20 18:17:00]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3588kb
  • [2024-11-20 18:16:50]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> PII;
typedef long long LL;
const int INF = 0x3f3f3f3f3f3f3f3f;

bool multi = 1;

int query(int u, int v) {
    cout << "? " << u << ' ' << v << ' ' << endl;
    int x;
    cin >> x;
    return x;
}

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

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> adj(n + 1);
    for(int i = 1; i <= n; i++) {
        int x, y;
        cin >> x >> y;
        for(auto t : {x, y}) {
            if(t == 0) continue;
            adj[i].push_back(t);
            adj[t].push_back(i);
        }
    }
    vector<bool> st(n + 1);
    int bg = 1;
    vector<int> tmp;
    auto dfs1 = [&](auto self, int u, int father) -> void {
        tmp.push_back(u);
        for(auto v: adj[u]) {
            if(v == father || st[v]) continue;
            self(self, v, u);
        }
    };
    vector<int> sz(n + 1), mx(n + 1);
    int rt, tot;
    auto dfs2 = [&](auto self, int u, int father) -> void {
        sz[u] = 1, mx[u] = 0;
        for(auto v: adj[u]) {
            if(v == father || st[v]) continue;
            self(self, v, u);
            sz[u] += sz[v];
            mx[u] = max(mx[u], sz[v]);
        }
        mx[u] = max(mx[u], tot - sz[u]);
        if(mx[rt] > mx[u]) rt = u;
    };
    int cnt = 0;
    while(1) {
        cnt++;
        if(cnt > (int)log2(n))
        {
            cout << "! " << 0 << endl;
            return;
        }
        tmp.clear();
        dfs1(dfs1, bg, -1);
        tot = tmp.size();
        mx[rt = 0] = INF;
        dfs2(dfs2, bg, -1);
        vector<int> son;
        for(auto v: adj[rt]) {
            if(st[v]) continue;
            son.push_back(v);
        }
        if(son.size() == 0) {
            answer(rt);
            return;
        }
        if(son.size() == 1) {
            int x = query(rt, son[0]);
            if(x == 0) {
                answer(rt);
                return;
            }else if(x == 2) {
                answer(son[0]);
                return;
            }else{
                // cout << "! " << 0 << endl;
                // return;
            }
        }else if(son.size() == 2) {
            int x = query(son[0], son[1]);
            if(x == 0) {
                bg = son[0];
                st[rt] = 1;
            }else if(x == 1) {
                answer(rt);
                return;
            }else{
                bg = son[1];
                st[rt] = 1;
            }
        }else{
            int x = query(son[0], son[1]);
            if(x == 0) {
                bg = son[0];
                st[rt] = 1;
            }else if(x == 1) {
                bg = rt;
                st[son[0]] = st[son[1]] = 1;
            }else{
                bg = son[1];
                st[rt] = 1;
            }
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int T = 1;
    if(multi) 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: 3588kb

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
? 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
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

output:

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

result: