QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#760317 | #9733. Heavy-light Decomposition | OutsiderZz | WA | 13ms | 14300kb | C++23 | 1.9kb | 2024-11-18 16:10:30 | 2024-11-18 16:10:32 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n, k;
std::cin >> n >> k;
std::vector<int> l(k), r(k);
int cnt = 0;
for (int i = 0; i < k; i++) {
std::cin >> l[i] >> r[i];
l[i]--;
cnt += r[i] - l[i] - 1;
}
if (cnt + k - 1 != n - 1) {
//std::cerr << "debug\n";
std::cout << "IMPOSSIBLE\n";
return;
}
std::vector<std::vector<int>> adj(n);
for (int i = 0; i < k; i++) {
for (int j = l[i]; j + 1 < r[i]; j++) {
adj[j].push_back(j + 1);
}
}
std::vector<int> p(k);
std::iota(p.begin(), p.end(), 0);
std::sort(p.begin(), p.end(), [&](int i, int j) {
return r[i] - l[i] > r[j] - l[j];
});
int rt = l[p[0]];
std::vector<int> top(n, -1), ans(n, -1), parent(n);
for (int i = 1; i < k; i++) {
adj[rt].push_back(l[p[i]]);
}
ans[rt] = rt;
for (int i = 0; i < k; i++) {
for (int j = l[i]; j < r[i]; j++) {
ans[j] = l[i];
}
}
if (std::find(ans.begin(), ans.end(), -1) != ans.end()) {
std::cout << "IMPOSSIBLE\n";
return;
}
parent[rt] = -1;
top[rt] = rt;
//std::cerr << rt << "\n";
auto dfs = [&](auto &&self, int u) -> void {
for (auto v : adj[u]) {
parent[v] = u;
top[v] = v == adj[u][0] ? top[u] : v;
self(self, v);
}
};
dfs(dfs, rt);
if (ans != top) {
std::cout << "IMPOSSIBLE\n";
return;
}
for (int i = 0; i < n; i++) {
std::cout << parent[i] + 1 << " \n"[i == n - 1];
}
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
input:
3 12 5 1 5 9 11 7 8 6 6 12 12 4 3 1 1 4 4 2 3 2 2 1 1 2 2
output:
0 1 2 3 4 1 1 7 1 9 10 1 2 0 2 2 IMPOSSIBLE
result:
ok Correct. (3 test cases)
Test #2:
score: -100
Wrong Answer
time: 13ms
memory: 14300kb
input:
10 1 1 1 1 100000 1 1 100000 12 4 1 3 4 6 7 9 10 12 6 3 4 6 2 3 1 1 8999 3 1 3000 3001 6000 6001 8999 14 4 1 3 4 6 7 10 11 14 17 5 1 3 4 6 7 10 11 14 15 17 19999 2 1 9999 10000 19999 1 1 1 1 5 3 1 1 2 3 4 5
output:
0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
wrong answer 2 should be a heavy child, but the maximum one with size 3 and 2 found. (test case 3)