QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#733286#9570. Binary Treeucup-team5008#ML 1ms4024kbC++232.1kb2024-11-10 18:07:092024-11-10 18:07:11

Judging History

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

  • [2024-11-10 18:07:11]
  • 评测
  • 测评结果:ML
  • 用时:1ms
  • 内存:4024kb
  • [2024-11-10 18:07:09]
  • 提交

answer

#include <cstdio>
#include <cassert>
#include <algorithm>
#include <vector>

const int N = 100000;
std::vector<int> g[N];
int n;
bool done[N];

void connect(int x, int y) {
	g[x].push_back(y);
	g[y].push_back(x);
}

int get_size(int v, int pr = -1) {
	int size = 1;
	for (int i = 0; i < (int)g[v].size(); ++i) if (g[v][i] != pr && !done[g[v][i]]) size += get_size(g[v][i], v);
	return size;
}

int get_c(int v, int cur_size, int pr = -1) {
	int size = 1;
	for (int i = 0; i < (int)g[v].size(); ++i) {
		int to = g[v][i];
		if (to != pr && !done[to]) {
			int k = get_c(to, cur_size, v);
			if (k <= 0) return k;
			size += k;
		}
	}
	if (size * 2 >= cur_size) return -v;
	return size;
}

int query(int a, int b) {
	printf("? %d %d\n", a + 1, b + 1);
	fflush(stdout);
	int re;
	scanf("%d", &re);
	return re;
}

int solve() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		done[i] = 0;
		g[i].clear();
	}
	for (int i = 0; i < n; ++i) {
		int l, r;
		scanf("%d%d", &l, &r);
		if (l) connect(i, l - 1);
		if (r) connect(i, r - 1);
	}
	int c = 0;
	while (true) {
		c = -get_c(c, get_size(c));
		std::vector<int> child;
		for (int i = 0; i < (int)g[c].size(); ++i) if (!done[g[c][i]]) child.push_back(g[c][i]);
		assert(child.size() <= 3);
		if (child.size() == 0) return c;
		int re;
		done[c] = 1;
		if (child.size() == 1) {
			re = query(child[0], c);
			if (re == 0) return child[0];
			return c;
		}
		if (child.size() == 2) {
			re = query(child[0], child[1]);
			if (re == 0) c = child[0];
			else if (re == 1) return c;
			c = child[1];
		} else {
			std::vector<std::pair<int, int> > ord;
			for (int i = 0; i < 3; ++i) ord.push_back(std::make_pair(get_size(child[i]), child[i]));
			std::sort(ord.begin(), ord.end());
			re = query(child[ord[1].second], child[ord[2].second]);
			if (re == 0) c = child[ord[1].second];
			else if (re == 1) {
				done[c] = 0;
				done[child[ord[1].second]] = 1;
				done[child[ord[2].second]] = 1;
			} else c = child[ord[2].second];
		}
	}
	return 0;
}

int main() {
	int tt;
	scanf("%d", &tt);
	while (tt--) {
		printf("! %d\n", solve() + 1);
		fflush(stdout);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Memory Limit Exceeded

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
2

output:

? 1 5
! 5

result: