QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#760779 | #9570. Binary Tree | kkkkk | RE | 2ms | 12920kb | C++20 | 2.2kb | 2024-11-18 19:09:57 | 2024-11-18 19:09:57 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef pair<ll,ll> PII;
typedef pair<ll,PII> PIII;
#define x first
#define y second
const ll N =2e5+100,M=N,INF = 1e18;
ll n,m,k;
multiset<ll> v[N];
ll root=-1;
ll sz[N];
ll ans=1e9;
ll dfszx(ll u,ll fa,ll pp){
ll sum=1,res=0;
for(auto j:v[u]){
if(j!=fa){
ll s=dfszx(j,u,pp);
res=max(res,s);
sum+=s;
}
}
res=max(res,pp-sum);
if(res<ans){
ans=res;
root=u;
}
return sum;
}
void dfs(ll u,ll fa){
sz[u]=1;
for(auto j:v[u]){
if(j!=fa){
dfs(j,u);
sz[u]+=sz[j];
}
}
}
bool cmp(ll x,ll y){
return sz[x]>=sz[y];
}
void solve(){
cin>>n;
for(ll i=1;i<=n;i++) v[i].clear();
root=-1;
for(ll i=1;i<=n;i++){
ll x,y;cin>>x>>y;
if(x) v[i].insert(x),v[x].insert(i);
if(y) v[i].insert(y),v[y].insert(i);
}
ll pp=1;
ll szz=n;
while(1){
ans=1e9;
dfszx(pp,-1,szz);
dfs(root,-1);
if(v[root].size()==1){
cout<<"? "<<root<<" "<<*v[root].begin()<<endl;
ll x;cin>>x;
if(x==0) cout<<"! "<<root<<endl;
else cout<<"! "<<*v[root].begin()<<endl;
return ;
}
else if(v[root].size()==2){
ll p1=*v[root].begin(),p2=*(--v[root].end());
cout<<"? "<<p1<<" "<<p2<<endl;
ll x;cin>>x;
if(x==0){
pp=p1;
szz=sz[pp];
v[pp].erase(root);
}
else if(x==1){
cout<<"! "<<root<<endl;
return ;
}
else{
pp=p2;
szz=sz[pp];
v[pp].erase(root);
}
}
else{
vector<ll> pl;
for(auto j:v[root]) pl.push_back(j);
sort(pl.begin(),pl.end(),cmp);
cout<<"? "<<pl[0]<<" "<<pl[1]<<endl;
ll x;cin>>x;
if(x==0){
pp=pl[0];
szz=sz[pp];
v[pp].erase(root);
}
else if(x==1){
pp=root;
szz=sz[pl[2]]+1;
v[pp].erase(pl[0]);
v[pp].erase(pl[1]);
}
else if(x==2){
pp=pl[1];
szz=sz[pp];
v[pp].erase(root);
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
ll __=1;cin>>__;
while(__--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 12920kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 5 ? 1 2 ! 1 ? 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 0 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 1 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 0 2 5 4 5 3 1 0 0 0 0 0 0 0 2 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 0
output:
? 2 4 ? 2 7 ? 1 2 ! 2 ? 3 5 ? 4 3 ? 1 2 ! 1 ? 1 6 ? 1 7 ? 5 1 ! 1 ? 2 5 ? 3 2 ! 2 ? 5 7 ? 1 4