QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#733184 | #9570. Binary Tree | ucup-team059 | WA | 0ms | 3848kb | C++20 | 3.3kb | 2024-11-10 17:34:59 | 2024-11-10 17:35:00 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
void solve(){
int n;
std::cin >> n;
std::vector<std::vector<int>> adj(n);
for (int i = 0; i < n; i++){
int L, R;
std::cin >> L >> R;
L--, R--;
if (R >= 0) {
adj[i].push_back(R);
adj[R].push_back(i);
}
if (L >= 0) {
adj[i].push_back(L);
adj[L].push_back(i);
}
}
std::vector<int> cut(n);
auto work = [&](int root){
std::vector<int> size(n, 1e9), max(n, 1e9);
int tot = 0;
auto dfs = [&](auto &&self, int x, int fa)->void{
size[x] = 1;
max[x] = 0;
tot++;
for (auto y : adj[x]){
if (y == fa || cut[y]) continue;
self(self, y, x);
size[x] += size[y];
max[x] = std::max(max[x], size[y]);
}
};
dfs(dfs, root, -1);
int heavy = root, val = std::max(max[heavy], tot - size[heavy]);
for (int i = 0; i < n; i++){
if (val > std::max(max[i], tot - size[i])){
heavy = i;
val = std::max(max[i], tot - size[i]);
}
}
std::vector<std::pair<int, int>> son;
for (auto y : adj[heavy]){
if (cut[y]) continue;
son.push_back({size[y], y});
}
std::sort(son.begin(), son.end(), std::greater<>());
return std::make_pair(heavy, son);
};
auto query = [](int u, int v){
std::cout << "? " << u + 1 << ' ' << v + 1 << std::endl;
int t;
std::cin >> t;
return t;
};
int root = 0;
while (true){
auto [x, son] = work(root);
int dag = 0;
dag = son.size();
std::cout << dag << " ";
std::cout << x + 1 << std::endl;
if (dag == 0) {
std::cout << "! ";
std::cout << x + 1 << std::endl;
return;
}else if (dag == 1){
int t = query(x, son[0].second);
if (t == 0){
std::cout << "! ";
std::cout << x + 1 << std::endl;
}else{
std::cout << "! ";
std::cout << son[0].second + 1 << std::endl;
}
return;
}else if (dag == 2){
int t = query(son[0].second, son[1].second);
if (t == 0){
cut[x] = 1;
root = son[0].second;
}else if (t == 1){
std::cout << "! ";
std::cout << x + 1 << std::endl;
return;
}else{
cut[x] = 1;
root = son[1].second;
}
}else if (dag == 3){
int t = query(son[0].second, son[1].second);
if (t == 0){
cut[x] = 1;
root = son[0].second;
}else if (t == 1){
cut[son[0].second] = 1;
cut[son[1].second] = 1;
root = son[2].second;
}else{
cut[x] = 1;
root = son[1].second;
}
}
}
}
int main() {
int t;
std::cin >> t;
while (t--){
solve();
}
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3848kb
input:
2 5 0 0 1 5 2 4 0 0 0 0
output:
3 2 ? 1 3
result:
wrong answer Token parameter [name=op] equals to "3", doesn't correspond to pattern "[?!]{1,1}" (test case 1)