QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#772403#9570. Binary TreeF7487TL 0ms0kbC++142.3kb2024-11-22 19:22:282024-11-22 19:22:29

Judging History

你现在查看的是最新测评结果

  • [2024-11-22 19:22:29]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-22 19:22:28]
  • 提交

answer

#include<bits/stdc++.h>
#define maxn 400005
using namespace std;
int qread(){
	int s=0;char a=getchar();
	while(!isdigit(a))a=getchar();
	while(isdigit(a)){
		s=(s<<1)+(s<<3)+a-'0';
		a=getchar();
	}
	return s;
}
int cnt,head[maxn],siz[maxn],tot,son[5];
bool vis[maxn];
struct xs{
	int nxt,to;
}e[maxn];
void addn(int x,int y){
	e[++cnt].nxt=head[x];head[x]=cnt;
	e[cnt].to=y;return;
}
void dfs_ini(int x,int f){
	int i,v;
	siz[x]=1;tot++;
	for(i=head[x];i;i=e[i].nxt){
		v=e[i].to;
		if(vis[v])continue;
		if(v==f)continue;
		else{
			dfs_ini(v,x);siz[x]+=siz[v];
		}
	}
	return;
}
int pos;
void dfs_core(int x,int f){
	int i,v;bool bl=1;
	for(i=head[x];i;i=e[i].nxt){
		v=e[i].to;
		if(vis[v])continue;
		if(v==f)continue;
		if(siz[v]>tot/2){
			bl=0;dfs_core(v,x);
		}
		if(pos)break;
	}
	if(bl)pos=x;
	return;	
}
int main(){
	int T=qread(),I,n,x,y,p,v,cts,ans,st,i,j=0;
	for(I=1;I<=T;I++){
	    n=qread();cnt=0;ans=0;
	    for(i=1;i<=n;i++){
	    	head[i]=siz[i]=vis[i]=0;
	    }
		for(i=1;i<=n;i++){
			x=qread();y=qread();
			if(x){
				addn(i,x);addn(x,i);
			}
			if(y){
				addn(i,y);addn(y,i);
			}
		}	
		pos=1;j=0;
		while(1){
			j++;
			p=pos;pos=0;tot=0;
			dfs_ini(p,0);dfs_core(p,0);cts=0;
			for(i=head[pos];i;i=e[i].nxt){
				v=e[i].to;if(vis[v])continue;
				son[++cts]=v;
			}
		    if(cts==3){
		    	cout<<"? "<<son[1]<<" "<<son[2]<<'\n';cout.flush();
		    	st=qread();if(st==0){
		    		vis[pos]=1;pos=son[1];
		    	}
		    	else{
		    		if(st==1){
		    			vis[pos]=1;pos=son[3];
		    		}
		    		else{
		    			vis[pos]=1;pos=son[2];
		    		}
		    	}		    				    	
		    }
		    else{
		    	if(cts==2){
		    		cout<<"? "<<son[1]<<" "<<son[2]<<'\n';cout.flush();
		    		st=qread();if(st==0){
		    			vis[pos]=1;pos=son[1];
		    		}
		    		else{
		    			if(st==1){
		    				ans=pos;break;
		    			}
		    			else{
		    				vis[pos]=1;pos=son[2];
		    			}
		    		}		    		
		    	}
		    	else{
		    		if(cts==1){
		    			cout<<"? "<<pos<<" "<<son[1]<<'\n';cout.flush();
		    			st=qread();if(st==0){
		    				ans=pos;break;
		    			}
		    			else{
		    				ans=son[1];break;
		    			}
		    		}
		    		else{
		    			ans=pos;break;
		    		}
		    	}
		    }
		    if(j>100000)break;
		}
		cout<<"! "<<ans<<'\n';
	}
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

2
5
0 0
1 5
2 4
0 0
0 0
1

output:

? 3 5

result: