QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#734018 | #9570. Binary Tree | 20225954# | RE | 0ms | 0kb | C++20 | 1.5kb | 2024-11-10 23:05:47 | 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;
}
Details
Tip: Click on the bar to expand more detailed information
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