結果

問題 No.3047 Verification of Sorting Network
ユーザー 👑 Mizar
提出日時 2025-01-31 21:20:07
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 3,245 bytes
コンパイル時間 3,612 ms
コンパイル使用メモリ 287,660 KB
実行使用メモリ 11,680 KB
最終ジャッジ日時 2025-03-05 20:32:48
合計ジャッジ時間 7,237 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 TLE * 1
other -- * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

// TLE想定 O(M*2^n)解

#include <bits/stdc++.h> // all
using namespace std;

#define MAX_T 10000
#define MAX_N 27
#define MAX_COST 1e8

expected<vector<bool>, vector<bool>> is_sorting_network(const int n, const vector<pair<int, int>> cmps) {
    using stat = uint64_t;
    assert(2 <= n && n <= MAX_N && MAX_N <= numeric_limits<stat>::digits);
    for (auto [a, b] : cmps) {
        // 0-indexed
        assert(0 <= a && a < b && b < n);
    }
    int m = cmps.size();
    vector<bool> unused(m, true), unsorted(n - 1, false);
    for (stat i = ((~(stat)1) << (n - 1)); i != 0; i++) {
        stat state = i;
        for (int j = 0; auto [a, b] : cmps) {
            if (((state >> a) & 1) > ((state >> b) & 1)) {
                unused[j] = false;
                state ^= ((stat)1 << a) | ((stat)1 << b);
            }
            ++j;
        }
        if (countl_one(state) + countr_zero(state) < numeric_limits<stat>::digits) {
            for (int j = 0; j < n - 1; ++j) {
                if (((state >> j) & 3) == 1) {
                    unsorted[j] = true;
                }
            }
        }
    }
    if (any_of(unsorted.begin(), unsorted.end(), [](bool x) { return x; })) {
        return unexpected(unsorted);
    }
    return unused;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    assert(1 <= t && t <= MAX_T);

    // phi = (1 + sqrt(5)) / 2 : golden ratio
    double phi = sqrt(1.25) + 0.5;
    // testcases cost <= 1e8
    double cost = 0.0;

    for (int i = 0; i < t; ++i) {
        int n, m;
        cin >> n >> m;
        vector<pair<int, int>> cmps;
        assert(2 <= n && n <= MAX_N && 1 <= m && m <= n * (n - 1) / 2);
        cost += m * pow(phi, n);
        assert(cost <= MAX_COST);
        vector<int> vec_a, vec_b;
        for (int j = 0; j < m; ++j) {
            int a;
            cin >> a;
            vec_a.push_back(a);
        }
        for (int j = 0; j < m; ++j) {
            int b;
            cin >> b;
            vec_b.push_back(b);
        }
        for (int j = 0; j < m; ++j) {
            int a = vec_a[j], b = vec_b[j];
            assert(1 <= a && a < b && b <= n);
            // 1-indexed to 0-indexed
            cmps.emplace_back(a - 1, b - 1);
        }
        auto is_sorting = is_sorting_network(n, cmps);
        if (is_sorting.has_value()) {
            auto unused = is_sorting.value();
            assert(unused.size() == m);
            cout << "Yes\n";
            cout << count(unused.begin(), unused.end(), true) << '\n';
            // 1-indexed
            for (int j = 1; const auto x : unused) {
                if (x) {
                    cout << j << ' ';
                }
                j++;
            }
            cout << '\n';
        } else {
            auto unsorted = is_sorting.error();
            assert(unsorted.size() == n - 1);
            cout << "No\n";
            cout << count(unsorted.begin(), unsorted.end(), true) << '\n';
            // 1-indexed
            for (int j = 1; const auto x : unsorted) {
                if (x) {
                    cout << j << ' ';
                }
                j++;
            }
            cout << '\n';
        }
    }

    return 0;
}
0