cp-documentation

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub zawa-tin/cp-documentation

:heavy_check_mark: ABC318-G Typical Path Problem
(Test/AtCoder/abc318_g.test.cpp)

頂点容量が $B$ だけ2、他は1であるネットワークに $B$ を始点として水を流した時、 $A$ と $C$ 両方に水が流れるか?と言い換えることができる。

無向辺 $(u, v)$ のネットワークに対しては $u\to v$ へ流れた水と $v\to u$ へ流れた水は相殺していると考えてよく、両方の向きの辺を独立に張って良い

グラフのサイズが巨大だが、最大流量が2のためフローアルゴリズムが爆足

Depends on

Code

#define PROBLEM "https://atcoder.jp/contests/abc318/tasks/abc318_g"

#include "../../Src/Template/IOSetting.hpp"
#include "../../Src/Graph/Flow/Dinic.hpp"

#include <cassert>
#include <iostream>

int main() {
    using namespace zawa;
    SetFastIO();
    int n, m; std::cin >> n >> m;
    int a, b, c; std::cin >> a >> b >> c;
    a--; b--; c--;
    Dinic<int> mf(2 * n + 2, 2 + n + 2 * m);
    for (int i{} ; i < n ; i++) {
        mf.addEdge(i, i + n, (i == b ? 2 : 1));
    }
    for (int _{} ; _ < m ; _++) {
        int u, v; std::cin >> u >> v;
        u--; v--;
        mf.addEdge(n + u, v, 1);
        mf.addEdge(n + v, u, 1);
    }
    int s{2 * n}, t{s + 1};
    mf.addEdge(s, b, 2);
    mf.addEdge(n + a, t, 1);
    mf.addEdge(n + c, t, 1);
    int flow{mf.flow(s, t)};
    assert(flow == 1 or flow == 2);
    bool ans{flow == 2};
    std::cout << (ans ? "Yes" : "No") << '\n';
}
#line 1 "Test/AtCoder/abc318_g.test.cpp"
#define PROBLEM "https://atcoder.jp/contests/abc318/tasks/abc318_g"

#line 2 "Src/Template/IOSetting.hpp"

#line 2 "Src/Template/TypeAlias.hpp"

#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa
#line 4 "Src/Template/IOSetting.hpp"

#include <iostream>
#include <iomanip>

namespace zawa {

void SetFastIO() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
}

void SetPrecision(u32 dig) {
    std::cout << std::fixed << std::setprecision(dig);
}

} // namespace zawa
#line 2 "Src/Graph/Flow/Dinic.hpp"

#line 2 "Src/Utility/U32Pair.hpp"

#line 4 "Src/Utility/U32Pair.hpp"

#include <functional>
#line 7 "Src/Utility/U32Pair.hpp"

namespace zawa {

class U32Pair {
private:
    static constexpr u32 SHIFT{32};
    static constexpr u32 MASK{static_cast<u32>((1LL << SHIFT) - 1)};
    u64 value_{};
public:
    constexpr U32Pair() {}
    constexpr U32Pair(u32 first, u32 second) {
        value_ = (static_cast<u64>(first) << SHIFT) | second;
    }
    constexpr u32 first() const noexcept {
        return static_cast<u32>(value_ >> SHIFT);
    }
    constexpr u32 second() const noexcept {
        return static_cast<u32>(value_ & MASK);
    }
    constexpr u64 combined() const noexcept {
        return value_;
    }
    constexpr U32Pair& operator=(const U32Pair& rhs) {
        value_ = rhs.value_;
        return *this;
    }
    friend constexpr bool operator==(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ == rhs.value_;
    }
    friend constexpr bool operator!=(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ != rhs.value_;
    }
    friend constexpr bool operator<(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ < rhs.value_;
    }
    friend constexpr bool operator<=(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ <= rhs.value_;
    }
    friend constexpr bool operator>(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ > rhs.value_;
    }
    friend constexpr bool operator>=(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ >= rhs.value_;
    }
    friend std::ostream& operator<<(std::ostream& os, const U32Pair& pair) {
        os << '(' << pair.first() << ',' << pair.second() << ')';
        return os;
    }
};

struct U32PairHash {
    usize operator()(const U32Pair& pair) const noexcept {
        return std::hash<u64>{}(pair.combined());
    }
};

} // namespace zawa
#line 5 "Src/Graph/Flow/Dinic.hpp"

#include <algorithm>
#include <cassert>
#include <limits>
#include <type_traits>
#include <vector>
#include <queue>

namespace zawa {

template <class Cap> 
class Dinic {
private:
    static_assert(std::is_signed_v<Cap>, "Cap must be signed");
    usize n_{}, m_{};
    static constexpr u32 invalid() noexcept {
        return std::numeric_limits<u32>::max();
    }
public:
    inline usize size() const noexcept {
        return n_;
    }
    inline usize edgeNumber() const noexcept {
        return m_;
    }
private:
    struct Edge {
        u32 to{}, rev{};
        Cap residual{};
        Edge() = default;
        Edge(u32 to, u32 rev, const Cap& residual) 
            : to{to}, rev{rev}, residual{residual} {}
    };

    std::vector<std::vector<Edge>> g_;
    std::vector<U32Pair> edges_;
    std::vector<u32> label_, cur_;

