結果
問題 |
No.3047 Verification of Sorting Network
|
ユーザー |
👑 |
提出日時 | 2025-03-09 10:37:10 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 390 ms / 2,000 ms |
コード長 | 7,746 bytes |
コンパイル時間 | 4,154 ms |
コンパイル使用メモリ | 298,300 KB |
実行使用メモリ | 8,604 KB |
最終ジャッジ日時 | 2025-03-09 10:37:28 |
合計ジャッジ時間 | 17,532 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 61 |
ソースコード
// O(m*φ^n) + bit状態ベクトル #include <bits/stdc++.h> // all #if __cplusplus < 202002L #error C++20 or newer is required #endif #pragma GCC optimize ("Ofast") #pragma GCC target ("arch=x86-64-v3,tune=native") using namespace std; using NodeIndex = uint8_t; const int PROGRESS_THRESHOLD_N = 28; // 与えられた比較交換器ネットワークがソーティングネットワークかどうかをチェック // return: ソーティングネットワークなら未使用の比較交換器、そうでなければソートされていない可能性のある位置 expected<vector<bool>, vector<bool>> is_sorting_network(const int n, const int m, const vector<pair<NodeIndex, NodeIndex>> &cmps) { assert(2 <= n); for (auto [a, b] : cmps) { // 0-indexed かつ a < b かつ b < n であることを確認 assert(0 <= a && a < b && b < n); } // using State = uint32_t; using State = uint64_t; const State fullbit = ~(State)0; const int bits = numeric_limits<State>::digits; assert(n <= bits); // unused[i] は i 番目の比較交換器が未使用の場合に true // unsorted[i] は全ての比較交換器を通した後 i,(i+1) 番目の要素が昇順でない場合に true vector<bool> cmp_unused(m, true); State unsorted_i = 0; int progress_next = 0; int used_cmp_count = 0; // スタックに積む要素: 0状態ベクトル z, 1状態ベクトル o, 比較交換器の番号 i // もし ((z >> j) & 1, (o >> j) & 1) == (1, 1) なら j-bit目の状態は '?' // もし ((z >> j) & 1, (o >> j) & 1) == (1, 0) なら j-bit目の状態は '0' // もし ((z >> j) & 1, (o >> j) & 1) == (0, 1) なら j-bit目の状態は '1' // '0...01...1' または '0...0?1...1' の場合、ソートされている vector<pair<State, State>> queue; queue.emplace_back(fullbit >> (bits - n), fullbit); for (size_t j = 0; auto [a, b]: cmps) { vector<pair<State, State>> next_queue; next_queue.reserve(queue.size() * 2); bool cmp_unused_current = true; for (auto [zz, oo]: queue) { State z = zz, o = oo; if (((o >> a) & 1) == 0 || ((z >> b) & 1) == 0) { // 比較交換器の入力が (0, _) or (_, 1) {_ は任意を示す} の場合、交換はない // (0, 0) or (1, 1) or (0, 1) or (0, ?) or (?, 1) next_queue.emplace_back(z, o); } else if (((z >> a) & 1) == 0 || ((o >> b) & 1) == 0) { // この比較交換器は使用される可能性あり cmp_unused_current = false; // 比較交換器の入力が (1, 0) or (_, 0) or (1, _) の場合、要素を交換 State xz = (((z >> a) ^ (z >> b)) & (State)1); State xo = (((o >> a) ^ (o >> b)) & (State)1); z ^= ((xz << a) | (xz << b)); o ^= ((xo << a) | (xo << b)); // (z,o) がソートされていない場合、次の比較器へ if ((o & (z >> 1)) != 0) { next_queue.emplace_back(z, o); } } else { // この比較交換器は使用される可能性あり cmp_unused_current = false; // 比較交換器の入力が (_, _) であれば、 (0, 0) と (_, 1) に分岐して探索 // 新しい分岐は (_, _) -> (0, 0) に遷移 State qz = z, qo = (o & ~((State)1 << a) & ~((State)1 << b)); // 現在の分岐は (_, _) -> (_, 1) に遷移 z &= ~((State)1 << b); // (qz,qo) がソートされていない場合、次の比較器へ if ((qo & (qz >> 1)) != 0) { next_queue.emplace_back(qz, qo); } // (z,o) がソートされていない場合、次の比較器へ if ((o & (z >> 1)) != 0) { next_queue.emplace_back(z, o); } } } cmp_unused[j++] = cmp_unused_current; if (!cmp_unused_current) { used_cmp_count++; } sort(next_queue.begin(), next_queue.end()); decltype(next_queue)::iterator deduped = unique(next_queue.begin(), next_queue.end()); next_queue.erase(deduped, next_queue.end()); queue = move(next_queue); if (PROGRESS_THRESHOLD_N <= n && progress_next <= j) { int percent = j * 100 / m; progress_next = ((percent + 1) * m - 1) / 100 + 1; cerr << '\r' << percent << "%, used = " << used_cmp_count << '/' << m << flush; } } for (auto [z, o]: queue) { unsorted_i |= (o & (z >> 1)); } if (PROGRESS_THRESHOLD_N <= n) { cerr << endl; } // すべての分岐が終了 // いずれかの分岐でソートされていない場合、 unsorted に true が含まれるのでソーティングネットワークではない if (unsorted_i != 0) { // ソートされていない可能性のある位置を返す vector<bool> unsorted(n - 1, false); for (int k = 0; k < n - 1; ++k) { unsorted[k] = (((unsorted_i >> k) & 1) != 0); } return unexpected(unsorted); } // すべての分岐においてソートされたならばソーティングネットワーク // 未使用の比較交換器を返す return cmp_unused; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; for (int i = 0; i < t; ++i) { int n, m; cin >> n >> m; vector<pair<NodeIndex, NodeIndex>> cmps; vector<int> vec_a, vec_b; // 比較交換器を読み込む for (size_t j = 0; j < m; ++j) { int a; cin >> a; vec_a.push_back(a); } for (size_t j = 0; j < m; ++j) { int b; cin >> b; vec_b.push_back(b); } for (size_t 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, m, cmps); if (is_sorting.has_value()) { auto unused = is_sorting.value(); assert(unused.size() == m); cout << "Yes\n"; // 未使用の比較交換器 j を列挙 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"; // ソートされていない可能性がある位置 k を列挙 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; }