QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#722239 | #9570. Binary Tree | Beater# | WA | 1ms | 3844kb | C++20 | 2.3kb | 2024-11-07 18:15:59 | 2024-11-07 18:16:00 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
int ask(int u,int v)
{
cout<<"? "<<u<<" "<<v<<endl;
int t;
cin>>t;
return t;
}
int n,st,ed;
int L[100010],R[100010],in[100010];
void dfs(int u)
{
if(L[u]&&R[u])
{
int t=ask(L[u],R[u]);
if(t==1)
{
ed=u;
return ;
}
else if(t==0) dfs(L[u]);
else dfs(R[u]);
}
else if(L[u]||R[u])
{
if(L[u]) dfs(L[u]);
else dfs(R[u]);
}
else
{
ed=u;
return ;
}
}
int fa[100010];
void solve()
{
cin>>n;
for(int i=1;i<=n;i++) in[i]=fa[i]=0;
for(int i=1;i<=n;i++)
{
cin>>L[i]>>R[i];
fa[L[i]]=i;
fa[R[i]]=i;
in[L[i]]++;
in[R[i]]++;
}
int root;
for(int i=1;i<=n;i++)
if(!in[i])
{
root=i;
break;
}
ed=0;
dfs(root);
vector<int>path;
path.push_back(ed);
while(!L[fa[ed]]||!R[fa[ed]])
{
ed=fa[ed];
if(ed==0) break;
path.push_back(ed);
}
if(path.size()==1) cout<<"! "<<path[0]<<"\n";
else
{
int l=0,r=path.size()-1;
while(1)
{
int t=ask(path[l],path[r]);
if(t==1)
{
cout<<"! "<<path[(l+r)/2]<<"\n";
return ;
}
else if(t==0)
{
int len=r-l+1;
if(len&1)
{
r=l+len/2-1;
}
else
{
r=l+len/2-1;
}
}
else
{
int len=r-l+1;
if(len&1)
{
l=r-len/2+1;
}
else
{
l=r-len/2+1;
}
}
if(l==r) break;
}
cout<<"! "<<path[l]<<"\n";
}
}
int main (){
ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
cout.flush();
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3840kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 0 1 2 0 2 0 0 2
output:
? 2 4 ? 1 5 ! 2 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3844kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 2 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 1 1 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 2 1
output:
? 8 6 ? 5 4 ? 3 4 ! 3 ? 7 8 ? 1 4 ? 2 7 ! 3 ? 1 7 ? 5 8 ? 4 2 ? 6 8
result:
wrong answer Too many queries (test case 3)