結果

問題 No.459 C-VS for yukicoder
ユーザー maimai
提出日時 2018-09-25 09:49:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 18,177 bytes
コンパイル時間 3,402 ms
コンパイル使用メモリ 236,892 KB
実行使用メモリ 19,836 KB
最終ジャッジ日時 2024-04-10 08:11:28
合計ジャッジ時間 6,984 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 78 ms
19,836 KB
testcase_22 AC 65 ms
18,028 KB
testcase_23 AC 66 ms
18,200 KB
testcase_24 AC 27 ms
9,632 KB
testcase_25 AC 14 ms
6,940 KB
testcase_26 AC 10 ms
6,944 KB
testcase_27 AC 10 ms
6,940 KB
testcase_28 AC 13 ms
6,940 KB
testcase_29 AC 21 ms
8,432 KB
testcase_30 AC 10 ms
6,940 KB
testcase_31 AC 16 ms
6,944 KB
testcase_32 AC 3 ms
6,944 KB
testcase_33 AC 22 ms
7,360 KB
testcase_34 AC 24 ms
7,616 KB
testcase_35 AC 4 ms
6,944 KB
testcase_36 AC 20 ms
7,180 KB
testcase_37 AC 4 ms
6,940 KB
testcase_38 AC 20 ms
7,272 KB
testcase_39 AC 4 ms
6,940 KB
testcase_40 AC 14 ms
6,944 KB
testcase_41 AC 3 ms
6,944 KB
testcase_42 AC 13 ms
6,944 KB
testcase_43 AC 20 ms
6,948 KB
testcase_44 AC 5 ms
6,940 KB
testcase_45 AC 5 ms
6,940 KB
testcase_46 AC 4 ms
6,944 KB
testcase_47 AC 3 ms
6,944 KB
testcase_48 AC 20 ms
6,940 KB
testcase_49 AC 4 ms
6,944 KB
testcase_50 AC 20 ms
6,944 KB
testcase_51 AC 4 ms
6,944 KB
testcase_52 AC 10 ms
6,940 KB
testcase_53 AC 4 ms
6,940 KB
testcase_54 AC 20 ms
7,160 KB
testcase_55 AC 6 ms
6,944 KB
testcase_56 AC 13 ms
6,944 KB
testcase_57 AC 4 ms
6,940 KB
testcase_58 AC 23 ms
7,428 KB
testcase_59 AC 6 ms
6,940 KB
testcase_60 AC 10 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#include "bits/stdc++.h"

using namespace std;
using ll = long long int;

#define debugos cout
#define debug(v) {printf("L%d %s > ",__LINE__,#v);debugos<<(v)<<endl;}
#define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto e:(v)){debugos<<e<<" ";}debugos<<endl;}
#define debuga(m,w) {printf("L%d %s > ",__LINE__,#m);for(int x=0;x<(w);x++){debugos<<(m)[x]<<" ";}debugos<<endl;}
#define debugaa(m,h,w) {printf("L%d %s >\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){debugos<<(m)[y][x]<<" ";}debugos<<endl;}}
#define ALL(v) (v).begin(),(v).end()
#define repeat(cnt,l) for(remove_reference<remove_const<decltype(l)>::type>::type cnt=0;(cnt)<(l);++(cnt))
#define rrepeat(cnt,l) for(auto cnt=(l)-1;0<=(cnt);--(cnt))
#define iterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);++(cnt))
#define diterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);--(cnt))
const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L;
inline void assert_call(bool assertion, function<void()> f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } }
template<typename T1, typename T2> inline ostream& operator <<(ostream &o, const pair<T1, T2> p) { o << '(' << p.first << ':' << p.second << ')'; return o; }
template<typename Vec> inline ostream& _ostream_vecprint(ostream& os, const Vec& a) {
    os << '['; for (const auto& e : a) os << ' ' << e << ' '; os << ']'; return os;
}
template<typename T> inline ostream& operator<<(ostream& o, const vector<T>& v) { return _ostream_vecprint(o, v); }
template<typename T, size_t S> inline ostream& operator<<(ostream& o, const array<T, S>& v) { return _ostream_vecprint(o, v); }
template<typename T> inline T& maxset(T& to, const T& val) { return to = max(to, val); }
template<typename T> inline T& minset(T& to, const T& val) { return to = min(to, val); }
void bye(string s, int code = 0) { cout << s << endl; exit(code); }
mt19937_64 randdev(8901016);
template<typename T> inline T rand(T l, T h) { return uniform_int_distribution<T>(l, h)(randdev); }
template<> inline double rand<double>(double l, double h) { return uniform_real_distribution<double>(l, h)(randdev); }
template<> inline float rand<float>(float l, float h) { return uniform_real_distribution<float>(l, h)(randdev); }

