QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#733296 | #9570. Binary Tree | ucup-team5008# | ML | 0ms | 3772kb | C++23 | 2.2kb | 2024-11-10 18:09:36 | 2024-11-10 18:09:37 |
Judging History
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, cnt = 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];
}
cnt += 1;
if ((1 << cnt) > n) assert(false);
}
return 0;
}
int main() {
int tt;
scanf("%d", &tt);
while (tt--) {
printf("! %d\n", solve() + 1);
for (int i = 0; i < n; ++i) g[i].clear();
fflush(stdout);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3772kb
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