QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#796445 | #9570. Binary Tree | ZycK# | RE | 2ms | 6136kb | C++14 | 2.0kb | 2024-12-01 18:45:12 | 2024-12-01 18:45:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn=1e5+4;
vector<int> G[maxn];
int rt,sz[maxn],rtv;
bool used[maxn];
int cnt;
void dfs(int p,int fa)
{
sz[p]=1;
int t=1e9;
for(auto v:G[p])
{
if(v==fa||used[v]) continue;
dfs(v,p);
sz[p]+=sz[v];
t=min(t,sz[v]);
}
if(t<rtv||(!rt)) rt=p,rtv=t;
}
int ans;
int ask(int u,int v)
{
assert(cnt);
cnt--;
cout<<"? "<<u<<" "<<v<<endl;
cout.flush();
int x; cin>>x;
if(x==-1) exit(0);
return x;
}
void calc(int p)
{
dfs(p,p);
if(sz[p]==1) {ans=p; return ;}
int t1=0,t2=0;
for(auto x:G[p])
{
if(!used[x])
{
if(!t1) t1=x;
else t2=x;
}
}
if(!t1)
{
ans=p;
return;
}
if(!t2)
{
int t=ask(t1,p);
if(t==0) ans=t1;
else ans=p;
return;
}
int t=ask(t1,t2);
if(t==0)
{
for(auto x:G[p])
{
if(x!=t1) used[x]=1;
}
used[p]=1;
rt=0; sz[0]=1e9; rtv=1e9;
dfs(t1,t1);
dfs(rt,rt);
}
else if(t==2)
{
for(auto x:G[p])
{
if(x!=t2) used[x]=1;
}
used[p]=1;
rt=0; sz[0]=1e9; rtv=1e9;
dfs(t2,t2);
dfs(rt,rt);
}
else
{
used[t1]=1,used[t2]=1;
rt=0; sz[0]=1e9; rtv=1e9;
dfs(p,p); dfs(rt,rt);
}
calc(rt);
}
void solve()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) G[i].resize(0),used[i]=0;
cnt=log2(n);
for(int i=1;i<=n;i++)
{
int x,y; cin>>x>>y;
if(x) G[x].push_back(i),G[i].push_back(x);
if(y) G[y].push_back(i),G[i].push_back(y);
}
rt=0; sz[0]=1e9; rtv=1e9;
dfs(1,1); dfs(rt,rt);
calc(rt);
cout<<"! "<<ans<<endl;
}
int main()
{
int T; cin>>T;
while(T--) solve();
}
/*
2
5
0 0
1 5
2 4
0 0
0 0
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 6136kb
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
Runtime Error
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 0 0
output:
? 3 8 ? 2 7 ? 2 5