#if defined(_WIN32) || defined(_WIN64)
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#elif defined(__GNUC__)
#else
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#endif
namespace {
#define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E)
    class MaiScanner {
    public:
        template<typename T> void input_integer(T& var) noexcept {
            var = 0; T sign = 1;
            int cc = getchar_unlocked();
            for (; cc < '0' || '9' < cc; cc = getchar_unlocked())
                if (cc == '-') sign = -1;
            for (; '0' <= cc && cc <= '9'; cc = getchar_unlocked())
                var = (var << 3) + (var << 1) + cc - '0';
            var = var * sign;
        }
        inline int c() noexcept { return getchar_unlocked(); }
        inline MaiScanner& operator>>(int& var) noexcept { input_integer<int>(var); return *this; }
        inline MaiScanner& operator>>(long long& var) noexcept { input_integer<long long>(var); return *this; }
        inline MaiScanner& operator>>(string& var) {
            int cc = getchar_unlocked();
            for (; !isvisiblechar(cc); cc = getchar_unlocked());
            for (; isvisiblechar(cc); cc = getchar_unlocked())
                var.push_back(cc);
            return *this;
        }
        template<typename IT> void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; }
    };
    class MaiPrinter {
    public:
        template<typename T>
        void output_integer(T var) noexcept {
            if (var == 0) { putchar_unlocked('0'); return; }
            if (var < 0)
                putchar_unlocked('-'),
                var = -var;
            char stack[32]; int stack_p = 0;
            while (var)
                stack[stack_p++] = '0' + (var % 10),
                var /= 10;
            while (stack_p)
                putchar_unlocked(stack[--stack_p]);
        }
        inline MaiPrinter& operator<<(char c) noexcept { putchar_unlocked(c); return *this; }
        inline MaiPrinter& operator<<(int var) noexcept { output_integer<int>(var); return *this; }
        inline MaiPrinter& operator<<(long long var) noexcept { output_integer<long long>(var); return *this; }
        inline MaiPrinter& operator<<(char* str_p) noexcept { while (*str_p) putchar_unlocked(*(str_p++)); return *this; }
        inline MaiPrinter& operator<<(const string& str) {
            const char* p = str.c_str();
            const char* l = p + str.size();
            while (p < l) putchar_unlocked(*p++);
            return *this;
        }
        template<typename IT> void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; }
    };
}
MaiScanner scanner;
MaiPrinter printer;


class Graph {
public:
    size_t n;
    vector<vector<int>> vertex_to;

    Graph(size_t n = 1) :n(n), vertex_to(n) {}

    inline size_t size() const { return n; }
    void resize(size_t _n) { vertex_to.resize(n = _n); }
    void connect(int from, int to) {
        vertex_to[(size_t)from].emplace_back(to);
        vertex_to[(size_t)to].emplace_back(from);
    }
};


template <typename T>
class SparseTable {
public:
    const int size;
    vector<int> log2;
    vector<T> data;
    vector<T> dp;

    SparseTable(int size) :size(size), log2(size + 1), data(size) {
        // for fast calculate log2
        for (int i = 2; i <= size; ++i) {
            log2[i] = log2[i >> 1] + 1;
        }

        dp.resize(size*(log2[size] + 1));
    }

    inline T& operator[](size_t i) { return data[i]; }
    inline T operator[](size_t i)const { return data[i]; }

    void build() {
        int l, i, f, b;
        for (i = 0; i < size; i++) {
            dp[i] = i;
        }
        for (l = 1; (1 << l) <= size; l++) {
            for (i = 0; i + (1 << l) <= size; i++) {
                f = dp[i + size * (l - 1)];
                b = dp[(i + (1 << (l - 1))) + size * (l - 1)];

                dp[i + size * l] = (data[f] <= data[b]) ? f : b; // minimum
            }
        }
    }

