QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#752133#9570. Binary Treeasuna241631RE 0ms0kbC++142.4kb2024-11-15 22:08:422024-11-15 22:08:43

Judging History

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

  • [2024-11-15 22:08:43]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-15 22:08:42]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=6e5+10,inf=1e9;
int e[N],h[N],nxt[N],d[N],n,cnt=1,rt,siz[N],mx[N],Tsiz,vis[N];
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||vis[y]) 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||vis[y])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<<" "<<d[rt]<<endl;
	if(d[rt]==0) cout<<"! "<<rt<<endl;
	else if(d[rt]==1){
//		puts("one");
//		cout<<rt<<endl;
		int y;
		for(int i=h[rt];i;i=nxt[i]){
			int yy=e[i];
			if(y==0||vis[y]) continue;
			y=yy;
		}
		int ans=ask(rt,y);
		if(ans==0) cout<<"! "<<rt<<endl;
		else cout<<"! "<<y<<endl;
		return ;
	}else if(d[rt]==2){
//		puts("two");
		int son[10],ct=0;
		for(int i=h[rt];i;i=nxt[i]){
			int y=e[i];
			if(y==0||vis[y]) continue;
		    d[y]--;
			son[++ct]=y;
		}
		work(rt,0);
		int ans=ask(son[1],son[2]);
		vis[rt]=1;
		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[10],ct=0;
		son[0]=son[1]=son[2]=son[3]=0;
		for(int i=h[rt];i;i=nxt[i]){
			int y=e[i];
			if(y==0||vis[y]) continue;
			d[y]--;
			son[++ct]=y;
		}
		work(rt,0);
		sort(son+1,son+4,cmp);
		int ans=ask(son[2],son[3]);
		if(ans==0){
			vis[rt]=1;
			found(son[2],siz[son[2]]);
		}else if(ans==1){
			d[son[1]]++,d[rt]=1;vis[son[2]]=vis[son[3]]=1;
			found(son[1],siz[son[1]]+1);
		}else {
			vis[rt]=1;
			found(son[3],siz[son[3]]);
		}
	}
	return ;
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++) vis[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;
}
/*
1
3
3 0
1 0
0 0
*/

详细

Test #1:

score: 0
Runtime Error

input:

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

output:

? 1 3

result: