結果

問題 No.1744 Selfish Spies 1 (à la Princess' Perfectionism)
ユーザー CyanmondCyanmond
提出日時 2021-11-14 21:45:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 10,282 bytes
コンパイル時間 3,151 ms
コンパイル使用メモリ 223,348 KB
実行使用メモリ 15,276 KB
最終ジャッジ日時 2023-08-20 06:27:55
合計ジャッジ時間 23,343 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 14 ms
4,380 KB
testcase_15 AC 16 ms
4,376 KB
testcase_16 AC 23 ms
4,380 KB
testcase_17 AC 81 ms
4,380 KB
testcase_18 AC 65 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 4 ms
4,380 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 237 ms
5,536 KB
testcase_25 AC 18 ms
4,380 KB
testcase_26 AC 29 ms
4,380 KB
testcase_27 AC 153 ms
4,380 KB
testcase_28 AC 3,222 ms
14,972 KB
testcase_29 AC 139 ms
4,376 KB
testcase_30 AC 115 ms
4,376 KB
testcase_31 AC 137 ms
4,376 KB
testcase_32 AC 129 ms
4,380 KB
testcase_33 AC 1,559 ms
14,904 KB
testcase_34 AC 1,251 ms
15,276 KB
testcase_35 TLE -
testcase_36 AC 3,302 ms
14,980 KB
testcase_37 AC 1,778 ms
14,792 KB
testcase_38 AC 1,232 ms
14,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"
#include <bits/stdc++.h>

#line 2 "library/utility/eoln.cpp"

constexpr char eoln = '\n';
#line 2 "library/utility/int_alias.cpp"

#line 5 "library/utility/int_alias.cpp"

using i8 = std::int8_t;
using u8 = std::uint8_t;
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;
using isize = std::ptrdiff_t;
#line 2 "library/utility/int_infinity.cpp"

#line 5 "library/utility/int_infinity.cpp"

template <class T, T Div = 2> constexpr T infty = std::numeric_limits<T>::max() / Div;

constexpr i32 infi32 = infty<i32, 2>;
constexpr i64 infi64 = infty<i64, 4>;

constexpr u32 infu32 = infty<u32, 4>;
constexpr u64 infu64 = infty<u32, 4>;

constexpr isize infisz = infty<isize, 2>;
constexpr usize infusz = infty<usize, 4>;
#line 2 "library/utility/int_literal.cpp"

#line 6 "library/utility/int_literal.cpp"

constexpr std::int8_t operator""_i8(unsigned long long n) noexcept { return static_cast<std::int8_t>(n); }
constexpr std::int16_t operator""_i16(unsigned long long n) noexcept { return static_cast<std::int16_t>(n); }
constexpr std::int32_t operator""_i32(unsigned long long n) noexcept { return static_cast<std::int32_t>(n); }
constexpr std::int64_t operator""_i64(unsigned long long n) noexcept { return static_cast<std::int64_t>(n); }

constexpr std::int8_t operator""_u8(unsigned long long n) noexcept { return static_cast<std::uint8_t>(n); }
constexpr std::uint16_t operator""_u16(unsigned long long n) noexcept { return static_cast<std::uint16_t>(n); }
constexpr std::uint32_t operator""_u32(unsigned long long n) noexcept { return static_cast<std::uint32_t>(n); }
constexpr std::uint64_t operator""_u64(unsigned long long n) noexcept { return static_cast<std::uint64_t>(n); }

constexpr isize operator""_iz(unsigned long long n) noexcept { return static_cast<isize>(n); }
constexpr usize operator""_uz(unsigned long long n) noexcept { return static_cast<usize>(n); }
#line 7 "main.cpp"

#line 2 "library/utility/rep.cpp"

#line 4 "library/utility/rep.cpp"

class rep {
    struct rep_iterator {
        usize itr;
        constexpr rep_iterator(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const usize &other) const noexcept { return itr != other; }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const rep_iterator first;
    const usize last;

  public:
    constexpr rep(const usize first_, const usize last_) noexcept : first(first_), last(last_) {}
    constexpr rep_iterator begin() const noexcept { return first; }
    constexpr usize end() const noexcept { return last; }
};
#line 2 "library/utility/revrep.cpp"

#line 4 "library/utility/revrep.cpp"

class revrep {
    struct revrep_iterator {
        usize itr;
        constexpr revrep_iterator(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { --itr; }
        constexpr bool operator!=(const usize &other) const noexcept { return itr != other; }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const revrep_iterator first;
    const usize last;

  public:
    constexpr revrep(const usize first_, const usize last_) noexcept
        : first(last_ - 1), last(first_ - 1) {}
    constexpr revrep_iterator begin() const noexcept { return first; }
    constexpr usize end() const noexcept { return last; }
};
#line 10 "main.cpp"

#line 2 "library/utility/scan.cpp"

#line 4 "library/utility/scan.cpp"

template <class T> T scan() {
    T x;
    std::cin >> x;
    return x;
}
#line 12 "main.cpp"

#ifndef CL_DEBUG
#define pdebug(...)
#endif

#line 1 "atcoder/maxflow.hpp"



#line 9 "atcoder/maxflow.hpp"

#line 1 "atcoder/internal_queue.hpp"



#line 5 "atcoder/internal_queue.hpp"

namespace atcoder {

namespace internal {

template <class T> struct simple_queue {
    std::vector<T> payload;
    int pos = 0;
    void reserve(int n) { payload.reserve(n); }
    int size() const { return int(payload.size()) - pos; }
    bool empty() const { return pos == int(payload.size()); }
    void push(const T& t) { payload.push_back(t); }
    T& front() { return payload[pos]; }
    void clear() {
        payload.clear();
        pos = 0;
    }
    void pop() { pos++; }
};

}  // namespace internal

}  // namespace atcoder


#line 11 "atcoder/maxflow.hpp"

namespace atcoder {

template <class Cap> struct mf_graph {
  public:
    mf_graph() : _n(0) {}
    explicit mf_graph(int n) : _n(n), g(n) {}

    int add_edge(int from, int to, Cap cap) {
        assert(0 <= from && from < _n);
        assert(0 <= to && to < _n);
        assert(0 <= cap);
        int m = int(pos.size());
        pos.push_back({from, int(g[from].size())});
        int from_id = int(g[from].size());
        int to_id = int(g[to].size());
        if (from == to) to_id++;
        g[from].push_back(_edge{to, to_id, cap});
        g[to].push_back(_edge{from, from_id, 0});
        return m;
    }

    struct edge {
        int from, to;
        Cap cap, flow;
    };

    edge get_edge(int i) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        auto _e = g[pos[i].first][pos[i].second];
        auto _re = g[_e.to][_e.rev];
        return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
    }
    std::vector<edge> edges() {
        int m = int(pos.size());
        std::vector<edge> result;
        for (int i = 0; i < m; i++) {
            result.push_back(get_edge(i));
        }
        return result;
    }
    void change_edge(int i, Cap new_cap, Cap new_flow) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        assert(0 <= new_flow && new_flow <= new_cap);
        auto &_e = g[pos[i].first][pos[i].second];
        auto &_re = g[_e.to][_e.rev];
        _e.cap = new_cap - new_flow;
        _re.cap = new_flow;
    }

    Cap flow(int s, int t) {
        return flow(s, t, std::numeric_limits<Cap>::max());
    }
    Cap flow(int s, int t, Cap flow_limit) {
        assert(0 <= s && s < _n);
        assert(0 <= t && t < _n);
        assert(s != t);

        std::vector<int> level(_n), iter(_n);
        internal::simple_queue<int> que;

        auto bfs = [&]() {
            std::fill(level.begin(), level.end(), -1);
            level[s] = 0;
            que.clear();
            que.push(s);
            while (!que.empty()) {
                int v = que.front();
                que.pop();
                for (auto e : g[v]) {
                    if (e.cap == 0 || level[e.to] >= 0) continue;
                    level[e.to] = level[v] + 1;
                    if (e.to == t) return;
                    que.push(e.to);
                }
            }
        };
        auto dfs = [&](auto self, int v, Cap up) {
            if (v == s) return up;
            Cap res = 0;
            int level_v = level[v];
            for (int &i = iter[v]; i < int(g[v].size()); i++) {
                _edge &e = g[v][i];
                if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;
                Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));
                if (d <= 0) continue;
                g[v][i].cap += d;
                g[e.to][e.rev].cap -= d;
                res += d;
                if (res == up) return res;
            }
            level[v] = _n;
            return res;
        };

        Cap flow = 0;
        while (flow < flow_limit) {
            bfs();
            if (level[t] == -1) break;
            std::fill(iter.begin(), iter.end(), 0);
            Cap f = dfs(dfs, t, flow_limit - flow);
            if (!f) break;
            flow += f;
        }
        return flow;
    }

    std::vector<bool> min_cut(int s) {
        std::vector<bool> visited(_n);
        internal::simple_queue<int> que;
        que.push(s);
        while (!que.empty()) {
            int p = que.front();
            que.pop();
            visited[p] = true;
            for (auto e : g[p]) {
                if (e.cap && !visited[e.to]) {
                    visited[e.to] = true;
                    que.push(e.to);
                }
            }
        }
        return visited;
    }

  private:
    int _n;
    struct _edge {
        int to, rev;
        Cap cap;
    };
    std::vector<std::pair<int, int>> pos;
    std::vector<std::vector<_edge>> g;
};

} // namespace atcoder