    // range [l,r)
    int getminrangeIdx(int l, int r) const {
        int lg = log2[r - l];
        int i1 = dp[l + size * lg];
        int i2 = dp[r - (1 << lg) + size * lg];
        return (data[i1] <= data[i2]) ? i1 : i2; // minimum
    }
};


class DGraph {
public:
    size_t n;
    vector<vector<int>> vertex_to;
    vector<vector<int>> vertex_from;

    DGraph(size_t n = 1) :n(n), vertex_to(n), vertex_from(n) {}

    inline size_t size() const { return n; }
    void resize(size_t _n) { n = _n; vertex_to.resize(_n); vertex_from.resize(_n); }
    void connect(int from, int to) {
        vertex_to[(size_t)from].emplace_back(to);
        vertex_from[(size_t)to].emplace_back(from);
    }
};

class Unionfind {
public:
    vector<int> data;
    Unionfind(size_t size) : data(size, -1) { }
    bool connect(size_t x, size_t y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = (int)x;
        }
        return x != y;
    }
    inline bool same(size_t x, size_t y) {
        return root(x) == root(y);
    }
    inline size_t root(size_t x) {
        return (size_t)(data[x] < 0 ? x : data[x] = root(data[x]));
    }
    inline int size(size_t x) {
        return -data[root(x)];
    }
};




class LCATable {
    vector<int> visited_;
    vector<int> visited_inv_;
    SparseTable<int> depth_;

public:
    LCATable(const Graph& g, int root = 0) :visited_(g.n * 2), visited_inv_(g.n), depth_(g.n * 2) { build(g, root); }

    int _tour_dfs(const Graph& g, int idx, int from = -1, int step = 0, int dep = 0) {
        depth_[step] = dep;
        visited_inv_[idx] = step;
        visited_[step] = idx;

        for (int to : g.vertex_to[idx]) {
            if (to == from) continue;
            step = _tour_dfs(g, to, idx, ++step, dep + 1);
            depth_[step] = dep;
            visited_[step] = idx;
        }
        return ++step;
    }

    inline void build(const Graph& g, int root = 0) {
        _tour_dfs(g, root);
        depth_.build();
    }

    inline int operator()(int u, int v) {
        return visited_inv_[u] <= visited_inv_[v] ?
            visited_[depth_.getminrangeIdx(visited_inv_[u], visited_inv_[v])] : operator()(v, u);
    }
};



class DGraphF {
public:
    typedef int cap_t;
    size_t n_;
    struct Arc {
        int from, to;
        // 残量
        cap_t left;
        // 容量
        cap_t cap;

        Arc(int from = 0, int to = 0, cap_t w = 1) :from(from), to(to), left(w), cap(w) {}
        inline bool operator<(const Arc& a) const { return (left != a.left) ? left < a.left : (left < a.left) | (cap < a.cap) | (from < a.from) | (to < a.to); }
        inline bool operator==(const Arc& a) const { return (from == a.from) && (to == a.to) && (left == a.left) && (cap == a.cap); }
    };
    vector<vector<int>> vertex_to;
    vector<vector<int>> vertex_from;
    vector<Arc> edges;

    DGraphF(int n = 1) :n_(n), vertex_to(n), vertex_from(n) { }

    void connect(int from, int to, cap_t left) {
        vertex_to[(size_t)from].push_back((int)edges.size()); // toto
        vertex_from[(size_t)to].push_back((int)edges.size()); // fromfrom
        edges.emplace_back(from, to, left);
    }

    inline size_t size() const { return n_; }
};


void dinic(DGraphF &graph, vector<DGraphF::cap_t>& result, int i_source, int i_sink) {
    assert(i_source != i_sink);

    result.resize(graph.n_);
    vector<int> dist(graph.n_);
    queue<int> q;
    vector<int> flag(graph.n_);

    static function<DGraphF::cap_t(int, int, DGraphF::cap_t)> _dfs = [&](int u, int i_sink, DGraphF::cap_t mini) {
        // DAG
        // TODO: 経路再利用
        if (i_sink == u) return mini;
        if (flag[u]) return (DGraphF::cap_t) - 1;
        flag[u] = true;

        DGraphF::cap_t sumw = 0;
        bool term = true;
        for (int e : graph.vertex_to[u]) {
            auto& edge = graph.edges[e];
            if (edge.left > 0 && dist[u] > dist[edge.to]) {
                DGraphF::cap_t w = (mini < 0) ? edge.left : min(edge.left, mini);

                w = _dfs(edge.to, i_sink, w);
                if (w == -1) continue;
                edge.left -= w;
                result[edge.to] += w;

                sumw += w;
                mini -= w;
                term = false;
                flag[u] = false; // TODO: 末尾では? 

                if (mini == 0) return sumw;
            }
        }
        for (int e : graph.vertex_from[u]) {
            auto& edge = graph.edges[e];
            if (edge.cap > edge.left && dist[u] > dist[edge.from]) {
                DGraphF::cap_t w = (mini < 0) ? (edge.cap - edge.left) : min(edge.cap - edge.left, mini);

                w = _dfs(edge.from, i_sink, w);
                if (w == -1) continue;
                edge.left += w;
                result[edge.to] -= w;

                sumw += w;
                mini -= w;
                term = false;
                flag[u] = false;
                if (mini == 0) return sumw;
            }
        }
        return term ? (DGraphF::cap_t)(-1) : sumw;
    };


    for (int distbegin = 0; ; distbegin += (int)graph.n_) {

        q.emplace(i_sink); // bfsはsinkからsourceへの距離を計算.
        dist[i_sink] = distbegin + 1;
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (int ie : graph.vertex_from[v]) {
                const auto edge = graph.edges[ie];
                if (0 < edge.left && dist[edge.from] <= distbegin) {
                    dist[edge.from] = dist[v] + 1;
                    q.emplace(edge.from);
                }
            }
            for (int ie : graph.vertex_to[v]) {
                const auto edge = graph.edges[ie];
                if (edge.left < edge.cap && dist[edge.to] <= distbegin) {
                    dist[edge.to] = dist[v] + 1;
                    q.emplace(edge.to);
                }
            }
        }
        fill(flag.begin(), flag.end(), false);

        if (dist[i_source] <= distbegin)
            break;
        else
            result[i_source] += _dfs(i_source, i_sink, -1);
    }
}



// ## 最小流量制限付き最大フロー
// + http://snuke.hatenablog.com/entry/2016/07/10/043918
// + http://yukicoder.me/submissions/137248
// + http://yukicoder.me/submissions/143696
// 
// #### 解説
// 最小流量制限付き最大フローは,普通の最大フローに置き換えることができる.
// 
// 面倒なので,最小流l,最大流hで頂点uから頂点vへ流れる有向辺を(u,v)[l,h]と表記する.
// 
// + s→tな最大最小流量制限付きフローG=(V,E)を考える.最大流量制限付きフローG'を作りたい.
// + 新たに頂点S,Tを作る.
// + (u,v)[c,c+d]がGに存在するとき,G'に(u,v)[0,d],(u,T)[0,c],(S,v)[0,c]を与える.
// + G'に多重辺が出来ることがある.
// + S→T,S→t,s→T,s→tの順に最大流を求める.S,Tに隣接する辺に優先して流すため.
// + S,Tに隣接する辺が全てemptyになっていれば,条件を満たすフローが存在
// + 流量は(u,v)+(u,T)
// 
// 事前に全体の流量が把握出来るならば, #137248のように,S→s,t→Tの辺を作ってS→Tを流せばよい


