QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#728445#9570. Binary Treeucup-team5657#RE 1ms7680kbC++143.6kb2024-11-09 15:11:302024-11-09 15:11:32

Judging History

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

  • [2024-11-09 15:11:32]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:7680kb
  • [2024-11-09 15:11:30]
  • 提交

answer

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

typedef long long ll;

const int N = 1e5 + 5;
const int B = 19;

struct edge {
	int to, Next;

	edge() {}
	edge(int to, int Next): to(to), Next(Next) {}
} G[N << 1];

int head[N], cnt;

inline void add_edge(int u, int v) {
	G[++cnt] = edge(v, head[u]);
	head[u] = cnt;
}

int T, n;

vector <int> V, tmp1;
vector <pair <int, int> > E, tmp2;

int fa[N], dep[N], dfn[N], dcnt;
pair <int, int> f[B][N];

inline void dfs0(int u) {
	dep[u] = dep[fa[u]] + 1;
	dfn[u] = ++dcnt;
	for (int i = head[u]; i; i = G[i].Next) {
		int v = G[i].to;
		if (v == fa[u]) {
			continue;
		}
		fa[v] = u;
		dfs0(v);
	}
}

inline void init() {
	dcnt = 0;
	dfs0(1);
	for (int i = 1; i <= n; ++i) {
		f[0][dfn[i]] = make_pair(dep[i], i);
	}
	for (int j = 1; j < B; ++j) {
		for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
			f[j][i] = min(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]);
		}
	}
}

inline int ask(int u, int v) {
	cout << "? " << u << " " << v << endl;
	int res;
	cin >> res;
	return res;
}

inline void print(int u) {
	cout << "! " << u << endl;
}

ll siz[N], g[N];

inline void dfs1(int fa, int u) {
	siz[u] = 1;
	for (int i = head[u]; i; i = G[i].Next) {
		int v = G[i].to;
		if (v == fa) {
			continue;
		}
		dfs1(u, v);
		siz[u] += siz[v];
		g[u] += g[v] + siz[v];
	}
}

inline void dfs2(int fa, int u) {
	if (fa) {
		g[u] = g[fa] - siz[u] + (V.size() - siz[u]);
	}
	for (int i = head[u]; i; i = G[i].Next) {
		int v = G[i].to;
		if (v == fa) {
			continue;
		}
		dfs2(u, v);
	}
}

inline int lca(int u, int v) {
	if (u == v) {
		return u;
	}
	if (dfn[u] > dfn[v]) {
		swap(u, v);
	}
	int d = __builtin_clz(dfn[v] - dfn[u]) ^ 31;
	int p = min(f[d][dfn[u] + 1], f[d][dfn[v] - (1 << d) + 1]).second;
	return fa[p];
}

inline int dis(int u, int v) {
	return dep[u] + dep[v] - (dep[lca(u, v)] << 1);
}

inline int F() {
	if (V.size() == 1) {
		return V[0];
	}
	for (auto u: V) {
		head[u] = 0;
	}
	cnt = 0;
	for (auto p: E) {
		add_edge(p.first, p.second);
		add_edge(p.second, p.first);
	}
	dfs1(0, V[0]);
	dfs2(0, V[0]);
	int u = -1;
	for (auto p: V) {
		if (u == -1 || g[p] <= g[u]) {
			u = p;
		}
	}
	int p = G[head[u]].to, q = G[G[head[u]].Next].to;
	if (!q) {
		q = u;
	}
	int res = ask(p, q);
	tmp1 = V;
	tmp2 = E;
	V.clear();
	E.clear();
	if (res == 0) {
		for (auto u: tmp1) {
			if (dis(u, p) < dis(u, q)) {
				V.emplace_back(u);
			}
		}
		for (auto k: tmp2) {
			if (dis(k.first, p) < dis(k.first, q) && dis(k.second, p) < dis(k.second, q)) {
				E.emplace_back(k);
			}
		}
	} else if (res == 1) {
		for (auto u: tmp1) {
			if (dis(u, p) == dis(u, q)) {
				V.emplace_back(u);
			}
		}
		for (auto k: tmp2) {
			if (dis(k.first, p) == dis(k.first, q) && dis(k.second, p) == dis(k.second, q)) {
				E.emplace_back(k);
			}
		}
	} else {
		for (auto u: tmp1) {
			if (dis(u, p) > dis(u, q)) {
				V.emplace_back(u);
			}
		}
		for (auto k: tmp2) {
			if (dis(k.first, p) > dis(k.first, q) && dis(k.second, p) > dis(k.second, q)) {
				E.emplace_back(k);
			}
		}
	}
	return F();
}

inline void solve() {
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		head[i] = 0;
	}
	cnt = 0;
	V.clear();
	E.clear();
	for (int i = 1; i <= n; ++i) {
		int u, v;
		cin >> u >> v;
		V.emplace_back(i);
		if (u) {
			E.emplace_back(make_pair(i, u));
			add_edge(i, u);
			add_edge(u, i);
		}
		if (v) {
			E.emplace_back(make_pair(i, v));
			add_edge(i, v);
			add_edge(v, i);
		}
	}
	init();
	print(F());
}

int main() {
	cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}

详细

Test #1:

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

input:

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

output:

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

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

output:

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

result: