QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#752046#9570. Binary Treedog207RE 0ms0kbC++141.8kb2024-11-15 21:47:182024-11-15 21:47:19

Judging History

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

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

answer

#include<bits/stdc++.h>
#define mk make_pair
#define X first
#define Y second
#define pb push_back
using namespace std;
const int N=2e5+5;
int n,T,op;
vector<int> G[N];
int vis[N],maxnp,f[N],siz[N],maxn[N];
void dfs(int x,int fa){
	f[x]=fa;
	siz[x]=1;
	maxn[x]=0;
	for(int y:G[x]){
		if(!vis[y]&&y!=fa){
			dfs(y,x);
			siz[x]+=siz[y];
			maxn[x]=max(maxn[x],siz[y]);
		}
	}
	maxn[x]=max(n-siz[x],maxn[x]);
	if(!maxnp||maxn[maxnp]>maxn[x]){
		maxnp=x;
	}
}
int work(int x){
	int d=0;
	for(int y:G[x]){
		if(!vis[y]){
			++d;
		}
	}
	if(d==0){
		return x;
	}
	maxnp=0;
	dfs(x,0);
	d=0;
	int g[4];
	for(int y:G[maxnp])
		if(!vis[y]){
			g[++d]=y;
			if(y==f[maxnp]){
				siz[y]=n-siz[maxnp];
			}
		}
	if(d>=3)
	if(siz[g[3]]>siz[g[2]])swap(g[2],g[3]);
	if(siz[g[2]]>siz[g[1]])swap(g[2],g[1]);
	if(d>=3)
	if(siz[g[3]]>siz[g[2]])swap(g[2],g[3]);
	if(d==1){
		cout<<"? "<<maxnp<<" "<<g[1]<<endl;
		cin>>op;
		vis[maxnp]=1;
		if(!op){
			return maxnp;
		}else{
			return g[1];
		}
	}
	if(d==2){
		cout<<"? "<<g[1]<<" "<<g[2]<<endl;
		cin>>op;
		vis[maxnp]=1;
		if(op==1){
			return maxnp;
		}else{
			if(!op){
				n=siz[g[1]];
				return work(g[1]);
			}else{
				n=siz[g[2]];
				return work(g[2]);
			}
		}
	}
	cout<<"? "<<g[1]<<" "<<g[2]<<endl;
	cin>>op;
	if(op==1){
		vis[g[1]]=vis[g[2]]=1;
		n=siz[g[3]]+1;
		return work(maxnp);
	}
	vis[maxnp]=1;
	if(op==0){
		n=siz[g[1]];
		return work(g[1]);
	}
	n=siz[g[2]];
	return work(g[2]);
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>T;
	while(T--){
		cin>>n;
		for(int i=1;i<=n;++i)G[i].clear();
		for(int i=1,x,y;i<=n;++i){
			cin>>x>>y;
			vis[i]=0;
			if(x)
			G[i].pb(x),G[x].pb(i);
			if(y)
			G[i].pb(y),G[y].pb(i);
		}
		int x=work(1);
		cout<<"! "<<x<<endl;
	}
}

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
1
0
2
0 2
0 0

output:

? 3 1
? 5 2
! 5

result: