QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#728321 | #9570. Binary Tree | ucup-team5697# | RE | 0ms | 3872kb | C++14 | 1.7kb | 2024-11-09 14:58:13 | 2024-11-09 14:58:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;bool Mbe;
inline int Ask(int x,int y){printf("? %d %d\n",x,y);fflush(stdout);scanf("%d",&x);assert(0<=x&&x<=2);return x;}
inline void Rep(int x){printf("! %d\n",x);fflush(stdout);}
namespace MAOJUN{
const int N=1e5+5;
int n;
const int E=N<<1;
int tot,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(){
#ifdef LOCAL
freopen("1.in","r",stdin);
freopen("data.out","w",stdout);
#endif
MAOJUN::main();
#ifdef LOCAL
fprintf(stderr,"%lfs\n",clock()/1000.);
fprintf(stderr,"%lfMB\n",(&Mbe-&Med)/1024./1024);
#endif
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3872kb
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 1 1 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 1 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 2 2 5 4 5 3 1 0 0 0 0 0 0 0 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 1 1 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 0 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 ...
output:
? 4 5 ? 6 8 ? 1 2 ! 1 ? 3 5 ? 3 4 ? 1 2 ! 2 ? 6 1 ? 7 1 ? 5 1 ! 1 ? 2 5 ? 3 2 ! 3 ? 7 6 ? 4 1 ? 3 5 ! 3 ? 1 5 ? 3 1 ! 1 ? 4 2 ? 3 4 ! 3 ? 2 3 ! 3 ? 2 1 ! 2 ? 3 2 ! 2 ? 5 6 ? 7 9 ? 8 2 ! 2 ? 2 1 ! 2 ? 9 5 ? 9 6 ? 1 9 ! 1 ? 8 5 ? 5 1 ? 9 3 ! 7 ? 9 4 ? 9 7 ? 1 2 ! 2 ? 2 1 ! 1 ? 6 3 ? 7 1 ? 5 4