QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#753005#9570. Binary TreesyhyydsWA 1ms5740kbC++172.7kb2024-11-16 10:55:162024-11-16 10:55:17

Judging History

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

  • [2024-11-16 10:55:17]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5740kb
  • [2024-11-16 10:55:16]
  • 提交

answer

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <math.h>
#include <map>
#include <sstream>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <array>
#include <climits>
using namespace std;
#define   LL long long
#define ls o<<1
#define rs o<<1|1
#define PII pair<int,int>
#define PPI pair<pair<int,int>,int >
const int N = 2e5 + 100;
const LL  mod = 998244353;
const LL MAX = 1e18;
int w[N], le[N], ri[N];
int n, x, pa[N];
void dfs(int u)
{
	w[u] = 1;
	if (le[u])
	{
		dfs(le[u]);
		w[u] += w[le[u]];
	}
	if (ri[u])
	{
		dfs(ri[u]);
		w[u] += w[ri[u]];
	}
}
int seek(int u,int fa)
{
	if ( w[le[u]] > n/2)
		return seek(le[u],u);
	if ( w[ri[u]] > n/2)
		return seek(ri[u],u);
	if (le[u] && ri[u])
	{
		int t;
		if (w[le[u]] < n / 2) 
		{
			cout << "? " << ri[u] << " " <<pa<< endl;
			cin >> t;
			cout.flush();
			if (!t) return ri[u];
			if (t == 1) 
			{
				ri[u] = 0;
				return u;
			}
			le[u] = 0;
			ri[u] = 0;
			if (le[fa] == u) le[fa] = 0;
			else ri[fa] = 0;
			return x;
		}
		if (w[ri[u]] < n / 2)
		{      
			cout << "? " << le[u] << " " <<pa<< endl;
			cin >> t;
			cout.flush();
			if (!t) return le[u];
			if (t == 1) {
				le[u] = 0;
				return u;
			}
			le[u] = ri[u] = 0;
			if (le[fa] == u) le[fa] = 0;
			else ri[fa] = 0;
			return x;
		}
		cout << "? " << le[u] << " " << ri[u] << endl;
		cin >> t;
		cout.flush();
		if (!t) return le[u];
		if (t == 2) return ri[u];
		le[u] = 0;
		ri[u] = 0;
		return x;
	}
	if (le[u])
	{
		int t;
		cout << "? " << le[u] << " " << fa << endl;
		cin >> t;
		cout.flush();
		if (t == 1)
		{
			le[u] = 0;
			return u;
		}	
		if (!t) return le[u];
		if (le[fa] == u) le[fa] = 0;
		else ri[fa] = 0;
		le[u] = 0;
		return x;
	}
	int t;
	cout << "? " << ri[u] << " " << fa << endl;
	cin >> t;
	cout.flush();
	if (t == 1) 
	{
		ri[u] = 0;
		return u;
	}
	if (!t) return ri[u];
	ri[u] = 0;
	if (le[fa] == u) le[fa] = 0;
	else ri[fa] = 0;
	return x;
}
void solve()
{
	cin >> n;
	for (int i = 1; i <= n; i++) pa[i] = 0;
	for (int i = 1; i <= n; i++)
		cin>>le[i]>>ri[i], pa[le[i]] = i, pa[ri[i]] = i;
	x = 1;
	pa[0] = 0;
	while (pa[x] != 0) x = pa[x];
	while (1)
	{
		dfs(x);
		n = w[x];
		if (n == 1) break;
		int y = seek(x,x);
		x = y;
	}
	cout << "! " << x << endl;
}
int main()
{
	int T;
	cin >> T;
	while (T--)
		solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5740kb

input:

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

output:

? 5 0x562035501280

result:

wrong answer format  Expected integer, but "0x562035501280" found (test case 1)