結果

問題 No.1123 Afforestation
ユーザー 0w10w1
提出日時 2020-12-30 14:03:12
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,617 bytes
コンパイル時間 2,596 ms
コンパイル使用メモリ 213,492 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-16 12:22:46
合計ジャッジ時間 8,733 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
11,136 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 68 ms
5,376 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
testcase_92 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T = int, T INF = 0x3f3f3f3f>
struct EKarp {
  int n;
  vector<vector<T>> capacity;
  vector<vector<int>> adj;

  EKarp(int _n) : n(_n), capacity(_n, vector<T>(_n)), adj(_n) {}

  void addEdge(T w, int u, int v) {
    adj[u].push_back(v);
    adj[v].push_back(u);
    capacity[u][v] += w;
  }

  T bfs(int s, int t, vector<int> &parent) {
    fill(parent.begin(), parent.end(), -1);
    parent[s] = -2;
    queue<pair<int, int>> que;
    que.push({ s, INF });
    while (que.size()) {
      auto [cur, flow] = que.front();
      que.pop();
      for (int nxt : adj[cur]) {
        if (parent[nxt] == -1 && capacity[cur][nxt]) {
          parent[nxt] = cur;
          T nflow = min(flow, capacity[cur][nxt]);
          if (nxt == t) return nflow;
          que.push({ nxt, nflow });
        }
      }
    }
    return (T) 0;
  }

  T maxflow(int s, int t) {
    T flow = 0;
    vector<int> parent(n);
    T nflow;
    while (nflow = bfs(s, t, parent)) {
      flow += nflow;
      int cur = t;
      while (cur != s) {
        int pre = parent[cur];
        capacity[pre][cur] -= nflow;
        capacity[cur][pre] += nflow;
        cur = pre;
      }
    }
    return flow;
  }
};

int main() {
  ios::sync_with_stdio(false);

  int H, W; {
    cin >> H >> W;
  }

  vector<int> A(H); {
    for (int i = 0; i < H; ++i) cin >> A[i];
  }

  vector<int> B(W); {
    for (int i = 0; i < W; ++i) cin >> B[i];
  }

  int K; {
    cin >> K;
  }

  vector<int> X(K), Y(K); {
    for (int i = 0; i < K; ++i) cin >> X[i] >> Y[i], --X[i], --Y[i];
  }

  set<pair<int, int>> badBag; {
    for (int i = 0; i < K; ++i) badBag.emplace(X[i], Y[i]);
  }

  EKarp ekarp(H + W + 2); {
    for (int i = 0; i < H; ++i) ekarp.addEdge(A[i], H + W, i);
    for (int i = 0; i < W; ++i) ekarp.addEdge(B[i], H + i, H + W + 1);
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        int w = !badBag.count(make_pair(i, j));
        ekarp.addEdge(w, i, H + j);
      }
    }
  }

  int mf = ekarp.maxflow(H + W, H + W + 1);
  int sumA = accumulate(A.begin(), A.end(), 0);
  int sumB = accumulate(B.begin(), B.end(), 0);

  if (mf != max(sumA, sumB)) {
    cout << ":(" << endl;
  } else {
    cout << "Yay!" << endl;
    vector<string> res(H, string(W, '.')); {
      for (int i = 0; i < K; ++i) {
        res[X[i]][Y[i]] = 'x';
      }
      for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
          if (ekarp.capacity[H + j][i] == 1) res[i][j] = 'o';
        }
      }
    }
    for (int i = 0; i < H; ++i) cout << res[i] << "\n";
  }

}

0