QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#733089#9570. Binary Treeucup-team059WA 0ms3552kbC++203.0kb2024-11-10 17:09:352024-11-10 17:09:36

Judging History

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

  • [2024-11-10 17:09:36]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3552kb
  • [2024-11-10 17:09:35]
  • 提交

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 heavy = -1, val = 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);
        for (int i = 0; i < n; i++){
            if (val < std::max(max[i], tot - max[i])){
                heavy = i;
                val = std::max(max[i], tot - max[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 << ' ' << v << 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();
        if (dag == 0) {
            std::cout << x + 1 << std::endl;
            return;
        }else if (dag == 1){
            int t = query(x, son[0].second);
            if (t == 0){
                std::cout << x << std::endl;
            }else{
                std::cout << son[0].second << 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 << x << 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[x] = 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: 3552kb

input:

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

output:

0

result:

wrong answer Token parameter [name=op] equals to "0", doesn't correspond to pattern "[?!]{1,1}" (test case 1)