結果

問題 No.1745 Selfish Spies 2 (à la Princess' Perfectionism)
ユーザー suisensuisen
提出日時 2022-03-17 14:02:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 778 ms / 5,000 ms
コード長 7,812 bytes
コンパイル時間 2,047 ms
コンパイル使用メモリ 139,476 KB
実行使用メモリ 41,036 KB
最終ジャッジ日時 2024-10-01 09:49:14
合計ジャッジ時間 9,490 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 1 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 1 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 1 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 2 ms
5,248 KB
testcase_17 AC 3 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 2 ms
5,248 KB
testcase_21 AC 2 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 7 ms
5,248 KB
testcase_25 AC 3 ms
5,248 KB
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 4 ms
5,248 KB
testcase_28 AC 27 ms
7,248 KB
testcase_29 AC 4 ms
5,248 KB
testcase_30 AC 4 ms
5,248 KB
testcase_31 AC 4 ms
5,248 KB
testcase_32 AC 4 ms
5,248 KB
testcase_33 AC 27 ms
7,320 KB
testcase_34 AC 25 ms
7,256 KB
testcase_35 AC 30 ms
7,232 KB
testcase_36 AC 30 ms
7,368 KB
testcase_37 AC 31 ms
7,364 KB
testcase_38 AC 27 ms
7,372 KB
testcase_39 AC 12 ms
5,632 KB
testcase_40 AC 18 ms
7,336 KB
testcase_41 AC 18 ms
7,380 KB
testcase_42 AC 37 ms
11,528 KB
testcase_43 AC 54 ms
14,620 KB
testcase_44 AC 71 ms
17,880 KB
testcase_45 AC 246 ms
28,092 KB
testcase_46 AC 256 ms
27,752 KB
testcase_47 AC 174 ms
40,340 KB
testcase_48 AC 165 ms
40,340 KB
testcase_49 AC 145 ms
11,944 KB
testcase_50 AC 131 ms
12,420 KB
testcase_51 AC 46 ms
6,708 KB
testcase_52 AC 26 ms
9,488 KB
testcase_53 AC 30 ms
8,948 KB
testcase_54 AC 84 ms
17,344 KB
testcase_55 AC 86 ms
17,224 KB
testcase_56 AC 778 ms
15,780 KB
testcase_57 AC 162 ms
40,912 KB
testcase_58 AC 167 ms
41,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define PROBLEM "https://yukicoder.me/problems/no/1745"

#include <iostream>

#include <atcoder/scc>

#include <algorithm>
#include <deque>
#include <random>
#include <utility>
#include <vector>

namespace suisen {
    struct BipartiteMatching {
        static constexpr int ABSENT = -1;

        BipartiteMatching() {}
        BipartiteMatching(int n, int m) : _n(n), _m(m), _to_r(_n, ABSENT), _to_l(_m, ABSENT), _g(n + m) {}

        void add_edge(int fr, int to) {
            _g[fr].push_back(to), _f = -1;
        }

        template <bool shuffle = true>
        int solve() {
            if (_f >= 0) return _f;

            static std::mt19937 rng(std::random_device{}());
            if constexpr (shuffle) for (auto &adj : _g) std::shuffle(adj.begin(), adj.end(), rng);

            std::vector<int8_t> vis(_n, false);
        
            auto dfs = [&, this](auto dfs, int u) -> bool {
                if (std::exchange(vis[u], true)) return false;
                for (int v : _g[u]) if (_to_l[v] == ABSENT) return _to_r[u] = v, _to_l[v] = u, true;
                for (int v : _g[u]) if (dfs(dfs, _to_l[v])) return _to_r[u] = v, _to_l[v] = u, true;
                return false;
            };
    
            for (bool upd = true; std::exchange(upd, false);) {
                vis.assign(_n, false);
                for (int i = 0; i < _n; ++i) if (_to_r[i] == ABSENT) upd |= dfs(dfs, i);
            }

            return _f = _n - std::count(_to_r.begin(), _to_r.end(), ABSENT);
        }

        std::vector<std::pair<int, int>> max_matching() {
            if (_f < 0) _f = solve();
            std::vector<std::pair<int, int>> res;
            res.reserve(_f);
            for (int i = 0; i < _n; ++i) if (_to_r[i] != ABSENT) res.emplace_back(i, _to_r[i]);
            return res;
        }

        std::vector<std::pair<int, int>> min_edge_cover() {
            auto res = max_matching();
            std::vector<bool> vl(_n, false), vr(_n, false);
            for (const auto &[u, v] : res) vl[u] = vr[v] = true;
            for (int u = 0; u < _n; ++u) for (int v : _g[u]) if (not (vl[u] and vr[v])) {
                vl[u] = vr[v] = true;
                res.emplace_back(u, v);
            }
            return res;
        }

        std::vector<int> min_vertex_cover() {
            if (_f < 0) _f = solve();
            std::vector<std::vector<int>> g(_n + _m);
            std::vector<bool> cl(_n, true), cr(_m, false);
            for (int u = 0; u < _n; ++u) for (int v : _g[u]) {
                if (_to_r[u] == v) {
                    g[v + _n].push_back(u);
                    cl[u] = false;
                } else {
                    g[u].push_back(v + _n);
                }
            }
            std::vector<bool> vis(_n + _m, false);
            std::deque<int> dq;
            for (int i = 0; i < _n; ++i) if (cl[i]) {
                dq.push_back(i);
                vis[i] = true;
            }
            while (dq.size()) {
                int u = dq.front();
                dq.pop_front();
                for (int v : g[u]) {
                    if (vis[v]) continue;
                    vis[v] = true;
                    (v < _n ? cl[v] : cr[v - _n]) = true;
                    dq.push_back(v);
                }
            }
            std::vector<int> res;
            for (int i = 0; i < _n; ++i) if (not cl[i]) res.push_back(i);
            for (int i = 0; i < _m; ++i) if (cr[i]) res.push_back(_n + i);
            return res;
        }
        
        std::vector<int> max_independent_set() {
            std::vector<bool> use(_n + _m, true);
            for (int v : min_vertex_cover()) use[v] = false;
            std::vector<int> res;
            for (int i = 0; i < _n + _m; ++i) if (use[i]) res.push_back(i);
            return res;
        }

        int left_size() const { return _n; }
        int right_size() const { return _m; }
        std::pair<int, int> size() const { return { _n, _m }; }

        int right(int l) const { return _to_r[l]; }
        int left(int r) const { return _to_l[r]; }

        const auto graph() const { return _g; }

        auto reversed_graph() const {
            std::vector<std::vector<int>> h(_m);
            for (int i = 0; i < _n; ++i) for (int j : _g[i]) h[j].push_back(i);
            return h;
        }

    private:
        int _n, _m;
        std::vector<int> _to_r, _to_l;
        std::vector<std::vector<int>> _g;
        int _f = 0;
    };
    
} // namespace suisen

namespace suisen {
    std::vector<std::pair<std::vector<int>, std::vector<int>>> dulmage_mendelsohn_decomposition(BipartiteMatching& bm) {
        bm.solve();
        const int n = bm.left_size(), m = bm.right_size();

        std::vector<int8_t> wk_l(n, false), wk_r(m, false);
        const auto& g = bm.graph();
        auto dfs_l = [&](auto dfs_l, int i) -> void {
            if (i == BipartiteMatching::ABSENT or std::exchange(wk_l[i], true)) return;
            for (int j : g[i]) wk_r[j] = true, dfs_l(dfs_l, bm.left(j));
        };
        for (int i = 0; i < n; ++i) if (bm.right(i) == BipartiteMatching::ABSENT) dfs_l(dfs_l, i);

        std::vector<int8_t> w0_l(n, false), w0_r(m, false);
        const auto h = bm.reversed_graph();
        auto dfs_r = [&](auto dfs_r, int j) -> void {
            if (j == BipartiteMatching::ABSENT or std::exchange(w0_r[j], true)) return;
            for (int i : h[j]) w0_l[i] = true, dfs_r(dfs_r, bm.right(i));
        };
        for (int j = 0; j < m; ++j) if (bm.left(j) == BipartiteMatching::ABSENT) dfs_r(dfs_r, j);

        std::vector<std::pair<std::vector<int>, std::vector<int>>> dm;
        auto add_pair = [&](int i, int j) {
            auto& [l, r] = dm.back();
            l.push_back(i), r.push_back(j);
        };
        // W_0
        dm.emplace_back();
        for (int i = 0; i < n; ++i) if (w0_l[i]) {
            add_pair(i, bm.right(i));
        }
        for (int j = 0; j < m; ++j) if (w0_r[j] and bm.left(j) == BipartiteMatching::ABSENT) {
            dm.back().second.push_back(j);
        }
        // W_1, ..., W_{k-1}
        atcoder::scc_graph scc_g(n + m);
        for (int i = 0; i < n; ++i) {
            for (int j : g[i]) scc_g.add_edge(i, n + j);
            int j = bm.right(i); 
            if (j != BipartiteMatching::ABSENT) scc_g.add_edge(n + j, i);
        }
        for (const auto& group : scc_g.scc()) {
            if (int v0 = group.front(); (v0 < n and (w0_l[v0] or wk_l[v0])) or (v0 >= n and (w0_r[v0 - n] or wk_r[v0 - n]))) continue;
            dm.emplace_back();
            for (int i : group) if (i < n) add_pair(i, bm.right(i));
        }
        // W_k
        dm.emplace_back();
        for (int j = 0; j < m; ++j) if (wk_r[j]) {
            add_pair(bm.left(j), j);
        }
        for (int i = 0; i < n; ++i) if (wk_l[i] and bm.right(i) == BipartiteMatching::ABSENT) {
            dm.back().first.push_back(i);
        }
        return dm;
    }
} // namespace suisen

using namespace suisen;

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

    int n, m, l;
    std::cin >> n >> m >> l;

    std::vector<std::pair<int, int>> edges;
    BipartiteMatching bm(n, m);
    for (int i = 0; i < l; ++i) {
        int u, v;
        std::cin >> u >> v;
        --u, --v;
        bm.add_edge(u, v);
        edges.emplace_back(u, v);
    }

    auto dm = dulmage_mendelsohn_decomposition(bm);
    const int k = dm.size() - 1;

    std::vector<int> cl(n), cr(m);
    for (int i = 0; i <= k; ++i) {
        for (int u : dm[i].first)  cl[u] = i;
        for (int v : dm[i].second) cr[v] = i;
    }

    for (const auto &[u, v] : edges) {
        if (cl[u] != cr[v]) {
            std::cout << "No\n";
        } else {
            std::cout << "Yes\n";
        }
    }
    return 0;
}

0