QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#770115 | #9570. Binary Tree | eastcloud | TL | 0ms | 3604kb | C++20 | 2.5kb | 2024-11-21 20:43:51 | 2024-11-21 20:43:51 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define cpy(x,y,s) memcpy(x,y,sizeof(x[0])*(s))
#define mset(x,v,s) memset(x,v,sizeof(x[0])*(s))
#define all(x) begin(x),end(x)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ary array
#define eb emplace_back
#define IL inline
using namespace std;
#define N 300005
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9')f=(ch=='-'?-1:f),ch=getchar();
while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x*f;
}
void write(int x){
if(x<0)x=-x,putchar('-');
if(x/10)write(x/10);
putchar(x%10+'0');
}
vi e[N];
int vis[N],siz[N],tot;
int rt,maxn;
void find_root(int x,int fa){
int maxsiz=0;siz[x]=1;
for(auto v:e[x]){
if(v==fa || vis[v])continue;
find_root(v,x);siz[x]+=siz[v];
maxsiz=max(maxsiz,siz[v]);
}
maxsiz=max(maxsiz,tot-siz[x]);
if(maxsiz<maxn)maxn=maxsiz,rt=x;
}
void solve(){
int n=read();
for(int i=1;i<=n;i++)e[i].clear(),vis[i]=0;
for(int i=1;i<=n;i++){
int ls=read(),rs=read();
if(ls)e[i].pb(ls),e[ls].pb(i);
if(rs)e[i].pb(rs),e[rs].pb(i);
}
int x=1,cnt=0;
while(1){
vi S;cnt++;
for(auto v:e[x])if(!vis[v])S.pb(v);
if(!S.size()){cout<<"! "<<x<<endl;break;}
find_root(x,0);rt=0;maxn=n+5;tot=siz[x];find_root(x,0);
x=rt;S.clear();
//cerr<<rt<<' '<<S.size()<<' '<<tot<<"**"<<endl;
for(auto v:e[x])if(!vis[v])S.pb(v);
if(S.size()==1){
int v=e[x][0];
cout<<"? "<<x<<' '<<v<<endl;int res;cin>>res;
if(res==0)cout<<"! "<<x<<endl;
else cout<<"! "<<v<<endl;break;
}
else if(S.size()==2){
int u=S[0],v=S[1];
cout<<"? "<<u<<' '<<v<<endl;int res;cin>>res;
if(res==1){cout<<"! "<<x<<endl;break;}
else if(res==0)vis[x]=1,x=u;
else vis[x]=1,x=v;
}
else{
sort(all(S),[](int x,int y){return siz[x]>siz[y];});
cout<<"? "<<S[0]<<' '<<S[1]<<endl;int res;cin>>res;
if(res==1)vis[S[0]]=vis[S[1]]=1;
else if(res==0)vis[x]=1,x=S[0];
else vis[x]=1,x=S[1];
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int T=read();while(T--)solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 0 2 0 2 0 0 2
output:
? 1 3 ? 4 3 ! 4 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Time Limit Exceeded
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 2 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 5 3 0 5 1 0 0 0 0 4 0 0 2 5 5 0 0 0 0 0 3 0 2 4 1 0 3 3 0 1 0 0 0 2 2 2 0 0 0 0 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 2 4 ? 2 7 ? 1 2 ! 2 ? 5 3 ? 3 1 ? 4 2 ! 4 ? 1 6 ? 1 7 ? 5 1 ! 1 ? 2 4 ? 3 2 ! 2 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 4 5 ! 5 ? 1 4 ? 2 5 ! 2 ? 3 2 ! 2 ? 2 1 ! 2 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 10 9 ! 9 ? 2 1 ! 2 ? 5 9 ? 5 4 ? 8 3 ! 8 ? 5 8 ? 5 7 ? 9 7 ! 9 ? 9 3 ? 9 1 ? 7 2 ! 7 ? 2 1 ! 1 ? 4 3 ? 1 7 ! 7 ? 9 4 ? 2 3 ? 4...