QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#829546#9570. Binary TreeDiaoTianhao#WA 13ms32120kbC++142.4kb2024-12-24 10:59:182024-12-24 10:59:19

Judging History

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

  • [2024-12-24 10:59:19]
  • 评测
  • 测评结果:WA
  • 用时:13ms
  • 内存:32120kb
  • [2024-12-24 10:59:18]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

namespace IO {
	inline ll rd() {
		char ch = getchar();
		ll s = 0, w = 1;
		while (ch < '0' || ch > '9') {
			if (ch == '-') w = -1;
			ch = getchar();
		}
		while (ch >= '0' && ch <= '9') {
			s = (s << 3) + (s << 1) + (ch ^ 48);
			ch = getchar();
		}
		return (s * w);
	}
	void wr(ll x) {
		if (x < 0) {
			putchar('-');
			x = -x;
		}
		if (x <= 9) {
			putchar(x + 48);
			return;
		}
		wr(x / 10);
		putchar(x % 10 + 48);
	}
}
using namespace IO;

constexpr ll INF = 1e18;

constexpr ll fn = 1e6;
constexpr ll N = fn + 10;

ll T, n;
vector<ll> E[N];

bool vis[N];
ll sum_sz, sz[N], mx_sz[N];
ll ct;
void proc_sz(ll x, ll fr) {
	sz[x] = 1;
	for (ll v : E[x]) {
		if (vis[v] || v == fr) continue;
		proc_sz(v, x);
		sz[x] += sz[v];
		mx_sz[x] = max(mx_sz[x], sz[v]);
	}
	mx_sz[x] = max(mx_sz[x], sum_sz - sz[x]);
	if (mx_sz[x] < mx_sz[ct]) {
		ct = x;
	}
}
void dac(ll x) {
	#define ask(u, v) \
	cout << "? " << (u) << ' ' << (v) << endl << flush; \
	ll k; \
	cin >> k;

	#define answer(u) \
	do { \
		cout << "! " << (u) << endl << flush; \
		return; \
	} while (0)

	#define recursion(v) \
	do { \
		sum_sz = sz[(v)]; \
		ct = 0; \
		proc_sz((v), 0); \
		dac(ct); \
	} while (0)

	vis[x] = 1;
	proc_sz(x, 0);

	vector<ll> P;
	for (ll v : E[x]) {
		if (!vis[v]) {
			P.push_back(v);
		}
	}
	sort(P.begin(), P.end(), [&](ll i, ll j) -> bool {
		return sz[i] > sz[j];
	});

	if (P.empty()) 
		answer(x);
	else if (ll(P.size()) == 1) {
		ask(x, P[0]);
		if (k == 0) 
			answer(x);
		else 
			recursion(P[0]);
	}
	else if (ll(P.size()) == 2) {
		ask(P[0], P[1]);
		if (k == 0) 
			recursion(P[0]);
		else if (k == 1) 
			answer(x);
		else 
			recursion(P[1]);
	}
	else {
		ask(P[0], P[1]);
		if (k == 0) 
			recursion(P[0]);
		else if (k == 1) {
			vis[P[0]] = 1, vis[P[1]] = 1, vis[x] = 0;
			recursion(P[2]);
		}
		else 
			recursion(P[1]);
	}
}

void DianFenZhi() {
	mx_sz[0] = INF;
	sum_sz = n;
	ct = 0;
	proc_sz(1, 0);
	dac(ct);
}

int main() {
	cin >> T;
	while (T--) {
		cin >> n;
		for (ll i = 1; i <= n; ++i) {
			E[i].clear();
			vis[i] = 0;
		}
		for (ll x = 1; x <= n; ++x) {
			ll u, v;
			cin >> u >> v;
			if (u) {
				E[x].push_back(u), E[u].push_back(x);
			}
			if (v) {
				E[x].push_back(v), E[v].push_back(x);
			}
		}
		
		DianFenZhi();
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 32120kb

input:

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

output:

? 3 1
? 2 5
! 2
? 2 1
! 1

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 13ms
memory: 31048kb

input:

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

output:

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

result:

wrong answer Too many queries (test case 2)