QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#747057#9570. Binary TreeATM12345#WA 0ms5652kbC++171.7kb2024-11-14 16:15:342024-11-14 16:15:35

Judging History

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

  • [2024-11-14 16:15:35]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:5652kb
  • [2024-11-14 16:15:34]
  • 提交

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;
}

void 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 n;
bool vis[MAX];
void sol() {
	
	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);
			return;
		}
		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: 100
Accepted
time: 0ms
memory: 3628kb

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
Wrong Answer
time: 0ms
memory: 5652kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
0
2
0
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
2
2
2
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
2
1
0
5
4 5
3 1
0 0
0 0
0 0
1
0
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
0
1
5
3 0
5 1
0 0
0 0
4 0
0
2
5
5 0
0 0
0 0
3 0
2 4
2
0
3
3 0
1 0
0 0
2

output:

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

result:

wrong answer Too many queries (test case 8)