QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#743055 | #9733. Heavy-light Decomposition | _xqy_ | WA | 0ms | 3564kb | C++20 | 1.3kb | 2024-11-13 18:06:27 | 2024-11-13 18:06:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int,int>;
void solve() {
int n,k;
cin >> n >> k;
vector<pair<int,int>> seg(k);
for (int i = 0 ; i < k ; ++i) {
cin >> seg[i].first >> seg[i].second;
}
auto getlen = [&] (PII x) {
return x.second - x.first + 1;
};
sort(seg.begin() , seg.end() , [&](PII x , PII y){
return getlen(x) > getlen(y);
});
if (k == 1) {
for (int i = 1 ; i <= n ; ++i) {
cout << i - 1 << " \n"[i == n];
}
return;
}
if (n == 1) {
cout << 0 << "\n";
return;
}
vector<int> par(n + 1);
for (int j = 0 ; j < k ; ++j) {
for (int i = seg[j].first ; i <= seg[j].second ; ++i) {
if (i != seg[j].first) par[i] = i - 1;
}
}
int id = 0;
while (id < k && getlen(seg[id]) == getlen(seg[0])) {
id ++;
}
if (id == k) {
cout << "IMPOSSIBLE\n";
return;
}
for (int j = id ; j < k ; ++j) {
int i = seg[j].first;
par[i] = seg[0].first + 1;
}
for (int j = 1 ; j < id ; ++j) {
int i = seg[j].first;
par[i] = seg[0].first;
}
for (int i = 1 ; i <= n ; ++i) {
cout << par[i] << " \n"[i == n];
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3564kb
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 2 2 7 2 9 10 2 3 0 2 3 IMPOSSIBLE
result:
wrong answer vertex 3 is not a leaf (test case 2)