#line 18 "main.cpp"

void main_() {
    // main process

    const usize N = scan<usize>();
    const usize M = scan<usize>();
    atcoder::mf_graph<int> graph(N + M + 2);
    const usize L = scan<usize>();
    std::vector<usize> S(L), T(L);
    for (const auto i : rep(0, L)) {
        S[i] = scan<usize>() - 1;
        T[i] = scan<usize>() - 1;
        graph.add_edge(S[i] + 1, N + T[i] + 1, 1);
    }
    for (const auto i : rep(0, N))
        graph.add_edge(0, i + 1, 1);
    for (const auto i : rep(0, M))
        graph.add_edge(N + i + 1, N + M + 1, 1);
    const auto max_flow = graph.flow(0, N + M + 1);

    auto edges = graph.edges();
    std::set<usize> needcheck;
    for (const auto i : rep(0, L))
        if (edges[i].flow == 1) needcheck.emplace(i);
    std::vector<bool> is_ok(L, true);
    for (const auto e : needcheck)
        is_ok[e] = false;

    while (not needcheck.empty()) {
        for (const auto i : rep(0, L + N + M))
            graph.change_edge(i, 1, 0);
        const auto f = *needcheck.begin();
        needcheck.erase(f);
        graph.change_edge(f, 1, 1);
        const auto max_flow_sub = graph.flow(0, N + M + 1);
        if (max_flow_sub != max_flow) continue;
        is_ok[f] = true;
        edges = graph.edges();
        for (const auto i : rep(0, L))
            if (edges[i].flow == 0) {
                if (needcheck.find(i) != needcheck.end()) {
                    is_ok[i] = true;
                    needcheck.erase(i);
                }
            }
    }
    for (const auto e : is_ok)
        std::cout << (e ? "Yes" : "No") << eoln;
    // Dinic 法の実用的な計算量はオーダーから想定されるより小さいらしいが、わからん!
}

int main(void) {
    usize T = 1;
    // std::cin >> T;
    while (T--)
        main_();
}
0