QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#811818#9570. Binary TreeInsert_Username_HereWA 3ms3660kbC++202.2kb2024-12-13 02:44:272024-12-13 02:44:28

Judging History

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

  • [2024-12-13 02:44:28]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3660kb
  • [2024-12-13 02:44:27]
  • 提交

answer

#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
// const ll mod = 1e9 + 7;
// #include <brawlstars>
// FOR PAIN OR FOR GLORYYY ELLL PRIMOOOOOO

const int N = 1e5 + 1;
pii ch[N];
int sz[N], p[N];
int dfs(int i) {
	if(i < 0) return 0;
	sz[i] = 1 + dfs(ch[i].f) + dfs(ch[i].s);
	return sz[i];
}
int qry(int i, int j) {
	cout << "? " << i + 1 << " " << j + 1 << "\n";
	cout.flush();
	int ans;
	cin >> ans;
	return ans;
}
int bk(int i, int r) {
	if(ch[i].f < 0 && ch[i].s < 0 && i == r) return i;
	if(ch[i].f < 0 || ch[i].s < 0) {
		if(ch[i].f < 0) swap(ch[i].f, ch[i].s);
		if(sz[r] - sz[ch[i].f] < sz[ch[i].f]) return bk(ch[i].f, r);
		if(qry(i, ch[i].f)) return bk(ch[i].f, ch[i].f);
		int u = i;
		while(p[u] >= 0) sz[u] -= sz[ch[i].f], u = p[u];
		sz[u] -= sz[ch[i].f], ch[i].f = -1;
		return bk(r, r);
	}
	if(sz[ch[i].f] < sz[ch[i].s]) swap(ch[i].f, ch[i].s);
	int num = ceil(sz[r] / 3.0);
	if(sz[r] - sz[i] + 1 < num && sz[ch[i].s] < num) return bk(ch[i].f, r);
	int ans = qry(ch[i].f, ch[i].s);
	if(ans == 0) return bk(ch[i].f, ch[i].f);
	if(ans == 2) return bk(ch[i].s, ch[i].s);
	int u = i;
	while(p[u] >= 0) sz[u] -= sz[ch[i].f] + sz[ch[i].s], u = p[u];
	sz[u] -= sz[ch[i].f] + sz[ch[i].s], ch[i] = {-1, -1};
	return bk(r, r);
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		for(int i = 0; i < n; i++) p[i] = -1;
		for(int i = 0; i < n; i++) {
			cin >> ch[i].f >> ch[i].s;
			ch[i].f--, ch[i].s--;
			if(ch[i].f >= 0) p[ch[i].f] = i;
			if(ch[i].s >= 0) p[ch[i].s] = i;
		}
		int r = 0;
		while(p[r] >= 0) r = p[r];
		dfs(r);
		if(n == 3) {
			int c1, c2, c3;
			if(ch[r].f >= 0 && ch[r].s >= 0) c1 = ch[r].f, c2 = r, c3 = ch[r].s;
			else c1 = r, c2 = (ch[r].f >= 0 ? ch[r].f : ch[r].s), c3 = (ch[c2].f >= 0 ? ch[c2].f : ch[c2].s);
			int ans = qry(c1, c3);
			if(ans == 0) cout << "! " << c1 + 1 << "\n";
			if(ans == 1) cout << "! " << c2 + 1 << "\n";
			if(ans == 2) cout << "! " << c3 + 1 << "\n";
			cout.flush();
			continue;
		}
		int ans = bk(r, r);
		cout << "! " << ans + 1 << "\n";
		cout.flush();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
? 1 2
! 2

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 3660kb

input:

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

output:

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

result:

wrong answer Too many queries (test case 14)