QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#864358#9733. Heavy-light DecompositionJlyfishWA 0ms3584kbC++202.3kb2025-01-20 15:17:152025-01-20 15:17:42

Judging History

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

  • [2025-01-20 15:17:42]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3584kb
  • [2025-01-20 15:17:15]
  • 提交

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);
    if (k != 1 && a.front().second - a.front().first == a[1].second - a[1].first) {
        if (a.back().second - a.back().first - 1 == a.front().second - a.front().first) {
            cout << "IMPOSSIBLE\n";
            return;
        }
        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;
            }
        }
    } else {
        for (auto &[l, r] : a) {
            if (l != root) {
                fa[l] = root;
            }
            for (int i = l + 1; i <= r; i++) {
                fa[i] = i - 1;
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        cout << i << ' ' << fa[i] << '\n'; //<< " \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: 3584kb

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:

1 0
2 1
3 2
4 3
5 4
6 1
7 1
8 7
9 1
10 9
11 10
12 1
1 2
2 0
3 2
4 2
IMPOSSIBLE

result:

wrong answer n integers needed in one line. (test case 1)