QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#748536 | #9570. Binary Tree | acansaidong | WA | 6ms | 7836kb | C++20 | 2.3kb | 2024-11-14 20:39:18 | 2024-11-14 20:39:19 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int N=2e5+10;
const int M=0x3f3f3f3f3f3f3f3f;
vector<int>e[N];
int a[N],b[N],vis[N];
int yu,n;
void f()
{
queue<pair<int,int>>q;
for(int i=1;i<=n;i++) if(e[i].size()==1) q.push({i,1});
while(q.size())
{
int x=q.front().first,y=q.front().second;q.pop();
// cerr<<x<<"\n";
if(vis[x]!=0) continue;
vis[x]=max(vis[x],y);
for(auto i:e[x])
{
if(vis[i]==0) q.push({i,y+1});
}
}
}
signed main()
{
// ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int T;cin>>T;
while(T--)
{
cin>>n;int ans=1;
for(int i=1;i<=n;i++) vis[i]=0,e[i].clear();
for(int i=1;i<=n;i++)
{
int x,y;cin>>x>>y;
a[i]=x;b[i]=y;
if(y)
{
e[i].push_back(y);
e[y].push_back(i);
}
if(x)
{
e[i].push_back(x);
e[x].push_back(i);
}
}
int u,v,t,la=0,mx=-1;
int yui=floor(log2(n));
f();
for(int i=1;i<=n;i++)
{
if(vis[i]>mx) mx=vis[i],yu=i;
else if(vis[i]==mx&&e[i].size()>e[yu].size()) yu=i;
// cerr<<vis[i]<<" ";
vis[i]=0;
}
// cerr<<yu<<"\n";
while(yui--)
{
int sum=0;
for(auto i:e[yu]) if(vis[i]==0) sum++;
sum=e[yu].size();
if(sum==0) {ans=u;break;}
else if(sum==1)
{
u=yu;
for(auto i:e[yu]) if(vis[i]==0) v=i;
}
else if(sum==2) u=e[yu][0],v=e[yu][1];
else
{
u=v=-1;
for(auto i:e[yu])
{
if(vis[i]==0&&u==-1)
{
u=i;
}
else if(vis[i]==0&&u!=-1)
{
v=i;break;
}
}
}
vis[u]=vis[v]=1;
// cout<<sum<<" ";
cout<<"? "<<u<<" "<<v<<endl;
cin>>t;
if(sum==1)
{
if(t==0) {ans=u;break;}
else {ans=v;break;}
}
else if(sum==2)
{
if(t==1) {ans=yu;break;}
else if(t==0) yu=u,ans=u;
else yu=v,ans=v;
}
else
{
if(t==0) yu=u,ans=u;
else if(t==1) {for(auto i:e[yu]) if(vis[i]==0) {yu=i,ans=i;break;}}
else if(t==2) yu=v,ans=v;
}
}
cout<<"! "<<ans<<"\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5724kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0 2
output:
? 5 1 ? 4 2 ! 3 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 6ms
memory: 7836kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 1 2 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 0 1 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 1 1 5 4 5 3 1 0 0 0 0 0 0 1 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 1 1 5 3 0 5 1 0 0 0 0 4 0 0 1 5 5 0 0 0 0 0 3 0 2 4 2 0 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 1 10 2 8 9 7 0 0 ...
output:
? 1 6 ? 2 4 ? 3 8 ! 3 ? 5 3 ? 2 7 ? 4 1 ! 2 ? 1 6 ? 8 5 ? 7 1 ! 3 ? 5 4 ? 1 3 ! 1 ? 6 5 ? 8 3 ! 7 ? 1 5 ? 3 2 ! 1 ? 1 4 ? 3 5 ! 3 ? 3 2 ! 2 ? 1 2 ! 2 ? 3 2 ! 1 ? 1 7 ? 2 5 ? 3 4 ! 6
result:
wrong answer There are 2 candidates. (test case 11)