QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#734018#9570. Binary Tree20225954#RE 0ms0kbC++201.5kb2024-11-10 23:05:472024-11-10 23:05:47

Judging History

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

  • [2024-11-10 23:05:47]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-10 23:05:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+5;
map<pair<int,int>,bool> vis;
vector<int> g[maxn];
int d[maxn];
int fa[maxn];
int n;
int nn;
void dfs(int u,int f){
    fa[u]=f;
    d[u]=1;
    for(int v:g[u]){
        if(v==f)continue;
        if(vis[{u,v}])continue;
        dfs(v,u);
        d[u]+=d[v];
    }
}
void di(int now){
    for(int i=1;i<=n;i++)d[i]=-1;
    dfs(now,0);
    vector<pair<int,int> > tmp;
    for(int i=1;i<=n;i++){
        if(d[i]!=-1)tmp.push_back({d[i],i});
    }
    sort(tmp.begin(),tmp.end());
    int mid=(nn+1)/2;
    mid--;
    pair<int,int> pos=*lower_bound(tmp.begin(),tmp.end(),make_pair(mid,0));
    int v=pos.second;
    int u=fa[v];
    cout<<"? "<<u<<" "<<v<<endl;
    int t=0;
    cin>>t;
    vis[{fa[u],u}]=1;
    vis[{u,fa[u]}]=1;
    if(t==0){
        // u
        nn=nn-d[v];
        if(nn==1){
            cout<<u<<endl;
            return;
        }
        di(u);
    }else if(t==1){
        // v
        nn=d[v];
        if(nn==1){
            cout<<v<<endl;
            return;
        }
        di(v);
    }else{
        // ans
    }
}
void solve(){
    cin>>n;
    nn=n;
    for(int i=1;i<=n;i++){
        int u,v;
        cin>>u>>v;
        if(u!=0){
            g[u].push_back(i);
            g[i].push_back(u);
        }
        if(v!=0){
            g[v].push_back(i);
            g[i].push_back(v);
        }
    }
    di(1);
}
int main(){
    int _=1;
    cin>>_;
    while(_--)solve();
    return 0;
}

詳細信息

Test #1:

score: 0
Runtime Error

input:

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

output:

? 2 3
? 3 4
? 3 4

result: