QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#751312#9570. Binary TreeAdo1phosaRE 3ms13560kbC++233.0kb2024-11-15 18:04:302024-11-15 18:04:31

Judging History

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

  • [2024-11-15 18:04:31]
  • 评测
  • 测评结果:RE
  • 用时:3ms
  • 内存:13560kb
  • [2024-11-15 18:04:30]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define rep(i, l, r) for(int i = l; i <= (int)r; i ++)
#define debug(a) cout << #a << " = " << a << '\n';
#define debug2(a, b) cout << #a << " = " << a << "  " << #b << " = " << b << '\n';
const int N = 2e5 + 5;

int n, sz[N];
set<int> adj[N];
void dfs_fi(int x, int father, vector<int> &node) {
	node.push_back(x);
	for(auto y : adj[x]) {
		if(y == father) continue;
		dfs_fi(y, x, node);
	}
}
void dfs_se(int x, int father, int &center, int &weight, int total) {
	sz[x] = 1;
	vector<int> cut;
	for(auto y : adj[x]) {
		if(y == father) continue;
		dfs_se(y, x, center, weight, total);
		sz[x] += sz[y];
		cut.push_back(sz[y]);
	}
	cut.push_back(total - sz[x]);

	int Max = 0;
	for(auto x : cut) {
		Max = max(Max, x);
	}

	if(Max < weight) {
		weight = Max;
		center = x;
	}

}
int find_center(int x) {
	vector<int> node;
	dfs_fi(x, -1, node);
	for(auto x : node) sz[x] = 0;

	int center = 0, weight = 1e9;
	dfs_se(x, -1, center, weight, node.size());

	return center;
}
void answer(int s) {
	cout << "! " << s << endl;
}
int ask(int u, int v) {
	cout << "? " << u << " " << v << endl;
	int res;
	cin >> res;
	return res;
}
void add(int a, int b) {
	adj[a].insert(b);
	adj[b].insert(a);
}
void erase(int a, int b) {
	adj[a].erase(b);
	adj[b].erase(a);
}
int get_size(int x, int father) {
	int res = 1;
	for(auto y : adj[x]) {
		if(y == father) continue;
		res += get_size(y, x);
	}
	return res;
}
void solve() {
	cin >> n;
	rep(i, 1, n) adj[i].clear();
	rep(i, 1, n) {
		int l, r;
		cin >> l >> r;
		if(l) {
			add(i, l);
		}
		if(r) {
			add(i, r);
		}
	}



	
	int root = 1;
	while(true) {
		int center = find_center(root);

		int degree = adj[center].size();

		if(degree == 0) {
			answer(center);
			break;
		} else if(degree == 1) {
			int u = center, v = *adj[u].begin();
			int res = ask(u, v);
			if(res == 0)
				answer(u);
			else
				answer(v);
			break;
		} else if(degree == 2) {
			int u = *adj[center].begin(), v = *adj[center].rbegin();
			int res = ask(u, v);
			if(res == 1) {
				answer(center);
				break;
			} else {
				if(res == 0) {
					erase(center, u);
					root = u;
				} else {
					erase(center, v);
					root = v;
				}
			}
		} else {
			auto it = adj[center].begin();
			int u = *it, v = *(++ it), w = *(++ it);

			erase(center, u);
			erase(center, v);
			erase(center, w);

			if(get_size(u, -1) > get_size(w, -1)) swap(u, w);
			if(get_size(v, -1) > get_size(w, -1)) swap(v, w);


			int res = ask(u, v);
			if(res == 1) {
				add(center, w);
				root = center;
			} else {
				if(res == 0)
					root = u;
				else
					root = v;
			}
 		}
	}

	// cout << find_center(1) << '\n';

	// rep(i, 1, n) cout << sz[i] << " ";

	// dfs(root);
}
main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int _ = 1;
    cin >> _;
    while(_ --) {
    	solve();
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 13560kb

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
Runtime Error

input:

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

output:

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

result: