QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#727889 | #9570. Binary Tree | ucup-team5697# | TL | 0ms | 0kb | C++23 | 1.5kb | 2024-11-09 14:05:07 | 2024-11-09 14:05:08 |
answer
#include<bits/stdc++.h>
using namespace std;bool Mbe;
inline int Ask(int x,int y){printf("? %d %d\n",x,y);scanf("%d",&x);return x;}
inline void Rep(int x){printf("! %d\n",x);}
namespace MAOJUN{
const int N=1e5+5;
int n;
const int E=N<<1;
int tot=0,hd[N],to[E],nxt[E];
inline void Add(int u,int v){to[++tot]=v;nxt[tot]=hd[u];hd[u]=tot;}
int al,mn,wc,sz[N];bool vs[N];
void gwc(int u,int f){
int mx=0;sz[u]=1;
for(int i=hd[u];i;i=nxt[i])if(int v=to[i];v!=f&&!vs[v]){
gwc(v,u);sz[u]+=sz[v];mx=max(mx,sz[v]);
}
mx=max(mx,al-sz[u]);if(mx<mn){mn=mx;wc=u;}
}
int dvd(int u){
vector<int>sn;vs[u]=1;gwc(u,0);
for(int i=hd[u];i;i=nxt[i])if(int v=to[i];!vs[v])sn.emplace_back(v);
if(sn.size()==0)return u;
else if(sn.size()==1)return Ask(u,sn[0])?sn[0]:u;
else if(sn.size()==2){
int x=Ask(sn[0],sn[1]);
if(x==1)return u;
int v=x?sn[1]:sn[0];
al=mn=sz[v];gwc(v,u);
return dvd(wc);
}
int x=Ask(sn[0],sn[1]);
if(x==1){
vs[u]=0;vs[sn[0]]=vs[sn[1]]=1;
al=mn=sz[sn[2]]+1;gwc(u,0);return dvd(wc);
}
int v=x?sn[1]:sn[0];
al=mn=sz[v];gwc(v,u);
return dvd(wc);
}
inline void slv(){
scanf("%d",&n);
tot=0;for(int i=1;i<=n;i++)hd[i]=vs[i]=0;
for(int i=1,u,v;i<=n;i++){
scanf("%d%d",&u,&v);
if(u){Add(i,u);Add(u,i);}
if(v){Add(i,v);Add(v,i);}
}
al=mn=n;gwc(1,0);Rep(dvd(wc));
}
inline void main(){
int T;scanf("%d",&T);while(T--)slv();
}
}bool Med;int main(){
MAOJUN::main();
#ifdef LOCAL
fprintf(stderr,"%lfs\n",clock()/1000.);
fprintf(stderr,"%lfMB\n",(&Mbe-&Med)/1024./1024);
#endif
return 0;
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
2 5 0 0 1 5 2 4 0 0 0 0