QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#811788#9570. Binary TreeInsert_Username_HereWA 1ms3620kbC++201.6kb2024-12-13 01:18:402024-12-13 01:18:40

Judging History

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

  • [2024-12-13 01:18:40]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3620kb
  • [2024-12-13 01:18:40]
  • 提交

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) 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].s, ch[i].s);
		sz[i] -= 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);
	if(sz[r] - sz[i] + 3 < sz[ch[i].f] + sz[ch[i].s]) 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);
	sz[i] = 1, 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);
		int ans = bk(r, r);
		cout << "! " << ans + 1 << "\n";
		cout.flush();
	}
}

详细

Test #1:

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

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: 0ms
memory: 3620kb

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

output:

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

result:

wrong answer Query two identical vertices 1 (test case 2)