题目
解析
把每一场比赛看作满二叉树的一个节点,父节点递归遍历子节点的结果,进行试填。
代码
cpp
#include <bits/stdc++.h>
using i64 = long long;
struct Node {
int win, lose;
};
void solve() {
int k;
std::cin >> k;
int siz = (1 << k);
std::vector<Node> tree(siz);
for (int r = k; r > 0; r--) {
int n = 1 << (r - 1);
int s = n;
while (n--) {
std::cin >> tree[s++].lose;
}
}
std::cin >> tree[1].win;
if (tree[1].win < tree[1].lose) {
std::cout << "No Solution\n";
} else {
std::function<bool(int, int)> dfs = [&] (int u, int siz) -> bool {
if (u * 2 >= siz) {
return true;
}
int l = u * 2, r = u * 2 + 1;
int faw = tree[u].win, fal = tree[u].lose;
if (faw > tree[l].lose && fal > tree[r].lose) {
tree[l].win = faw;
tree[r].win = fal;
if (dfs(l, siz) && dfs(r, siz)) {
return true;
}
tree[l].win = tree[r].win = 0;
}
if (faw > tree[r].lose && fal > tree[l].lose) {
tree[r].win = faw;
tree[l].win = fal;
if (dfs(l, siz) && dfs(r, siz)) {
return true;
}
}
return false;
};
if (dfs(1, siz)) {
for (int i = (1 << (k - 1)); i < siz; i++) {
std::cout << tree[i].win << " " << tree[i].lose;
if (i != siz - 1) std::cout << " ";
}
} else {
std::cout << "No Solution\n";
}
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int t = 1;
// std::cin >> t;
for (int i = 1; i <= t; i++) {
// std::cout << "Case " << i << ": ";
solve();
}
return 0;
}