結果

問題 No.3047 Verification of Sorting Network
ユーザー 👑 Mizar
提出日時 2025-02-01 19:01:29
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 4,223 bytes
コンパイル時間 5,249 ms
コンパイル使用メモリ 291,328 KB
実行使用メモリ 8,612 KB
最終ジャッジ日時 2025-03-05 20:35:41
合計ジャッジ時間 82,314 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 48 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

// 乱択解 (おそらくWA)

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

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

class XorShift64 {
public:
    using result_type = uint64_t;

    XorShift64(uint64_t seed) : state(seed) {}

    uint64_t operator()() {
        state ^= state << 13;
        state ^= state >> 7;
        state ^= state << 17;
        return state;
    }

    static constexpr uint64_t min() { return 0; }
    static constexpr uint64_t max() { return UINT64_MAX; }

private:
    uint64_t state;
};

expected<vector<bool>, vector<bool>> is_sorting_network(const int n, const vector<pair<int, int>> cmps) {
    assert(2 <= n && n <= MAX_N && MAX_N <= UINT64_WIDTH);
    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);
    vector<int> states(n, 0);
    for (int i = 0; i < n; ++i) {
        states[i] = i;
    }
    random_device seed_gen;
    mt19937_64 rng(seed_gen() ^ chrono::steady_clock::now().time_since_epoch().count());
    //XorShift64 rng(seed_gen() ^ chrono::steady_clock::now().time_since_epoch().count());

    const double phi = sqrt(1.25) + 0.5;
    uint64_t limit = pow(phi, n) * 2.5 + 0.5;  // 試行回数の上限、でもよく見落とす
    for (uint64_t i = 0; i < limit; i += 1) {
        shuffle(states.begin(), states.end(), rng);
        for (int j = 0; auto [a, b] : cmps) {
            if (states[a] > states[b]) {
                swap(states[a], states[b]);
                unused[j] = false;
            }
            ++j;
        }
        for (int k = 1; k < n; ++k) {
            if (states[k - 1] > states[k]) {
                unsorted[k - 1] = 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';
            bool first = true;
            // 1-indexed
            for (int j = 1; const auto x : unused) {
                if (x) {
                    if (!first) {
                        cout << ' ';
                    }
                    cout << j;
                    first = false;
                }
                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';
            bool first = true;
            // 1-indexed
            for (int k = 1; const auto x : unsorted) {
                if (x) {
                    if (!first) {
                        cout << ' ';
                    }
                    cout << k;
                    first = false;
                }
                k++;
            }
            cout << '\n';
        }
    }

    return 0;
}
0