QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#735566#9570. Binary TreeUESTC_NLNS#WA 13ms3940kbC++202.0kb2024-11-11 20:45:242024-11-11 20:45:24

Judging History

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

  • [2024-11-11 20:45:24]
  • 评测
  • 测评结果:WA
  • 用时:13ms
  • 内存:3940kb
  • [2024-11-11 20:45:24]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,INF=0x3f3f3f3f;
int T,n,siz[N],son,SIZ;
bool u[N];
vector<int> s,p,e[N];
void Dfs(int x,int fa)
{
	siz[x]=1;int mx=0;s.push_back(x);
	for(auto v:e[x]) if(v!=fa&&!u[v]) Dfs(v,x),siz[x]+=siz[v];
	return;
}
void Dfss(int x,int fa)
{
	int mx=0;
	for(auto v:e[x]) if(v!=fa&&!u[v]) Dfss(v,x),mx=max(mx,siz[v]);
	if(fa) mx=max(mx,(int)s.size()-siz[x]);
	if(mx<SIZ) SIZ=mx,son=x;
	return;
}
void answer(int x)
{
	printf("! %d\n",x);
	fflush(stdout);
	return;
}
int query(int x,int y)
{
	int res;
	printf("? %d %d\n",x,y);
	fflush(stdout);
	scanf("%d",&res);
	return res;
}
bool cmp(int x,int y)
{
	return siz[x]<siz[y];
}
void solve(int x)
{
	s.clear();
	Dfs(x,0);
	if(s.size()==1) {answer(s[0]);return;}
	else if(s.size()==2)
	{
		int q=query(s[0],s[1]);
		if(q==0) answer(s[0]);
		else answer(s[1]);
		return;
	}
	else if(s.size()==3)
	{
		sort(s.begin(),s.end(),cmp);
		if(siz[s[1]]==2)
		{
			int q=query(s[0],s[2]);
			if(q==0) answer(s[0]);
			else if(q==2) answer(s[2]);
			else answer(s[1]);
		}
		else
		{
			int q=query(s[0],s[1]);
			if(q==0) answer(s[0]);
			else if(q==2) answer(s[1]);
			else answer(s[2]); 
		}
		return;
	}
	SIZ=INF;
	Dfss(x,0);
	p.clear();
	for(auto v:e[son]) if(!u[v]) p.push_back(v);
	if(p.size()==2)
	{
		int q=query(p[0],p[1]);
		if(q==0) {u[son]=true;solve(p[0]);}
		else if(q==2) {u[son]=true;solve(p[1]);}
		else answer(son);
	}
	else
	{
		int q=query(p[0],p[2]);
		if(q==0) {u[son]=true;solve(p[0]);}
		else if(q==2) {u[son]=true;solve(p[2]);}
		else {u[p[0]]=u[p[2]]=true;solve(son);}
	}
	return;
}
int a,b;
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&a,&b);u[i]=false;
			if(a) e[i].push_back(a),e[a].push_back(i); 
			if(b) e[i].push_back(b),e[b].push_back(i);
		}
		solve(1);
		for(int i=1;i<=n;i++) e[i].clear(); 
	}
	return 0;
}
/*
2
5
0 0
1 5
2 4
0 0
0 0
2
0 2
0 0
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

? 1 3
? 3 4
! 3
? 1 2
! 2

result:

ok OK (2 test cases)

Test #2:

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

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
2
1
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
0
0
2
5
4 5
3 1
0 0
0 0
0 0
1
2
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
0
0
5
3 0
5 1
0 0
0 0
4 0
0
2
5
5 0
0 0
0 0
3 0
2 4
1
0
3
3 0
1 0
0 0
2
2
2 0
0 0
0
3
2 3
0 0
0 0
2
10
2 8
9 7
0 0
...

output:

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

result:

wrong answer Too many queries (test case 33)