    bool dualStep(u32 s, u32 t) {
        std::fill(label_.begin(), label_.end(), invalid());
        label_[s] = 0;
        std::queue<u32> queue{ { s } };
        while (queue.size()) {
            u32 v{queue.front()};
            queue.pop();
            for (const Edge& e : g_[v]) if (e.residual > 0) {
                if (label_[e.to] > label_[v] + 1) {
                    label_[e.to] = label_[v] + 1;
                    if (e.to == t) return true;
                    queue.emplace(e.to);
                }
            }
        }
        return false;
    }

    bool admissible(u32 v, const Edge& e) const noexcept {
        return e.residual > 0 and label_[v] + 1 == label_[e.to];
    }

    inline void flow(Edge& e, Cap f) {
        e.residual -= f;
        g_[e.to][e.rev].residual += f;
    }

    Cap dfs(u32 v, u32 t, Cap up) {
        if (v == t) return up;
        Cap res{};
        for (u32& i{cur_[v]} ; i < g_[v].size() ; i++) {
            if (!admissible(v, g_[v][i])) continue;
            Cap f{dfs(g_[v][i].to, t, std::min(g_[v][i].residual, up - res))};
            if (f == 0) continue;
            flow(g_[v][i], f);
            res += f;
            if (res == up) return res;
        }
        return res;
    }

    Cap primalStep(u32 s, u32 t) {
        std::fill(cur_.begin(), cur_.end(), 0u);
        cur_[t] = g_[t].size();
        Cap res{};
        while (true) {
            Cap f{dfs(s, t, std::numeric_limits<Cap>::max())};
            if (f == 0) break;
            res += f;
        }
        return res;
    }

    const Edge& edge(u32 i) const noexcept {
        return g_[edges_[i].first()][edges_[i].second()];
    }
    const Edge& reverse(u32 i) const noexcept {
        const Edge& e{edge(i)};
        return g_[e.to][e.rev];
    }

public:
    Dinic() = default;
    Dinic(u32 n, u32 m = 0u) 
        : n_{n}, m_{m}, g_(n), edges_{}, label_(n), cur_(n) {
        g_.shrink_to_fit();
        label_.shrink_to_fit();
        cur_.shrink_to_fit();
        edges_.reserve(m);
    }

    u32 addEdge(u32 u, u32 v, const Cap& cap) {
        assert(u < size());
        assert(v < size());
        u32 id{static_cast<u32>(g_[u].size())};
        u32 revId{u == v ? id + 1 : static_cast<u32>(g_[v].size())};
        u32 res{static_cast<u32>(edges_.size())};
        edges_.emplace_back(u, id);
        g_[u].emplace_back(v, revId, cap);
        g_[v].emplace_back(u, id, Cap{});
        m_++;
        return res;
    }

    const Cap& flowed(u32 id) const noexcept {
        assert(id < edgeNumber());
        return reverse(id).residual;
    }
    const Cap& residual(u32 id) const noexcept {
        assert(id < edgeNumber());
        return edge(id).residual;
    }
    const Cap& capacity(u32 id) const noexcept {
        assert(id < edgeNumber());
        return edge(id).residual + reverse(id).residual;
    }
    const u32& from(u32 id) const noexcept {
        assert(id < edgeNumber());
        return edges_[id].first();
    }
    const u32& to(u32 id) const noexcept {
        assert(id < edgeNumber());
        return edge(id).to;
    }

    Cap flow(u32 s, u32 t) {
        assert(s < size());
        assert(t < size()); 
        Cap res{};
        while (dualStep(s, t)) {
            res += primalStep(s, t);
        }
        return res;
    }

    std::vector<bool> cut(u32 s) {
        std::vector<bool> res(size());
        res[s] = true;
        std::queue<u32> queue{ { s } };
        while (queue.size()) {
            u32 v{queue.front()};
            queue.pop();
            for (const auto& e : g_[v]) if (e.residual > 0 and !res[e.to]) {
                res[e.to] = true;
                queue.emplace(e.to);
            }
        }
        return res;
    }    
};

} // namespace zawa
#line 5 "Test/AtCoder/abc318_g.test.cpp"

#line 8 "Test/AtCoder/abc318_g.test.cpp"

int main() {
    using namespace zawa;
    SetFastIO();
    int n, m; std::cin >> n >> m;
    int a, b, c; std::cin >> a >> b >> c;
    a--; b--; c--;
    Dinic<int> mf(2 * n + 2, 2 + n + 2 * m);
    for (int i{} ; i < n ; i++) {
        mf.addEdge(i, i + n, (i == b ? 2 : 1));
    }
    for (int _{} ; _ < m ; _++) {
        int u, v; std::cin >> u >> v;
        u--; v--;
        mf.addEdge(n + u, v, 1);
        mf.addEdge(n + v, u, 1);
    }
    int s{2 * n}, t{s + 1};
    mf.addEdge(s, b, 2);
    mf.addEdge(n + a, t, 1);
    mf.addEdge(n + c, t, 1);
    int flow{mf.flow(s, t)};
    assert(flow == 1 or flow == 2);
    bool ans{flow == 2};
    std::cout << (ans ? "Yes" : "No") << '\n';
}
Back to top page