QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#758506#9570. Binary TreeYuuRE 1ms3812kbC++232.7kb2024-11-17 18:37:292024-11-17 18:37:29

Judging History

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

  • [2024-11-17 18:37:29]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3812kb
  • [2024-11-17 18:37:29]
  • 提交

answer

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

//  The 2024 ICPC Asia Nanjing Regional Contest

int ask(int u, int v){
    std::cout<<"? "<<u<<" "<<v<<endl;
    int res; std::cin>>res;
    return res;
}

void sol(){
    int n; std::cin>>n;

    const int K = std::__lg(n);
    std::vector<std::vector<int>>G(n + 1);

    for(int i = 1;i <= n;i++){
        int l, r;
        std::cin>>l>>r;
        if(l){
            G[i].push_back(l);
            G[l].push_back(i);
        }
        if(r){
            G[i].push_back(r);
            G[r].push_back(i);
        }
    }

    int cur = 1, rt = 0, M = n;
    std::vector<int>sz(n + 1);
    std::vector<int>del(n + 1);
    std::vector<int>V(n + 1);
    V[0] = n;

    auto dfs = [&](auto& self, int u, int pa = 0)->void{
        sz[u] = 1, V[u] = 0;
        for(auto v : G[u]) {
            if(pa == v || del[v] == 1) continue;
            self(self, v, u);
            sz[u] += sz[v];
            V[u] = std::max(V[u], sz[v]);
        }
        V[u] = std::max(V[u], M - sz[u]);
        if(V[rt] > V[u]) rt = u;
    };

    while(true){
        rt = 0;
        dfs(dfs, cur);

        std::vector<std::array<int, 2>>vec;
        int du = 0;
        for(auto v : G[rt]) if(del[v] == 0){
            vec.push_back({sz[v], v});
            du++;
        }

        if(du == 0){
            std::cout<<"! "<<rt<<endl;
            return ;
        }

        if(du == 1){
            int ot = vec.back()[1];
            int t = ask(rt, ot);
            std::cout<<"! "<<(t == 0?rt : ot)<<endl;
            return ;
        }

        if(du == 2){
            auto [s1, v1] = vec[0];
            auto [s2, v2] = vec[1];
            int t = ask(v1, v2);
            if(t == 1){
                std::cout<<"! "<<rt<<endl;
                return ;
            } 
            cur = (t == 0?v1 : v2);
            del[rt] = del[v2 ^ v1 ^ cur] = 1; 
            M = sz[cur];
        }

        if(du == 3){
            std::sort(vec.begin(), vec.end());

            auto [s1, v1] = vec[0];
            auto [s2, v2] = vec[1];
            auto [s3, v3] = vec[2];
            
            int t = ask(v2, v3);
            if(t == 1){
                del[v2] = del[v3] = 1;
                M = sz[v1] + 1;
                cur = rt;
            }else if(t == 2){
                del[rt] = 1;
                M = sz[v3];
                cur = v3;
            }else{
                del[rt] = 1;
                M = sz[v2];
                cur = v2;
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0), cout.tie(0);
    int t = 1; std::cin>>t;
    while(t--) sol();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3812kb

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

output:

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

result: