QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#751601#9570. Binary Treeucup-team5799RE 0ms3864kbC++144.1kb2024-11-15 19:38:072024-11-15 19:38:07

Judging History

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

  • [2024-11-15 19:38:07]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3864kb
  • [2024-11-15 19:38:07]
  • 提交

answer

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

struct node{
    int to;
    int id;
};

void solve(){
    int n;
    cin >> n;
    vector<vector<node>> e(n + 1);
    int times = 0;
    vector<int> vi(n * 5);
    for(int i = 1; i <= n; i++){
        int a, b;
        cin >> a >> b;
        if(a != 0){
            e[a].push_back({i, ++times});
            e[i].push_back({a, times});
            vi[times] = 1;
        }
        if(b != 0){
            e[i].push_back({b, ++times});
            e[b].push_back({i, times});
            vi[times] = 1;
        }
    }
    auto getsize = [&](auto&& getsize, int u, int f) -> int {
        int x = 1;
        for(auto [v, id] : e[u]){
            if(v == f)continue;
            if(vi[id] == 0)continue;
            x += getsize(getsize, v, u);
        }
        return x;
    };
    auto find = [&](int x) -> int {
        vector<int> fa(n + 1);
        vector<int> ans(n + 1);
        vector<int> siz(n + 1);
        vector<int> big(n + 1);
        auto dfs = [&](auto&& dfs, int u) -> void {
            // cout << u << endl;
            siz[u] = 1;
            ans[u] = u;
            for(auto [v, id] : e[u]){
                if(vi[id] == 0)continue;
                if(fa[u] == v){continue;}
                fa[v] = u;
                dfs(dfs, v);
                siz[u] += siz[v];
                big[u] = max(big[u], siz[v]);
            }
            for(auto [v, id] : e[u]){
                if(vi[id] == 0)continue;
                if(fa[u] == v){continue;}
                int p = ans[v];
                while(p != u){
                    if(max(big[p], siz[u] - siz[p]) <= siz[u] / 2){
                        ans[u] = p;
                        break;
                    }else{
                        p = fa[p];
                    }
                }
            }
        };
        fa[x] = x;
        dfs(dfs, x);
        return ans[x];
    };
    auto work = [&](auto&& work, int u) -> void {
        // cout << "aaaaa" << endl;
        u = find(u);
        int cnt = 0;
        vector<node> temp;
        for(auto [v, id] : e[u]){
            if(vi[id]){cnt++; temp.push_back({v, id});}
        }
        if(cnt == 0){cout << "! " << u << endl;}
        else if(cnt == 1){
            int f = temp[0].to;
            cout << "? " << f << " " << u << endl;
            int op;
            cin >> op;
            if(op == 0){cout << "! " << f << endl;return ;}
            else if(op == 2){cout << "! " << u << endl; return ;}
        }
        else if(cnt == 2){
            int f = temp[0].to;
            int v = temp[1].to;
            cout << "? " << f << " " << v << endl;
            int op;
            cin >> op;
            if(op == 1){
                cout << "! " << u << endl;
                return ;
            }
            else if(op == 0){
                vi[temp[0].id] = 0;
                work(work, f);
            }
            else if(op == 2){
                vi[temp[1].id] = 0;
                work(work, v);
            }
        }
        else if(cnt == 3){
            vector<pair<int, pair<int, int>>> tt;
            for(auto [v, id] : e[u]){
                tt.push_back({getsize(getsize, v, u), {v, id}});
            } 
            // sort(tt.begin(), tt.end());
            int f = tt[2].second.first;
            int v = tt[1].second.first;
            cout << "? " << f << " " << v << endl;
            int op;
            cin >> op;
            if(op == 1){
                vi[tt[1].second.second] = 0;
                vi[tt[2].second.second] = 0;
                work(work, u);
            }
            else if(op == 0){
                vi[tt[2].second.second] = 0;
                work(work, f);
            }
            else if(op == 2){
                vi[tt[1].second.second] = 0;
                work(work, v);
            }
        }
    };
    work(work, 1);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    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: 3864kb

input:

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

output:

? 3 5
? 2 1
! 2
? 1 2
! 2

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

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

result: