QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#746778#9570. Binary TreeATM12345#ML 0ms0kbC++171.7kb2024-11-14 15:31:292024-11-14 15:31:30

Judging History

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

  • [2024-11-14 15:31:30]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-11-14 15:31:29]
  • 提交

answer

#include <bits/stdc++.h>
//#define int long long
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
using namespace std;
const int MAX = 2e5+5;
#define ff first
#define ss second

int ask(int u, int v) {
	cout << "? " << u << ' ' << v << endl;
	int x;
	cin >> x;
	return x;
}

int res(int u) {
	cout << "! " << u << endl;
}

int l[MAX], r[MAX];

int s[MAX];
int build(int x) {
	s[x] = 1;
	if (l[x]) {
		s[x] += build(l[x]);
	}
	if (r[x]) {
		s[x] += build(r[x]);
	}
	return s[x];
}

int vis[MAX];
void sol() {
	int n;
	cin >> n;
	for (int x = 1; x <= n; x++) {
		vis[x] = false;
	}
	for (int x = 1; x <= n; x++) {
		cin >> l[x] >> r[x];
		vis[l[x]] = true;
		vis[r[x]] = true;
	}
	int init_root;
	for (int x = 1; x <= n; x++) {
		if (!vis[x]) {
			init_root = x;
			break;
		}
	}
	
	int x = init_root;
	while (true) {
		int root = x;
		int k = build(x);
		if (k == 1) {
			res(x);
			break;
		}
		while (true) {
			if (l[x] && s[l[x]] * 2 > k) {
				x = l[x];
			} else if (r[x] && s[r[x]] * 2 > k) {
				x = r[x];
			} else {
				if (l[x] && r[x]) {
					int t = ask(l[x], r[x]);
					if (t == 0) {
						x = l[x];
					} else if (t == 2) {
						x = r[x];
					} else {
						l[x] = 0;
						r[x] = 0;
						x = root;
					}
				} else if (l[x]) {
					int t = ask(l[x], x);
					if (t == 0) {
						x = l[x];
					} else {
						l[x] = 0;
						x = root;
					}
				} else if (r[x]) {
					int t = ask(r[x], x);
					if (t == 0) {
						x = r[x];
					} else {
						r[x] = 0;
						x = root;
					}
				}
				break;
			}
		}
	}
}


signed main()
{
	//IOS
	int tt=1;
	cin>>tt;
	while (tt--)
		sol();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Interactor Memory Limit Exceeded

input:

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

output:

? 1 5
? 2 4
! 3




























































































































































































































































































...

result: