QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#724699#9570. Binary Tree552HzRE 1ms3616kbC++202.3kb2024-11-08 14:34:132024-11-08 14:34:13

Judging History

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

  • [2024-11-08 14:34:13]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3616kb
  • [2024-11-08 14:34:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
void solve(){
	int n;
	cin>>n;
	vector<int>e[2 * n+1];
	for(int i =1;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		if(a != 0)
		{
			e[i].push_back(a);
			e[a].push_back(i);
		}
		if(b != 0)
		{
			e[i].push_back(b);
			e[b].push_back(i);
		}
	}

	auto ask = [&](int u,int v)
	{
		cout<<"? "<<u<<" "<<v<<endl;
		int res;
		cin>>res;
		return res;
	};

	auto output = [&](int ans)
	{
		cout<<"! "<<ans<<endl;
	};
	vector<int>sz(2 * n+1,1),del(2 * n + 1);
	int pos = 0,cur = 0;
	function<void(int,int)>dfs = [&](int u,int fa)
	{
		int res = 0;
		for(auto v:e[u])
		{
			if(v == fa || del[v])
				continue;
			dfs(v,u);
			sz[u] += sz[v];
			res = max(res,sz[v]);
		}
		res = max(res,cur - sz[u]);
		if(res <= cur / 2)
			pos = u;
	};
	function<void(int,int)>det = [&](int u,int fa)
	{
		del[u] = 1;
		for(auto v:e[u])
		{
			if(v == fa || del[v])
				continue;
			det(v,u);
		}
	};
	int id = 1;
	for(int query_times = -1;(1 << query_times) <= n;query_times ++)
	{
		cur = count(del.begin()+1,del.begin() + 1 + n,0);
		if(cur == 0)
			return void(output(pos));
		while(id <= n && del[id])
			id++;
		if(id > n)
			break;
		dfs(id,0);
		vector<int>son;
		if(pos <= 0 || pos > n)
			break;
		for(auto v:e[pos])
		{
			if(!del[v])
				son.push_back(v);
		}
		if(son.size() == 3)
		{
			int op = ask(son[0],son[1]);
			if(op == 0)
			{
				det(son[1],pos);
				det(son[2],pos);
				if(pos < 0 || pos > 2 * n)
					break;
				del[pos] = 1;
			}
			if(op == 1)
			{
				det(son[0],pos);
				det(son[1],pos);
			}
			if(op == 2)
			{
				det(son[0],pos);
				det(son[2],pos);
				if(pos < 0 || pos > 2 * n)
					break;
				del[pos] = 1;
			}
		}
		if(son.size() == 2)
		{

			int op = ask(son[0],son[1]);
			if(pos < 0 || pos > 2 * n)
				break;
			del[pos] = 1;
			if(op == 0)
				det(son[1],pos);
			if(op == 2)
				det(son[0],pos);
			if(op == 1)
				return void(output(pos));
		}
		if(son.size() == 1)
		{
			int op = ask(son[0],pos);
			if(op == 0)
				return void(output(son[0]));
			if(op == 2)
				return void(output(pos));
		}
		if(son.empty())
			return void(output(pos));
	}
	output(1);
}
signed main(){
	int t;
	cin>>t;
	while(t--){
		solve();
	}
}

详细

Test #1:

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

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

output:

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

result: