QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#751941#9570. Binary Treeasuna241631RE 0ms0kbC++142.2kb2024-11-15 21:21:232024-11-15 21:21:24

Judging History

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

  • [2024-11-15 21:21:24]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-15 21:21:23]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10,inf=1e9;
int e[N],h[N],nxt[N],d[N],n,cnt=1,rt,siz[N],mx[N],Tsiz;
void add(int x,int y){
	cnt++;
	e[cnt]=y;
	d[y]++;
	nxt[cnt]=h[x];
	h[x]=cnt;
}
void dfs(int x,int f){
	siz[x]=1;mx[x]=0;
	for(int i=h[x];i;i=nxt[i]){
		int y=e[i];
		if(y==f||y==0) continue;
		dfs(y,x);
		mx[x]=max(mx[x],siz[y]);
		siz[x]+=siz[y];
	}
	mx[x]=max(mx[x],Tsiz-siz[x]);
	if(mx[x]<mx[rt]) rt=x;
}
void work(int x,int f){
	siz[x]=1;
	for(int i=h[x];i;i=nxt[i]){
		int y=e[i];
		if(y==f||y==0) continue;
		work(y,x);
		siz[x]+=siz[y];
	}
}
int ask(int x,int y){
	cout<<"?"<<" "<<x<<" "<<y<<endl;
	int nnn;
	cin>>nnn;
	cout<<nnn<<endl;
	return nnn;
}
bool cmp(int x,int y){
	return siz[x]<siz[y];
}
void found(int x,int s){
	rt=0,mx[rt]=inf;Tsiz=s;
//	cout<<x<<" "<<s<<endl;
	dfs(x,0);
//	cout<<x<<" "<<rt<<endl;
	if(d[rt]==1){
//		puts("one");
//		cout<<rt<<endl;
		int y=e[h[rt]],ans=ask(rt,y);
		if(ans==0) cout<<"! "<<rt<<endl;
		else cout<<"! "<<y<<endl;
		return ;
	}else if(d[rt]==2){
//		puts("two");
		int son[3],ct=0;
		for(int i=h[rt];i;i=nxt[i]){
			int y=e[i];
			if(y==0) continue;
			e[i^1]=0,d[y]--;
			son[++ct]=y;
		}
		work(rt,0);
		int ans=ask(son[1],son[2]);
		if(ans==1) {
			cout<<"! "<<rt<<endl;
			return ;
		}else if(ans==0){
			found(son[1],siz[son[1]]);
		}else {
			found(son[2],siz[son[2]]);
		}
	}else {
//		puts("three");
		int son[4],ct=0;
		for(int i=h[rt];i;i=nxt[i]){
			int y=e[i];
			if(y==0) continue;
			e[i^1]=0,d[y]--;
			son[++ct]=y;
		}
		work(rt,0);
		sort(son+1,son+4,cmp);
		int ans=ask(son[2],son[3]);
		if(ans==0){
			found(son[2],siz[son[2]]);
		}else if(ans==1){
			h[rt]=0,d[rt]=0;
			add(rt,son[1]);add(son[1],rt);
			found(son[1],siz[son[1]]+1);
		}else {
			found(son[3],siz[son[3]]);
		}
	}
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++) d[i]=h[i]=0;cnt=1;
		for(int i=1;i<=n;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			if(x!=0) add(i,x),add(x,i);
			if(y!=0) add(i,y),add(y,i);
		}
		found(1,n);
		for(int i=1;i<=cnt;i++) e[i]=nxt[i]=0;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

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

output:

? 1 3
2
? 4 3

result: