QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#755625#9570. Binary TreeqiuuuML 0ms0kbC++171.4kb2024-11-16 17:46:532024-11-16 17:46:54

Judging History

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

  • [2024-11-16 17:46:54]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-11-16 17:46:53]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define PB push_back
const int N=100009;
int n,m,t,z1,z2,rt;
int sz[N];
vector<int> edge[N];
void dfs(int x,int fa)
{
	sz[x]=1;
	for(int i:edge[x])
	{
		if(i==fa) continue;
		dfs(i,x);
		sz[x]+=sz[i];
	}
}
void fd(int x,int fa)
{
	bool flg=1;
	for(int i:edge[x])
	{
		if(i==fa) continue;
		fd(i,x);
		if(sz[i]*2>sz[rt]) flg=0; 
	}
	if((sz[rt]-sz[x])*2>sz[rt]) flg=0;
	if(flg)
		if(z1) z2=x;
	else z1=x;
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=N-5;i++) edge[i].clear();
		for(int i=1,x,y;i<=n;i++)
		{
			scanf("%d%d",&x,&y);
			if(x) edge[x].PB(i),edge[i].PB(x);
			if(y) edge[y].PB(i),edge[i].PB(y);
		}
		rt=1;
		while(1)
		{
			dfs(rt,0);
			z1=z2=0;
			fd(rt,0);
			if(sz[rt]==1)
			{
				printf("! %d\n",rt);
				fflush(stdout);
				break;
			}
			printf("? %d %d\n",edge[z1].back(),edge[z1].end()[-2]);
			fflush(stdout);
			int x;
			scanf("%d",&x);
			if(x==0)
			{
				x=edge[z1].back();
				edge[z1].clear();
				edge[z1].PB(x);
				edge[x].erase(remove(edge[x].begin(),edge[x].end(),z1),edge[x].end());
				rt=x;
			}
			else if(x==2)
			{
				x=edge[z1].end()[-2];
				edge[z1].clear();
				edge[z1].PB(x);
				edge[x].erase(remove(edge[x].begin(),edge[x].end(),z1),edge[x].end());
				rt=x;
			}
			else
			{
				edge[z1].pop_back();
				edge[z1].pop_back();
				rt=z1;
			}
		}
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Memory Limit Exceeded

input:

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

output:

? 3 5
? 2 0

result: