QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#788443#9570. Binary TreeSZH#RE 2ms8964kbC++171.9kb2024-11-27 16:58:302024-11-27 16:58:31

Judging History

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

  • [2024-11-27 16:58:31]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:8964kb
  • [2024-11-27 16:58:30]
  • 提交

answer

#include<bits/stdc++.h>
#define pa pair<int,int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;

inline ll read()
{
	ll f=1,sum=0;char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-1;c=getchar();}
	while (isdigit(c)) {sum=sum*10+c-'0';c=getchar();}
	return sum*f;
}
const int MAXN=200010;
vector <int> g[MAXN];
int sz[MAXN],n,vis[MAXN],mxson[MAXN],mn,mnpos;
void get_n(int x,int f)
{
	n++;
	for (auto v:g[x]){
		if (v==f || vis[v]) continue;
		get_n(v,x);
	}
}
void dfs(int x,int f)
{
	mxson[x]=0;
	sz[x]=1;
	for (auto v:g[x])
	{
		if (v==f || vis[v]) continue;
		dfs(v,x);
		mxson[x]=max(mxson[x],sz[v]);
		sz[x]+=sz[v];
	}
	mxson[x]=max(mxson[x],n-sz[x]);
	if (mxson[x]<mn) mn=mxson[x],mnpos=x;
}
int main()
{
	int T;
	cin>>T;
	while (T--)
	{
		cin>>n;
		for (int i=1;i<=n;i++)
		{
			int v1,v2;
			cin>>v1>>v2;
			if (v1) g[i].push_back(v1),g[v1].push_back(i);
			if (v2) g[i].push_back(v2),g[v2].push_back(i);
		}
		int rt=1;
		int nn=n;
		while (1)
		{
			mn=INF;
			n=0;
			get_n(rt,0);
			// cout<<rt<<' '<<n<<endl;
			dfs(rt,0);
			// cout<<mnpos<<endl;
			vector <int> ql;
			for (auto v:g[mnpos])
			{
				if (vis[v]) continue;
				ql.push_back(v);
			}
			if (ql.size()==0)
			{
				cout<<"! "<<mnpos<<endl;
				break;
			}
			else if (ql.size()==1)
			{
				cout<<"? "<<mnpos<<' '<<ql[0]<<endl;
				int ret;
				cin>>ret;
				cout<<"! "<<((ret==0)?mnpos:ql[0])<<endl;
				break;
			}
			else
			{
				cout<<"? "<<ql[0]<<' '<<ql[1]<<endl;
				int ret;
				cin>>ret;
				if (!ret) vis[mnpos]=1,rt=ql[0];
				else if (ret==2) vis[mnpos]=1,rt=ql[1];
				else if (ret==1) vis[ql[0]]=vis[ql[1]]=1,rt=mnpos;
			}
		}
		for (int i=1;i<=nn;i++) g[i].clear(),vis[i]=0;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8964kb

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Runtime Error

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

result: