QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#728623 | #9570. Binary Tree | ucup-team4801# | ML | 1ms | 6168kb | C++17 | 2.9kb | 2024-11-09 15:32:29 | 2024-11-09 15:32:39 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#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 mxd,dp,stk[NV+5];
void dfs(int x,int p,int d){
if(d>mxd){
mxd=d;
dp=x;
}
for(int t:G[x]) if(t!=p){
dfs(t,x,d+1);
}
}bool dfstar(int x,int p,int tar){
if(x==tar){
stk[++*stk]=x;
return 1;
}
for(int t:G[x]) if(t!=p)
if(dfstar(t,x,tar)){
stk[++*stk]=x;
return 1;
}
return 0;
}
void _(){
int N;
scanf("%d",&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);
}
}
int rt=1;
for(;;){
if(G[rt].size()==0){
ANSWER(rt);
return;
}
*stk=mxd=dp=0;
dfs(rt,0,0);
int u=dp,v;
mxd=dp=0;
dfs(u,0,0);
v=dp;
dfstar(u,0,v);
std::reverse(stk+1,stk+*stk+1);
int t=QUE(u,v);
if(t==0){
if(*stk==2){
ANSWER(u);
return;
}
int p=stk[*stk/2];
G[p].erase(std::find(G[p].begin(),G[p].end(),
stk[*stk/2+1]));
rt=p;
}else if(t==1){
int p=stk[*stk/2+1];
G[p].erase(std::find(G[p].begin(),G[p].end(),
stk[*stk/2]));
G[p].erase(std::find(G[p].begin(),G[p].end(),
stk[*stk/2+2]));
rt=p;
}else{
if(*stk==2){
ANSWER(v);
return;
}
int p=stk[*stk-*stk/2+1];
G[p].erase(std::find(G[p].begin(),G[p].end(),
stk[*stk-*stk/2]));
rt=p;
}
}
}
}
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: 1ms
memory: 6168kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 1 2 0 2 0 0 2
output:
? 4 1 ? 1 5 ! 2 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Memory Limit Exceeded
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 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 2 0 0 5 4 5 3 1 0 0 0 0 0 0 2 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 2 0 5 3 0 5 1 0 0 0 0 4 0 2 2 5 5 0 0 0 0 0 3 0 2 4 2 2 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 0 10 2 8 9 7 0 0 ...
output:
? 3 7 ? 7 1 ? 7 6 ! 6 ? 6 1 ? 1 4 ? 3 2 ! 3 ? 4 7 ? 7 5 ? 7 3 ! 7 ? 3 4 ? 4 5 ! 4 ? 2 1 ? 1 4 ! 1 ? 4 3 ? 3 1 ! 1 ? 3 1 ? 1 2 ! 2 ? 3 2 ! 2 ? 2 1 ! 1 ? 2 3 ! 2 ? 4 8 ? 4 5 ? 4 3 ! 6 ? 2 1 ! 2 ? 4 6 ? 6 1 ? 1 9 ! 9 ? 6 9 ? 9 1 ? 1 5 ! 5 ? 6 1 ? 6 4 ? 6 5 ! 6 ? 2 1 ! 2 ? 2 1 ? 1 7 ! 4 ? 3 1 ? 1 7 ? 7 ...