class FlowMinMax {
public:
    DGraphF graph;
    const int v_source; // vertex of new source
    FlowMinMax(int n) :graph(n + 2), v_source(n) {}

private:
    bool _solve_dinic_edge(map<pair<int, int>, int>& result_edge, int i_source, int i_sink) {

        vector<int> resflow(graph.size(), 0);

        dinic(graph, resflow, v_source, v_source + 1);
        dinic(graph, resflow, v_source, i_sink);
        dinic(graph, resflow, i_source, v_source + 1);
        dinic(graph, resflow, i_source, i_sink);

        for (int e : graph.vertex_from[v_source + 1]) {
            const DGraphF::Arc& a = graph.edges[e];
            if (0 < a.left) return false;
        }
        int flow;
        for (int u = 0; u < graph.size() - 2; u++) {
            for (int ei : graph.vertex_to[u]) { // TODO:最適化の余地あり(らしい)
                const DGraphF::Arc& a = graph.edges[ei]; // u -> v
                if (a.to >= graph.size() - 2) {
                    if (0 < a.left) return false;
                    continue;
                }

                const DGraphF::Arc& c = graph.edges[ei + 1]; // S -> v
                if (a.to != c.to) {
                    flow = a.cap - a.left;
                }
                else {
                    if (0 < c.left) return false;
                    flow = c.cap + a.cap - c.left - a.left;
                }
                if (0 < flow)
                    result_edge[make_pair(u, a.to)] += flow;
            }
        }
        return true;
    }

public:

    void connect(int from, int to, int w_min, int w_max) {

        if (w_max == w_min) {
            graph.connect(v_source, to, w_min);
            graph.connect(from, v_source + 1, w_min);
        }
        else if (w_min == 0) {
            graph.connect(from, to, w_max - w_min);
        }
        else {
            graph.connect(from, v_source + 1, w_min);
            graph.connect(from, to, w_max - w_min);
            graph.connect(v_source, to, w_min);
        }
    }

    inline bool solve_dinic_edge(map<pair<int, int>, int>& result_edge, int i_source, int i_sink) {
        return _solve_dinic_edge(result_edge, i_source, i_sink);
    }

};








// https://yukicoder.me/submissions/172443

int width, height;
int m, n;

int field[10010];
int commands[30010];

int main() {
    int i, j, k;
    int x, y, a, b;

    cin >> height >> width >> n;
    cin.ignore();

    int nblocks = 0;
    // X座標にブロックがいくつ積まれているか、を記録する。
    // stringを保持する必要はない。
    for (y = 0; y < height; y++) {
        string s;
        cin >> s;
        for (x = 0; x < width; x++) {
            field[x] += s[x] == '#';
        }
    }
    for (x = 0; x < width; x++) { nblocks += field[x]; }

    for (i = 0; i < n; i++) {
        scanf("%d", commands + i);
    }


    //          A   _    B    _   C
    //             | | ----> | | ----> [sink]
    // [source] -> | |       | |
    //             | |       | |
    //             |_pack    |_field
    // 
    // A : [1,9] (packは[1,9]個のブロックを持つ)
    // B : [0,3] (packは3x3の容量を持つ)
    // C : [#,#] (x列には#個のブロックが積み上がっている)

    FlowMinMax flow(1 + n + width + 1);

    const int i_source = 0;
    const int i_sink = 1;

    for (i = 0; i < n; i++) {
        // A edge
        flow.connect(i_source, 2 + i, 1, 9);
        int left = commands[i];
        for (j = 0; j < 3; j++) {
            // B edge
            flow.connect(2 + i, 2 + n + left + j, 0, 3);
        }
    }
    for (x = 0; x < width; x++) {
        // C edge
        flow.connect(2 + n + x, i_sink, field[x], field[x]);
    }

    //for (Flow::Arrow& ar : flow.flow.arrow){
    //    if (ar.w_max == 0) continue;
    //    printf("%d -> %d\n",ar.from,ar.to);
    //}

    map<pair<int, int>, int> fl;
    if (!flow.solve_dinic_edge(fl, i_source, i_sink)) {
        abort();
        cout << "warn" << endl;
    }

    //debugv(nagare);
    int hako[3];
    for (i = 0; i < n; ++i) {
        for (j = 0; j < 3; ++j) {
            hako[j] = fl[make_pair(2 + i, 2 + n + commands[i] + j)];
        }
        for (y = 3; 0 < y; --y) {
            for (x = 0; x < 3; ++x) {
                if (y <= hako[x]) {
                    putchar_unlocked('#');
                }
                else {
                    putchar_unlocked('.');
                }
            }
            putchar_unlocked('\n');
        }
    }


    return 0;
}
0