QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#758328 | #9570. Binary Tree | Asrit | TL | 1ms | 5860kb | C++14 | 2.7kb | 2024-11-17 17:48:36 | 2024-11-17 17:48:36 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define rop(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define por(i,a,b) for(int i=(a);i>(b);i--)
using namespace std;
const int N=1e5+10;
int T,n,ls[N],rs[N],vis[N],sz[N],rt,msz;
struct node{
int nx,to;
}e[N<<1];
int h[N],idx;
void add(int x,int y){
e[idx].nx=h[x],e[idx].to=y,h[x]=idx++;
}
void init(){
rep(i,1,n) vis[i]=0,h[i]=-1;
rt=1,idx=0,msz=n;
}
void getsz(int x,int fa){
for(int i=h[x];~i;i=e[i].nx){
int y=e[i].to;
if(vis[y]||y==fa) continue;
getsz(y,x);
sz[x]+=sz[y];
}
}
int dfs(int x,int fa){
for(int i=h[x];~i;i=e[i].nx){
int y=e[i].to;
if(vis[y]||y==fa) continue;
if(sz[y]>msz/2) return dfs(y,x);
}
return x;
}
bool cmp(int a,int b){
return sz[a]>sz[b];
}
int dye(int x,int fa){
vis[x]=0;
int nsz=1;
for(int i=h[x];~i;i=e[i].nx){
int y=e[i].to;
if(y==fa) continue;
nsz+=dye(y,x);
}
return nsz;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
init();
rep(i,1,n){
scanf("%d%d",&ls[i],&rs[i]);
if(ls[i]) add(i,ls[i]),add(ls[i],i);
if(rs[i]) add(i,rs[i]),add(rs[i],i);
}
while(1){
rep(i,1,n) sz[i]=1;
getsz(rt,0);
rt=dfs(rt,0);
int s[4]={0,0,0,0},cnt=0;
for(int i=h[rt];~i;i=e[i].nx){
int y=e[i].to;
if(vis[y]) continue;
s[++cnt]=y;
}
sort(s+1,s+cnt+1,cmp);
// rep(i,1,n) cout<<"#"<<i<<' '<<sz[i]<<endl;
// cout<<"@"<<rt<<" "<<cnt<<endl;
if(cnt==0){
printf("! %d\n",rt);
fflush(stdout);
break;
}
if(cnt==1){
printf("? %d %d\n",rt,s[1]);
fflush(stdout);
int ans;
scanf("%d",&ans);
if(ans==0){
printf("! %d\n",rt);
fflush(stdout);
break;
}
if(ans==2){
printf("! %d\n",s[1]);
fflush(stdout);
break;
}
}
if(cnt==2){
printf("? %d %d\n",s[1],s[2]);
fflush(stdout);
int ans;
scanf("%d",&ans);
if(ans==1){
printf("! %d\n",rt);
fflush(stdout);
break;
}
rep(i,1,n) vis[i]=1;
if(ans==0){
msz=dye(s[1],rt);
rt=s[1];
}
if(ans==2){
msz=dye(s[2],rt);
rt=s[2];
}
}
if(cnt==3){
printf("? %d %d\n",s[1],s[2]);
fflush(stdout);
int ans;
scanf("%d",&ans);
rep(i,1,n) vis[i]=1;
if(ans==0){
msz=dye(s[1],rt);
rt=s[1];
}
if(ans==1){
msz=dye(s[3],rt)+1;
vis[rt]=0;
}
if(ans==2){
msz=dye(s[2],rt);
rt=s[2];
}
}
}
}
return 0;
}
/*
1
10
2 3
4 0
6 7
8 9
10 0
0 5
0 0
0 0
0 0
0 0
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5860kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 0 2 0 2 0 0 2
output:
? 1 3 ? 3 4 ! 3 ? 1 2 ! 2
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 2 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 2 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 1 0 5 4 5 3 1 0 0 0 0 0 0 1 0 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 2 0 5 5 0 0 0 0 0 3 0 2 4 1 2 3 3 0 1 0 0 0 1 2 2 0 0 0 2 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 1 8 ? 4 5 ? 4 3 ! 3 ? 2 7 ? 7 8 ? 8 6 ! 6 ? 8 3 ? 8 2 ? 6 4 ! 6 ? 2 5 ? 1 4 ! 1 ? 5 7 ? 4 1 ! 4 ? 1 5 ? 5 4 ! 5 ? 1 4 ? 5 2 ! 2 ? 2 3 ! 1 ? 1 2 ! 2 ? 3 2 ! 2 ? 1 7 ? 7 3 ? 6 4 ! 6 ? 1 2 ! 1 ? 9 5 ? 2 1 ? 2 6 ! 6 ? 3 10 ? 8 2 ? 5 7 ? 10 4