結果

問題 No.1123 Afforestation
ユーザー satashunsatashun
提出日時 2020-07-23 13:06:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 380 ms / 2,500 ms
コード長 4,680 bytes
コンパイル時間 2,675 ms
コンパイル使用メモリ 219,780 KB
実行使用メモリ 119,984 KB
最終ジャッジ日時 2023-09-05 22:10:36
合計ジャッジ時間 14,250 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 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 10 ms
10,264 KB
testcase_05 AC 75 ms
36,972 KB
testcase_06 AC 325 ms
119,604 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 6 ms
5,732 KB
testcase_12 AC 42 ms
23,768 KB
testcase_13 AC 338 ms
119,848 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 11 ms
10,688 KB
testcase_20 AC 95 ms
36,896 KB
testcase_21 AC 317 ms
119,644 KB
testcase_22 AC 377 ms
119,600 KB
testcase_23 AC 380 ms
119,932 KB
testcase_24 AC 360 ms
119,844 KB
testcase_25 AC 339 ms
119,984 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 3 ms
4,376 KB
testcase_40 AC 3 ms
4,428 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 6 ms
5,704 KB
testcase_43 AC 12 ms
10,352 KB
testcase_44 AC 19 ms
12,648 KB
testcase_45 AC 20 ms
12,640 KB
testcase_46 AC 21 ms
13,328 KB
testcase_47 AC 25 ms
13,332 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 3 ms
4,376 KB
testcase_51 AC 3 ms
4,376 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 2 ms
4,376 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 3 ms
4,380 KB
testcase_56 AC 3 ms
4,376 KB
testcase_57 AC 3 ms
4,380 KB
testcase_58 AC 6 ms
5,416 KB
testcase_59 AC 11 ms
10,440 KB
testcase_60 AC 22 ms
12,488 KB
testcase_61 AC 21 ms
12,472 KB
testcase_62 AC 23 ms
13,248 KB
testcase_63 AC 25 ms
13,268 KB
testcase_64 AC 1 ms
4,376 KB
testcase_65 AC 2 ms
4,376 KB
testcase_66 AC 2 ms
4,380 KB
testcase_67 AC 2 ms
4,380 KB
testcase_68 AC 3 ms
4,376 KB
testcase_69 AC 4 ms
4,940 KB
testcase_70 AC 6 ms
5,424 KB
testcase_71 AC 12 ms
10,428 KB
testcase_72 AC 2 ms
4,376 KB
testcase_73 AC 2 ms
4,376 KB
testcase_74 AC 1 ms
4,380 KB
testcase_75 AC 2 ms
4,380 KB
testcase_76 AC 2 ms
4,380 KB
testcase_77 AC 2 ms
4,376 KB
testcase_78 AC 2 ms
4,376 KB
testcase_79 AC 2 ms
4,376 KB
testcase_80 AC 1 ms
4,380 KB
testcase_81 AC 2 ms
4,376 KB
testcase_82 AC 2 ms
4,376 KB
testcase_83 AC 2 ms
4,376 KB
testcase_84 AC 244 ms
119,640 KB
testcase_85 AC 250 ms
119,752 KB
testcase_86 AC 1 ms
4,376 KB
testcase_87 AC 2 ms
4,376 KB
testcase_88 AC 230 ms
103,964 KB
testcase_89 AC 232 ms
103,900 KB
testcase_90 AC 1 ms
4,376 KB
testcase_91 AC 1 ms
4,376 KB
testcase_92 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct FordFulkerson {
    static constexpr int INF = TEN(9);

    struct edge {
        int to, cap, rev;
    };

    int n;
    VV<edge> g;
    V<bool> used;

    FordFulkerson(int n) : n(n), g(n), used(n) {}

    void add_edge(int from, int to, int cap, int rcap) {
        g[from].push_back((edge){to, cap, (int)g[to].size()});
        g[to].push_back((edge){from, rcap, (int)g[from].size() - 1});
    }

    int dfs(int v, int t, int f) {
        if (v == t) return f;
        used[v] = true;
        for (auto& e : g[v]) {
            if (!used[e.to] && e.cap > 0) {
                int d = dfs(e.to, t, min(f, e.cap));
                if (d > 0) {
                    e.cap -= d;
                    g[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

    int max_flow(int s, int t) {
        int flow = 0;
        while (1) {
            fill(used.begin(), used.end(), false);
            int f = dfs(s, t, INF);
            if (f == 0) return flow;
            flow += f;
        }
    }
};

int state[2010][2010];

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

    int H, W;
    cin >> H >> W;
    V<ll> A(H), B(W), ga, gb;
    rep(i, H) cin >> A[i];
    rep(i, W) cin >> B[i];
    ga = A, gb = B;

    ll s1 = 0, s2 = 0;
    rep(i, H) s1 += A[i];
    rep(i, W) s2 += B[i];
    if (s1 != s2) {
        cout << ":(" << endl;
        return 0;
    }

    {
        V<int> xid(H);
        iota(ALL(xid), 0);
        sort(ALL(xid), [&](int a, int b) { return A[a] > A[b]; });

        for (int x : xid) {
            V<int> yid(W);
            iota(ALL(yid), 0);
            sort(ALL(yid), [&](int a, int b) { return B[a] > B[b]; });

            int p = 0;
            while (A[x]) {
                if (B[yid[p]] == 0) {
                    cout << ":(" << endl;
                    return 0;
                }
                state[x][yid[p]] = 1;
                B[yid[p]]--;
                p++;
                A[x]--;
            }
        }
    }

    V<string> ans(H, string(W, '.'));

    int K;
    cin >> K;
    int dec = 0;

    rep(i, K) {
        int x, y;
        cin >> x >> y;
        --x, --y;
        if (state[x][y] == 1) {
            A[x]++;
            B[y]++;
            dec++;
        }
        state[x][y] = -1;
        ans[x][y] = 'x';
    }

    int node_cnt = H + W + 2;
    int s = node_cnt - 2, t = node_cnt - 1;
    FordFulkerson flow(node_cnt);

    rep(i, H) flow.add_edge(s, i, A[i], ga[i] - A[i]);
    rep(j, W) flow.add_edge(H + j, t, B[j], gb[j] - B[j]);
    rep(i, H) {
        rep(j, W) {
            if (state[i][j] != -1) {
                flow.add_edge(i, j + H, 1 - state[i][j], state[i][j]);
            }
        }
    }

    auto fl = flow.max_flow(s, t);
    if (fl != dec) {
        cout << ":(" << endl;
        return 0;
    }

    rep(i, H) {
        for (auto e : flow.g[i]) {
            if (e.to >= H && e.to < H + W && state[i][e.to - H] != -1 &&
                e.cap == 0) {
                ans[i][e.to - H] = 'o';
            }
        }
    }

    cout << "Yay!" << endl;
    rep(i, H) cout << ans[i] << '\n';

    return 0;
}
0