結果
問題 | No.1123 Afforestation |
ユーザー |
![]() |
提出日時 | 2020-07-23 13:06:23 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 373 ms / 2,500 ms |
コード長 | 4,680 bytes |
コンパイル時間 | 2,573 ms |
コンパイル使用メモリ | 214,952 KB |
最終ジャッジ日時 | 2025-01-12 04:12:54 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 90 |
ソースコード
#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 LOCALvoid 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))#endifstruct 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;}