QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#728767 | #9570. Binary Tree | ucup-team4801# | RE | 0ms | 6236kb | C++17 | 2.6kb | 2024-11-09 15:52:14 | 2024-11-09 15:52:18 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<functional>
#define fi first
#define se second
#define mkp std::make_pair
using ll=long long;
using llu=unsigned long long;
using std::max;
using std::min;
template<class T> void cmax(T&a,T b){a=max(a,b);}
template<class T> void cmin(T&a,T b){a=min(a,b);}
const int NV=1e5;
int QUE(int x,int y){
printf("? %d %d\n",x,y);
fflush(stdout);
int t;
scanf("%d",&t);
return t;
}void ANSWER(int x){
printf("! %d\n",x);
fflush(stdout);
}
namespace xm{
std::vector<int> G[NV+5];
int siz[NV+5],Gc;
bool vis[NV+5];
void dfsgc(int x,int p,int als){
bool F=1;
siz[x]=1;
for(int t:G[x]) if(t!=p&&!vis[t]){
dfsgc(t,x,als);
siz[x]+=siz[t];
if(siz[t]*2>als) F=0;
}
if(F&&(als-siz[x])*2<=als&&!Gc) Gc=x;
}void dfz(int x,int als){
int chc=0,fip;
for(int t:G[x]) if(!vis[t]){
++chc;
fip=t;
}
vis[x]=1;
if(chc==0){
ANSWER(x);
return;
}
if(chc==1){
ANSWER(QUE(x,fip)==0?x:fip);
return;
}
int p;
int nsz=-1;
std::pair<int,int> cs[3];
chc=0;
for(int t:G[x]) if(!vis[t])
cs[chc++]={siz[t]>siz[x]?als-siz[x]:siz[t],t};
std::sort(cs,cs+chc,std::greater<>());
int t=QUE(cs[0].se,cs[1].se);
if(t==0){
p=cs[0].se;
}else if(t==1){
p=cs[2].se;
if(!p){
ANSWER(x);
return;
}
vis[x]=0;
vis[cs[0].se]=vis[cs[1].se]=1;
nsz=cs[2].fi+1;
}else if(t==2){
p=cs[1].se;
}
int tsz=~nsz?nsz:(siz[p]>siz[x]?als-siz[x]:siz[p]);
Gc=0;
dfsgc(p,0,tsz);
dfz(p,tsz);
}void _(){
int N;
scanf("%d",&N);
memset(vis+1,0,sizeof*vis*N);
for(int i=1;i<=N;++i) G[i].clear();
for(int i=1;i<=N;++i){
int u,v;
scanf("%d%d",&u,&v);
if(u){
G[i].push_back(u);
G[u].push_back(i);
}
if(v){
G[i].push_back(v);
G[v].push_back(i);
}
}
Gc=0;
dfsgc(1,0,N);
dfz(Gc,N);
}
}
int main(){
int t;
scanf("%d",&t);
while(t--) xm::_();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 6236kb
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 2
output:
? 2 4 ? 6 1 ? 6 7 ! 7 ? 3 5 ? 3 2 ! 2