QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#751945#9570. Binary Treewhileone27WA 1ms11792kbC++141.6kb2024-11-15 21:22:212024-11-15 21:22:21

Judging History

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

  • [2024-11-15 21:22:21]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:11792kb
  • [2024-11-15 21:22:21]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2e6+5;
int n,T,siz[N],maxx[N],dui[4],top;
int bb[N];
int v[N],h[N],nt[N],cnt;
void add(int x,int y){
	v[++cnt]=y;
	nt[cnt]=h[x];
	h[x]=cnt;
}
void dfs(int x,int fa){
	siz[x]=1;
	for(int i=h[x];i;i=nt[i])
	if(v[i]!=fa&&!bb[v[i]]){
		dfs(v[i],x);
		siz[x]+=siz[v[i]];
		maxx[x]=max(maxx[x],siz[v[i]]);
	}
}
int work(int x){
	for(int i=1;i<=n;++i)maxx[i]=siz[i]=0;
	int rt=0,xx;
	int minn=N;
	dfs(x,0);
	for(int i=1;i<=n;++i)
	if(siz[i]&&max(maxx[i],siz[x]-siz[i])<minn)minn=max(maxx[i],siz[x]-siz[i]),rt=i;
	top=0;
	for(int i=h[rt];i;i=nt[i])
	if(!bb[v[i]])
	dui[++top]=v[i];//cout<<rt<<"FSKJ\n";
	x=rt;
	if(!top)return x;
	if(top==1){
		cout<<"? "<<x<<' '<<dui[1]<<endl;
		cin>>xx;
		if(xx==0)return x;
		return dui[1];
	}
	if(top==2){
		cout<<"? "<<dui[1]<<' '<<dui[2]<<endl;
		cin>>xx;
		if(xx==1)return x;
		bb[x]=1;
		if(xx==0)return work(dui[1]);
		return work(dui[2]);
	}
	if(top==3){
		if(siz[dui[3]]>siz[dui[1]])swap(dui[3],dui[1]);
		if(siz[dui[3]]>siz[dui[2]])swap(dui[3],dui[2]);
		cout<<"? "<<dui[1]<<' '<<dui[2]<<endl;
//		cout<<dui[3]<<"NCNMASFKLJF\n";
		cin>>xx;
		if(xx==1){
			bb[dui[1]]=1;
			bb[dui[2]]=1;
			return work(x);
		}
		bb[x]=1;
		if(xx==0)return work(dui[1]);
		return work(dui[2]);
	}
	
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>T;
	while(T--){
		cin>>n;
		cnt=0;
		int x,y;
		for(int i=1;i<=n;++i)h[i]=0;
		for(int i=1;i<=n;++i){
			cin>>x>>y;
			if(x)add(i,x),add(x,i);
			if(y)add(i,y),add(y,i);
		}
		int ans=work(1);
		cout<<"! "<<ans<<endl;
	}
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 11792kb

input:

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

output:

? 1 3
? 3 4
! 3
! 1

result:

wrong answer There are 2 candidates. (test case 2)