QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#758303#9570. Binary TreeYuuRE 0ms0kbC++233.0kb2024-11-17 17:35:592024-11-17 17:36:00

Judging History

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

  • [2024-11-17 17:36:00]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-17 17:35:59]
  • 提交

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::set<int>>G(n + 1);
    std::vector<int>vis(n + 1);

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

    int root = 0;
    for(int i = 1;i <= n;i++){
        if(vis[i] == 0){
            root = i;
            break;
        }
    }

    std::vector<std::vector<int>>del(n + 1);
    auto dfs = [&](auto& self, int u, int pa = 0)->void{
        for(auto v : G[u]) {
            if(v == pa) continue ;
            del[v].push_back(u);
            self(self, v, u);
        }
    };

    dfs(dfs, root);

    for(int i = 1;i <= n;i++){
        for(auto u : del[i]){
            G[i].erase(u);
        }
    }

    std::vector<int>vec;
    int f = root;

    auto DFS = [&](auto&self, int u)->void{
        vec.push_back(u);
        if(G[u].size() == 2) return ;
        if(G[u].size() == 1) self(self, *G[u].begin());
        return ;
    };

    while(G[f].size()){
        if(G[f].size() == 2){
            int ls = *G[f].begin(), rs = *G[f].rbegin();
            int t = ask(ls, rs);
            if(t == 1){
                std::cout<<"! "<<t<<endl;
                return ;
            }
            f = (t == 0?ls : rs);
        }else{
            vec.clear();
            DFS(DFS, f);
            if(vec.size() == 1){
                std::cout<<"! "<<vec.back()<<endl;
                return ;
            }else {
                int last = vec.back();
                if(G[last].size() == 2){
                    int ls = *G[last].begin(), rs = *G[last].rbegin();
                    int t = ask(ls, rs);
                    if(t != 1){
                        f = (t == 0?ls : rs);
                        continue ;
                    }
                }
                //  s在 vec[]内
                int l = 0, r = vec.size() - 1;
                while(l + 1 != r){
                    int t = ask(vec[l], vec[r]);
                    if(t == 1){
                        std::cout<<"! "<<vec[(l + r) / 2]<<endl;
                        return ;
                    }
                    int mid = l + r >> 1;
                    if(t == 0) r = mid; else l = mid;
                }

                int t = ask(vec[l], vec[r]);
                std::cout<<"! "<<(t == 0?vec[l]:vec[r])<<endl;              
                return ;
            }
        }
    }

    std::cout<<"! "<<f<<endl;
}

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: 0
Runtime Error

input:

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

output:

? 2 4
? 1 5
! 1

result: