結果

問題 No.1123 Afforestation
ユーザー 👑 hitonanodehitonanode
提出日時 2020-07-22 23:01:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 10,543 bytes
コンパイル時間 3,696 ms
コンパイル使用メモリ 235,324 KB
実行使用メモリ 24,492 KB
最終ジャッジ日時 2023-09-05 03:31:47
合計ジャッジ時間 20,543 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 6 ms
4,376 KB
testcase_05 AC 51 ms
8,544 KB
testcase_06 AC 268 ms
23,268 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 270 ms
23,780 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 270 ms
24,416 KB
testcase_22 AC 271 ms
24,412 KB
testcase_23 AC 271 ms
24,444 KB
testcase_24 AC 271 ms
24,408 KB
testcase_25 AC 271 ms
24,492 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 14 ms
19,464 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 14 ms
19,156 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 WA -
testcase_35 AC 3 ms
4,380 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 AC 1 ms
4,376 KB
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 WA -
testcase_72 AC 2 ms
4,380 KB
testcase_73 AC 2 ms
4,376 KB
testcase_74 AC 1 ms
4,376 KB
testcase_75 AC 1 ms
4,380 KB
testcase_76 AC 2 ms
4,376 KB
testcase_77 AC 2 ms
4,380 KB
testcase_78 AC 2 ms
4,380 KB
testcase_79 AC 2 ms
4,380 KB
testcase_80 AC 2 ms
4,376 KB
testcase_81 AC 1 ms
4,376 KB
testcase_82 AC 2 ms
4,380 KB
testcase_83 AC 2 ms
4,376 KB
testcase_84 AC 340 ms
23,556 KB
testcase_85 AC 344 ms
24,492 KB
testcase_86 AC 2 ms
4,380 KB
testcase_87 AC 1 ms
4,376 KB
testcase_88 AC 26 ms
22,992 KB
testcase_89 AC 29 ms
23,816 KB
testcase_90 AC 2 ms
4,380 KB
testcase_91 AC 2 ms
4,376 KB
testcase_92 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template <typename V, typename T> void ndfill(V &x, const T &val) { x = val; }
template <typename V, typename T> void ndfill(vector<V> &vec, const T &val) { for (auto &v : vec) ndfill(v, val); }
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> srtunq(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;

bool muri()
{
    puts(":(");
    exit(0);
}

// MaxFlow (Dinic algorithm)
template <typename T>
struct MaxFlow
{
    struct edge { int to; T cap; int rev; };
    std::vector<std::vector<edge>> edges;
    std::vector<int> level;  // level[i] = distance between vertex S and i (Default: -1)
    std::vector<int> iter;  // iteration counter, used for Dinic's DFS
    std::vector<int> used;  // Used for Ford-Fulkerson's Algorithm

    void bfs(int s)
    {
        level.assign(edges.size(), -1);
        std::queue<int> q;
        level[s] = 0;
        q.push(s);
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (edge &e : edges[v]) {
                if (e.cap > 0 and level[e.to] < 0) {
                    level[e.to] = level[v] + 1;
                    q.push(e.to);
                }
            }
        }
    }

    T dfs_dinic(int v, int goal, T f)
    {
        if (v == goal) return f;
        for (int &i = iter[v]; i < (int)edges[v].size(); i++) {
            edge &e = edges[v][i];
            if (e.cap > 0 and level[v] < level[e.to]) {
                T d = dfs_dinic(e.to, goal, std::min(f, e.cap));
                if (d > 0) {
                    e.cap -= d;
                    edges[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

    T dfs_ff(int v, int goal, T f)
    {
        if (v == goal) return f;
        used[v] = true;
        for (edge &e : edges[v]) {
            if (e.cap > 0 && !used[e.to]) {
                T d = dfs_ff(e.to, goal, std::min(f, e.cap));
                if (d > 0) {
                    e.cap -= d;
                    edges[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

public:
    MaxFlow(int N) { edges.resize(N); }
    void add_edge(int from, int to, T capacity)
    {
        edges[from].push_back(edge{to, capacity, (int)edges[to].size()});
        edges[to].push_back(edge{from, (T)0, (int)edges[from].size() - 1});
    }

    // Dinic algorithm
    // Complexity: O(VE)
    T Dinic(int s, int t)
    {
        constexpr T INF = std::numeric_limits<T>::max();
        T flow = 0;
        while (true) {
            bfs(s);
            if (level[t] < 0) return flow;
            iter.assign(edges.size(), 0);
            T f;
            while ((f = dfs_dinic(s, t, INF)) > 0) flow += f;
        }
    }

    // Ford-Fulkerson algorithm
    // Complexity: O(EF)
    T FF(int s, int t)
    {
        constexpr T INF = std::numeric_limits<T>::max();
        T flow = 0;
        while (true) {
            used.assign(edges.size(), 0);
            T f = dfs_ff(s, t, INF);
            if (f == 0) return flow;
            flow += f;
        }
    }

    void back_flow(int s, int t, int s_e, int t_e, T capacity_reduce)
    {
        int i;
        for (i=0; edges[s_e][i].to != t_e; ) i++;
        edge &e = edges[s_e][i];

        if (capacity_reduce <= e.cap) {
            e.cap -= capacity_reduce;
        }
        else {
            T flow = capacity_reduce - e.cap;
            e.cap = 0;
            edges[e.to][e.rev].cap -= flow;

            T f_sum = 0;
            while (f_sum != flow) {
                used.assign(edges.size(), 0);
                f_sum += dfs_ff(t, t_e, flow - f_sum);
            }
            f_sum = 0;
            while (f_sum != flow) {
                used.assign(edges.size(), 0);
                f_sum += dfs_ff(s_e, s, flow - f_sum);
            }
        }
    }
};

int main()
{
    int H, W;
    cin >> H >> W;
    vector<int> A(H), B(W);
    cin >> A >> B;
    int K;
    cin >> K;
    vector<int> X(K), Y(K);
    vector<vector<int>> bad(H, vector<int>(W));
    REP(i, K)
    {
        cin >> X[i] >> Y[i];
        X[i]--, Y[i]--;
        bad[X[i]][Y[i]] = 1;
    }
    const vector<int> xu = srtunq(X), yu = srtunq(Y);
    vector<int> xsp(H), ysp(W);
    for (auto x : xu) xsp[x] = 1;
    for (auto y : yu) ysp[y] = 1;
    int P = xu.size(), Q = yu.size();

    int atot = accumulate(ALL(A), 0), btot = accumulate(ALL(B), 0);
    if (atot != btot) muri();
    int allflow = atot;

    int T = 1 + P + 1 + Q + 1;
    MaxFlow<int> flow(T + 1);
    REP(i, P) flow.add_edge(0, i + 1, A[xu[i]]), atot -= A[xu[i]], flow.add_edge(i + 1, P + Q + 2, W - yu.size());
    flow.add_edge(0, P + 1, atot);
    REP(i, Q) flow.add_edge(P + 2 + i, T, B[yu[i]]), btot -= B[yu[i]], flow.add_edge(P + 1, P + 2 + i, H - xu.size());
    flow.add_edge(P + Q + 2, T, btot);
    flow.add_edge(P + 1, P + Q + 2, (H - xu.size()) * (W - yu.size()));
    REP(i, P) REP(j, Q) if (!bad[xu[i]][yu[j]]) flow.add_edge(i + 1, P + 2 + j, 1);

    if (allflow != flow.Dinic(0, T)) muri();

    vector<string> ret(H, string(W, '.'));
    REP(i, H) REP(j, W) if (bad[i][j]) ret[i][j] = 'x';
    REP(i, P) for (auto e : flow.edges[1 + i])
    {
        if (e.to >= P + 2 and e.to < P + Q + 2 and e.cap == 0)
        {
            int x = xu.at(i);
            int y = yu.at(e.to - P - 2);
            A[x]--, B[y]--, ret[x][y] = 'o';
        }
    }

    {
        int T = 1 + P + W;
        MaxFlow<int> fx(T + 1);
        REP(i, P) if (A[xu[i]]) fx.add_edge(0, 1 + i, A[xu[i]]);
        REP(i, P) REP(y, W) if (!ysp[y] and ret[xu[i]][y] == '.') fx.add_edge(1 + i, 1 + P + y, 1);
        REP(y, W) if (!ysp[y] and B[y]) fx.add_edge(1 + P + y, T, B[y]);
        fx.Dinic(0, T);
        REP(i, P) for (auto e : fx.edges[1 + i])
        {
            if (e.to >= P + 1 and e.to <= P + W and e.cap == 0)
            {
                int x = xu.at(i);
                int y = e.to - P - 1;
                A[x]--, B[y]--, ret[x][y] = 'o';
            }
        }
    }

    {
        int T = 1 + Q + H;
        MaxFlow<int> fy(T + 1);
        REP(i, Q) fy.add_edge(0, 1 + i, B[yu[i]]);
        REP(j, Q) REP(x, H) if (ret[x][yu[j]] == '.') fy.add_edge(1 + j, 1 + Q + x, 1);
        REP(x, H) if (!xsp[x] and A[x]) fy.add_edge(1 + Q + x, T, A[x]);
        REP(i, Q) for (auto e : fy.edges[1 + i])
        {
            if (e.to >= Q + 1 and e.to <= Q + H and e.cap == 0)
            {
                int y = yu.at(i);
                int x = e.to - Q - 1;
                A[x]--, B[y]--, ret[x][y] = 'o';
            }
        }
    }
    priority_queue<pint> pqx, pqy;
    REP(i, A.size()) if (A[i]) pqx.push(pint(A[i], i));
    REP(i, B.size()) if (B[i]) pqy.push(pint(B[i], i));

    while (pqx.size())
    {
        auto [sz, x] = pqx.top();
        pqx.pop();
        vector<pint> q;
        while (sz)
        {
            sz--;
            if (pqy.empty()) muri();
            auto [s_, y] = pqy.top();
            pqy.pop();
            ret[x][y] = 'o';
            s_--;
            if (s_) q.emplace_back(s_, y);
        }
        for (auto p : q) pqy.push(p);
    }
    cout << "Yay!\n";
    for (auto str : ret) cout << str << '\n';
}
0