QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#864348#9733. Heavy-light DecompositionJlyfishWA 0ms3712kbC++201.7kb2025-01-20 15:08:302025-01-20 15:08:37

Judging History

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

  • [2025-01-20 15:08:37]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3712kb
  • [2025-01-20 15:08:30]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
void solve() {
    int n, k;
    cin >> n >> k;

    vector<pair<int, int>> a(k);
    for (auto &[l, r] : a) {
        cin >> l >> r;
    }

    if (n == 1) {
        cout << "0\n";
        return;
    }

    vector<int> sum(n + 2);
    for (auto &[l, r] : a) {
        sum[l]++;
        sum[r + 1]--;
    }

    for (int i = 1; i <= n; i++) {
        sum[i] += sum[i - 1];
        if (sum[i] != 1) {
            cout << "IMPOSSIBLE\n";
            return;
        }
    }

    ranges::sort(a, [&](auto l, auto r) {
        return l.second - l.first > r.second - r.first;
    });

    int root = a[0].first;

    if (k != 1 && a.back().second - a.back().first == a.front().second - a.front().first) {
        cout << "IMPOSSIBLE\n";
        return;
    }

    if (a.front().second - a.front().first == 1 && a[1].second - a[1].first == 1) {
        cout << "IMPOSSIBLE\n";
        return;
    }

    vector<int> fa(n + 1);
    for (int i = a[0].first + 1; i <= a[0].second; i++) {
        fa[i] = i - 1;
    }
    assert(a[0].first + 1 <= a[0].second);
    fa[a.back().first] = a[0].first + 1;
    for (int i = a.back().first + 1; i <= a.back().second; i++) {
        fa[i] = i - 1;
    }

    a.erase(a.begin());
    a.erase(prev(a.end()));

    for (auto &[l, r] : a) {
        fa[l] = root;
        for (int i = l + 1; i <= r; i++) {
            fa[i] = i - 1;
        }
    }

    for (int i = 1; i <= n; i++) {
        cout << fa[i] << " \n"[i == n];
    }
}
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int TT;
    cin >> TT;

    while (TT--) {
        solve();   
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3712kb

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 2
2 0 2 3
IMPOSSIBLE

result:

wrong answer vertex 3 is not a leaf (test case 2)