QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#735718 | #9570. Binary Tree | yzhx# | RE | 0ms | 0kb | C++17 | 2.6kb | 2024-11-11 21:24:37 | 2024-11-11 21:24:38 |
answer
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
const int N = 1e5;
int head[N + 2], nxt[N * 2 + 2], ver[N * 2 + 2], tot;
int n, v[N + 2], sz[N + 2], mx[N + 2], S, root;
void add(int x, int y) {
tot++;
nxt[tot] = head[x];
ver[tot] = y;
head[x] = tot;
}
void find(int x, int fa) {
sz[x] = 1, mx[x] = 0;
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if (y == fa || v[y]) continue;
find(y, x);
sz[x] += sz[y];
mx[x] = std::max(mx[x], sz[y]);
}
mx[x] = std::max(mx[x], S - sz[x]);
if (mx[x] < mx[root]) root = x;
}
void deal(int x) {
v[x] = 1;
std::vector<int> q;
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if (v[y]) continue;
q.push_back(y);
}
if (q.size() == 0) {
std::cout << "! " << x << std::endl;
std::cout.flush();
return;
} else if (q.size() == 1) { // ?
std::cout << q[0] << " " << x << std::endl;
std::cout.flush();
int a;
std::cin >> a;
if (a == 0)
std::cout << "! " << q[0] << std::endl;
else
std::cout << "! " << x << std::endl;
std::cout.flush();
return;
} else {
std::cout << q[0] << " " << q[1] << std::endl;
std::cout.flush();
int a;
std::cin >> a;
if (a == 1) {
v[q[0]] = 1, v[q[1]] = 1, v[x] = 0;
find(x, q[0]);
S = sz[x], root = 0, mx[0] = 1e9;
find(x, q[0]);
deal(root);
} else if (a == 0) {
find(q[0], x);
S = sz[q[0]], root = 0, mx[0] = 1e9;
find(q[0], x);
deal(root);
} else {
find(q[1], x);
S = sz[q[1]], root = 0, mx[0] = 1e9;
find(q[1], x);
deal(root);
}
}
std::cout.flush();
}
void solve() {
std::cin >> n;
tot = 0;
for (int i = 1; i <= n + 1; i++) head[i] = v[i] = sz[i] = mx[i] = 0;
for (int i = 1; i <= n; i++) {
int a, b;
std::cin >> a >> b;
if (a) add(i, a), add(a, i);
if (b) add(i, b), add(b, i);
}
// add(n, n + 1), add(n + 1, n);
// root = n + 1;
find(1, 0);
S = sz[1], root = 0, mx[0] = 1e9;
find(1, 0);
deal(root);
}
int main() {
int times;
std::cin >> times;
while (times--) {
solve();
}
return 0;
}
/*
1
5
4 0
1 3
0 5
0 0
0 0
*/
詳細信息
Test #1:
score: 0
Runtime Error
input:
2 5 0 0 1 5 2 4 0 0 0 0
output:
3 5