QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#747243#9570. Binary TreeATM12345#WA 4ms5700kbC++172.4kb2024-11-14 16:40:562024-11-14 16:40:57

Judging History

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

  • [2024-11-14 16:40:57]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:5700kb
  • [2024-11-14 16:40:56]
  • 提交

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], f[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];
}

void sol() {
	int n;
	cin >> n;
	for (int x = 1; x <= n; x++) {
		f[x] = 0;
	}
	for (int x = 1; x <= n; x++) {
		cin >> l[x] >> r[x];
		f[l[x]] = x;
		f[r[x]] = x;
	}
	int init_root;
	for (int x = 1; x <= n; x++) {
		if (!f[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];
						f[x] = 0;
					} else if (t == 2) {
						x = r[x];
						f[x] = 0;
					} else {
						l[x] = 0;
						r[x] = 0;
						x = root;
					}
				} else if (l[x]) {
					if (f[x]) {
						int t = ask(l[x], f[x]);
						if (t == 0) {
							x = l[x];
							f[x] = 0;
						} else if (t == 2) {
							if (l[f[x]] == x) {
								l[f[x]] = 0;
							} else {
								r[f[x]] = 0;
							}
							x = root;
						} else {
							l[x] = 0;
							f[x] = 0;
						}
					} else {
						int t = ask(l[x], x);
						if (t == 0) {
							x = l[x];
							f[x] = 0;
						} else {
							l[x] = 0;
							x = root;
						}
					}
				} else if (r[x]) {
					if (f[x]) {
						int t = ask(r[x], f[x]);
						if (t == 0) {
							x = r[x];
							f[x] = 0;
						} else if (t == 2) {
							if (r[f[x]] == x) {
								r[f[x]] = 0;
							} else {
								l[f[x]] = 0;
							}
							x = root;
						} else {
							r[x] = 0;
							f[x] = 0;
						}
					} else {
						int t = ask(r[x], x);
						if (t == 0) {
							x = r[x];
							f[x] = 0;
						} 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: 5688kb

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: 4ms
memory: 5700kb

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

output:

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

result:

wrong answer Too many queries (test case 17)