QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#740031#9570. Binary TreeLight_KnightTL 1ms3696kbC++141.6kb2024-11-13 00:28:442024-11-13 00:28:45

Judging History

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

  • [2024-11-13 00:28:45]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3696kb
  • [2024-11-13 00:28:44]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;   const int N = 1e5+10;

int l[N], r[N], f[N], m, rt;

int dfs(int x){
	if(!x) return 0;
	int a = dfs(l[x]), b = dfs(r[x]);
	if(a == -1 || b == -1) return -1;
	if(a == 0 && b == 0) return 1;
	int c = m - a - b - 1, o;
	if(a <= m / 2 && b <= m / 2 && c <= m / 2){
		if(min(a, c) >= b){
			cout << "? " << l[x] << ' ' << f[x] << endl;
			cin >> o;
			if(o == 0) m = a, rt = l[x];
			if(o == 1) m = b + 1, l[x] = 0, rt = x;
			if(o == 2) m = c, (x == l[f[x]] ? l[f[x]] : r[f[x]]) = 0;
		}
		else if(min(b, c) >= a){
			cout << "? " << r[x] << ' ' << f[x] << endl;
			cin >> o;
			if(o == 0) m = b, rt = r[x];
			if(o == 1) m = a + 1, r[x] = 0, rt = x;
			if(o == 2) m = c, (x == l[f[x]] ? l[f[x]] : r[f[x]]) = 0;
		}
		else{
			cout << "? " << l[x] << ' ' << r[x] << endl;
			cin >> o;
			if(o == 0) m = a, rt = l[x];
			if(o == 1) m = c + 1, l[x] = r[x] = 0;
			if(o == 2) m = b, rt = r[x];
		}
		return -1;
	}
	return a + b + 1;
}

int main(){
	int t, n;   cin >> t;
	while(t--){
		cin >> n;   m = rt = 0;
		for(int i = 1; i <= n; i++){
			cin >> l[i] >> r[i];
			f[l[i]] = f[r[i]] = i;
		}
		for(int i = 1; i <= n; i++) if(!f[i]) {rt = i; break;}
		for(m = n; ; dfs(rt)){
			if(m == 1){
				cout << "! " << rt << endl;
				break;
			}
			if(m == 2){
				int x = (l[rt] ? l[rt] : r[rt]), o;
				cout << "? " << rt << ' ' << x << endl;
				cin >> o;
				cout << "! " << (!o ? rt : x) << endl;
				break;
			}
		}
		for(int i = 1; i <= n; i++) l[i] = r[i] = f[i] = 0;
	}
	return 0;
}

詳細信息

Test #1:

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

input:

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

output:

? 1 3
? 3 4
! 3
? 1 2
! 2

result:

ok OK (2 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

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

output:

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

result: