QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#755357 | #9570. Binary Tree | yld | RE | 0ms | 3580kb | C++20 | 3.0kb | 2024-11-16 17:07:49 | 2024-11-16 17:07:49 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
// #define endl '\n'
using namespace std;
int ask(int x,int y)
{
cout<<"? "<<x<<' '<<y<<endl;
int res;cin>>res;
return res;
}
void solve()
{
int n;cin>>n;
set<int> e[n+1];
for(int i=1;i<=n;i++)
{
int v1,v2;
cin>>v1>>v2;
if(v1) e[i].insert(v1),e[v1].insert(i);
if(v2) e[i].insert(v2),e[v2].insert(i);
}
vector<int> dep(n+1);
vector<int> fa(n+1);
function<void(int,int)> dfs=[&](int u,int f)
{
dep[u]=dep[f]+1;
fa[u]=f;
for(auto v:e[u])
{
if(v==f) continue;
dfs(v,u);
}
};
int l=1,r,maxn=0,times=0;
while(times<log2(n))
{
fill(dep.begin(),dep.end(),0);
fill(fa.begin(),fa.end(),0);
maxn=0;
dfs(l,0);
for(int i=1;i<=n;i++)
{
if(dep[i]>maxn)
{
maxn=dep[i];
l=i;
}
}
fill(dep.begin(),dep.end(),0);
fill(fa.begin(),fa.end(),0);
// for(int i=1;i<=n;i++) cout<<dep[i]<<' ';
// cout<<l<<endl;
dfs(l,0);
// for(int i=0;i<=n;i++) cout<<dep[i]<<' ';
// cout<<endl;
maxn=0;
// cerr<<"============\n";
for(int i=1;i<=n;i++)
{
// cout<<maxn<<' '<<dep[i]<<endl;
if(maxn<dep[i])
{
maxn=dep[i];
r=i;
}
}
// cout<<l<<' '<<r<<endl;
vector<int> tmp;
for(int i=r;;i=fa[i])
{
tmp.push_back(i);
if(i==l) break;
}
reverse(tmp.begin(),tmp.end());
// for(auto x:tmp) cout<<x<<' ';
// cout<<endl;
if(l==r){cout<<"! "<<l<<endl;return;}
int op=ask(l,r);
if(op==0)
{
int mid=tmp.size()/2-1;
// cout<<mid<<endl;
if(mid+1<tmp.size() && mid>=0 && mid<tmp.size())e[tmp[mid]].erase(tmp[mid+1]);
}
if(op==1)
{
int mid=tmp.size()/2;
if(mid+1<tmp.size() && mid>=0 && mid<tmp.size())e[tmp[mid]].erase(tmp[mid+1]);
if(mid-1>=0 && mid>=0 && mid<tmp.size())e[tmp[mid]].erase(tmp[mid-1]);
l=tmp[mid];
}
if(op==2)
{
int mid=(tmp.size()+1)/2;
// cout<<mid<<endl;
// cout<<tmp[mid]<<endl;
if(mid-1>=0 && mid>=0 && mid<tmp.size())e[tmp[mid]].erase(tmp[mid-1]);
// cout<<*lower_bound(e[tmp[mid]].begin(),e[tmp[mid]].end(),tmp[mid-1])<<endl;
// for(auto v:e[2]) cout<<v<<' ';
// cout<<endl;
l=r;
// cout<<"l:"<<l<<endl;
}
times++;
}
cout<<"! "<<l<<endl;
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
int t=1;
cin>>t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 1 2 0 2 0 0 2
output:
? 4 1 ? 5 1 ! 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 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 1 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 0 0 5 4 5 3 1 0 0 0 0 0 0 2 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 2 0 5 3 0 5 1 0 0 0 0 4 0 2 2 5 5 0 0 0 0 0 3 0 2 4 2 2 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 0 10 2 8 9 7 0 0 ...
output:
? 3 7 ? 1 7 ? 2 1 ! 1 ? 6 1 ? 3 1 ? 4 2 ! 4 ? 2 7 ? 5 7 ? 1 5 ! 1 ? 3 4 ? 5 4 ! 5 ? 2 1 ? 4 1 ! 4 ? 4 3 ? 1 3 ! 3 ? 3 1 ? 2 1 ! 1 ? 2 3 ! 3 ? 2 1 ! 1 ? 2 3 ! 2 ? 3 8 ? 5 3 ? 4 3 ! 6 ? 2 1 ! 2 ? 4 6 ? 1 6 ? 2 6 ! 6 ? 2 9 ? 1 9 ? 5 1 ! 1 ? 5 1 ? 4 5 ? 6 5 ! 6 ? 2 1 ! 2 ? 2 1 ? 7 1 ! 4 ? 3 1 ? 7 1 ? 9 ...