QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#758243#9570. Binary TreewyhaoWA 13ms4072kbC++142.3kb2024-11-17 17:04:482024-11-17 17:04:48

Judging History

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

  • [2024-11-17 17:04:48]
  • 评测
  • 测评结果:WA
  • 用时:13ms
  • 内存:4072kb
  • [2024-11-17 17:04:48]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int n;
int ch[N][2],fa[N];
int rt,u,v,G,Gmin,nn;
int siz[N];
bool vis[N];
vector<int> vec;
void dfs(int x,int p){
	siz[x]=1;
	int Gmax = 0;
	if(!vis[fa[x]] and fa[x]!=p){
		dfs(fa[x],x);
		siz[x]+=siz[fa[x]];
		Gmax=max(Gmax,siz[fa[x]]);
	}
	if(!vis[ch[x][0]] and ch[x][0]!=p){
		dfs(ch[x][0],x);
		siz[x]+=siz[ch[x][0]];
		Gmax=max(Gmax,siz[ch[x][0]]);
	}
	if(!vis[ch[x][1]] and ch[x][1]!=p){
		dfs(ch[x][1],x);
		siz[x]+=siz[ch[x][1]];
		Gmax=max(Gmax,siz[ch[x][1]]);
	}
	Gmax = max(Gmax,nn-siz[x]);
	if(Gmax < Gmin){
		Gmin = Gmax;
		G = x;
	}
	vec.push_back(x);
}
bool fnd(){
	nn=0;
	for(auto p:vec){
		if(!vis[p]) nn++;
	}
	G=rt;Gmin=0x3f3f3f3f;
	vec.clear();
	dfs(rt,0);
	if(Gmin == 0) return false;
	return true;
}
void idfs(int x,int p){
	vis[x]=true;
	if(!vis[fa[x]] and fa[x]!=p){
		idfs(fa[x],x);
	}
	if(!vis[ch[x][0]] and ch[x][0]!=p){
		idfs(ch[x][0],x);
	}
	if(!vis[ch[x][1]] and ch[x][1]!=p){
		idfs(ch[x][1],x);
	}
}
void solve(){
	scanf("%d",&n);	
	for(int i=1;i<=n;i++) fa[i]=0;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&ch[i][0],&ch[i][1]);
		fa[ch[i][0]]=i;
		fa[ch[i][1]]=i;
		vis[i]=false;
		vec.push_back(i);
	}
	vis[0]=true;
	rt = 1;
	int s;
	vector<int>vv;
	while(fnd()){
		fflush(stdout);
		s=0;
		vv.clear();
		if(!vis[fa[G]]) vv.push_back(fa[G]);
		if(!vis[ch[G][0]]) vv.push_back(ch[G][0]);
		if(!vis[ch[G][1]]) vv.push_back(ch[G][1]);
		if(vv.size()==1){
			u = vv[0];
			printf("? %d %d\n",G,u);
			fflush(stdout);
			scanf("%d",&s);
			if(s==0){
				break;
			}else{
				G = u;
				break;
			}
		}else if(vv.size()==2){
			u = vv[0];
			v = vv[1];
			printf("? %d %d\n",u,v);
			fflush(stdout);
			scanf("%d",&s);
			if(s==1){
				break;
			}
			vis[G]=true;
			if(s==0){
				idfs(v,G);
				rt = u;
			}else{
				idfs(u,G);
				rt = v;
			}
		}else{
			u = vv[0];
			v = vv[1];
			printf("? %d %d\n",u,v);
			fflush(stdout);
			scanf("%d",&s);
			if(s==1){
				idfs(u,G);
				idfs(v,G);
				rt = G;
			}else if(s==0){
				idfs(G,u);
				rt = u;
			}else{
				idfs(G,v);
				rt = v;
			}
		}
	}
	printf("! %d\n",G);
	fflush(stdout);
}
int main(){
	int tests;
	scanf("%d",&tests);
	while(tests--){
		solve();
	}
	return 0;
} 

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 4072kb

input:

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

output:

? 3 1
? 5 2
! 5
? 1 2
! 2

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 13ms
memory: 3788kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
0
0
2
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
0
0
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
1
1
0
5
4 5
3 1
0 0
0 0
0 0
1
0
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
1
0
5
3 0
5 1
0 0
0 0
4 0
2
0
5
5 0
0 0
0 0
3 0
2 4
1
2
3
3 0
1 0
0 0
1
2
2 0
0 0
2
3
2 3
0 0
0 0
2
10
2 8
9 7
0 0
...

output:

? 2 5
? 2 7
? 1 2
! 2
? 7 2
? 5 6
? 7 5
! 7
? 3 5
? 8 4
? 2 6
! 2
? 2 4
? 5 1
! 5
? 7 5
? 3 2
! 3
? 5 1
? 3 1
! 3
? 1 2
? 5 3
! 3
? 2 3
! 1
? 1 2
! 2
? 2 3
! 3
? 1 9
? 7 4
? 2 5
! 7
? 1 2
! 1
? 7 2
? 5 4
? 7 5
! 5
? 3 10
? 8 6
? 10 8
! 10
? 8 2
? 8 6
? 5 3
! 3
? 1 2
! 2
? 7 5
? 5 2
? 6 5

result:

wrong answer Too many queries (test case 17)