結果

問題 No.1123 Afforestation
ユーザー KoDKoD
提出日時 2020-07-23 23:10:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 13,436 bytes
コンパイル時間 1,953 ms
コンパイル使用メモリ 111,292 KB
実行使用メモリ 392,320 KB
最終ジャッジ日時 2024-06-23 22:11:57
合計ジャッジ時間 20,861 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 RE -
testcase_05 AC 146 ms
64,640 KB
testcase_06 AC 390 ms
182,528 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 WA -
testcase_10 RE -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 402 ms
182,016 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 RE -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 3 ms
6,940 KB
testcase_28 AC 225 ms
7,552 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 116 ms
7,552 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
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 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
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 WA -
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
6,944 KB
testcase_73 AC 2 ms
6,944 KB
testcase_74 AC 2 ms
6,944 KB
testcase_75 RE -
testcase_76 RE -
testcase_77 AC 2 ms
6,940 KB
testcase_78 AC 2 ms
6,940 KB
testcase_79 AC 2 ms
6,944 KB
testcase_80 AC 2 ms
6,940 KB
testcase_81 AC 2 ms
6,944 KB
testcase_82 AC 2 ms
6,940 KB
testcase_83 AC 2 ms
6,944 KB
testcase_84 AC 100 ms
8,064 KB
testcase_85 AC 95 ms
7,808 KB
testcase_86 AC 2 ms
6,940 KB
testcase_87 AC 2 ms
6,940 KB
testcase_88 AC 617 ms
392,192 KB
testcase_89 AC 616 ms
392,320 KB
testcase_90 AC 2 ms
6,940 KB
testcase_91 AC 2 ms
6,940 KB
testcase_92 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"

/**
 * @title Template
 */

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

struct range {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { ++i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { }
  constexpr iterator begin() const noexcept { return l; }
  constexpr iterator end() const noexcept { return r; }
};

struct revrange {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { --i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr iterator begin() const noexcept { return r; }
  constexpr iterator end() const noexcept { return l; }
};

#line 2 "/Users/kodamankod/Desktop/Programming/Library/graph/network.cpp"

#include <cstddef>
#include <cstdint>
#line 8 "/Users/kodamankod/Desktop/Programming/Library/graph/network.cpp"
#include <type_traits>

template <class Edge>
class network {
public:
  using vertex_type = typename Edge::vertex_type;
  using edge_type   = Edge;
  using size_type   = size_t;

  class index_helper {
  private:
    const size_type M_size;
  public:
    explicit index_helper(const size_type size): M_size(size) { }
    vertex_type operator [] (const size_type index) const {
      return to_vertex(index);
    }
    vertex_type to_vertex(const size_type index) const {
      return index + M_size;
    }
    size_type to_index(const vertex_type vert) const {
      return vert - M_size;
    }
  };

protected:
  std::vector<std::vector<edge_type>> M_graph;

public:
  network() = default;

  template <bool ReturnsIndex = true>
  typename std::enable_if<ReturnsIndex, vertex_type>::type add_vertex() {
    vertex_type res = M_graph.size();
    M_graph.push_back({ });
    return res;
  }
  template <bool ReturnsIndex = true>
  typename std::enable_if<!ReturnsIndex, void>::type add_vertex() {
    M_graph.push_back({ });
  }

  template <bool ReturnsIndices = true>
  typename std::enable_if<ReturnsIndices, index_helper>::type 
  add_vertices(const size_type size) {
    size_type cur = M_graph.size();
    M_graph.resize(cur + size);
    return index_helper(cur);
  }
  template <bool ReturnsIndices = true>
  typename std::enable_if<!ReturnsIndices, void>::type 
  add_vertices(const size_type size) {
    size_type cur = M_graph.size();
    M_graph.resize(cur + size);
  }
  
  void add_edge(const edge_type &edge) {
    M_graph[edge.source].push_back(edge);
  }
  template <class... Args>
  void emplace_edge(const vertex_type src, Args&&... args) {
    M_graph[src].emplace_back(src, std::forward<Args>(args)...);
  }

  std::vector<edge_type> &operator [] (const vertex_type vert) {
    return M_graph[vert];
  }
  std::vector<edge_type> &at(const vertex_type vert) {
    return M_graph.at(vert);
  }
  const std::vector<edge_type> &operator [] (const vertex_type vert) const {
    return M_graph[vert];
  }
  const std::vector<edge_type> &at(const vertex_type vert) const {
    return M_graph.at(vert);
  }
  const std::vector<std::vector<edge_type>> &get() const {
    return M_graph;
  }

  size_type size() const {
    return M_graph.size();
  }
  bool empty() const {
    return M_graph.empty();
  }
  void clear() {
    M_graph.clear();
    M_graph.shrink_to_fit();
  }

};

class base_edge {
public:
  using vertex_type = uint32_t;
  const vertex_type source, dest;
  explicit base_edge(const vertex_type source, const vertex_type dest): 
    source(source), dest(dest) 
  { }
  base_edge reverse() {
    return base_edge(dest, source);
  }
};

template <class Flow>
class flow_edge: public base_edge {
public:
  using vertex_type = typename base_edge::vertex_type;
  using flow_type   = Flow;
  flow_type flow;
  const flow_type capacity;
  explicit flow_edge(const base_edge &edge, const flow_type capacity):
    base_edge(edge), flow(0), capacity(capacity)
  { }
  explicit flow_edge(const base_edge &edge, const flow_type flow, const flow_type capacity):
    base_edge(edge), flow(flow), capacity(capacity)
  { }
  explicit flow_edge(const vertex_type source, const vertex_type dest, const flow_type capacity):
    base_edge(source, dest), flow(0), capacity(capacity)
  { }
  explicit flow_edge(const vertex_type source, const vertex_type dest, const flow_type flow, const flow_type capacity):
    base_edge(source, dest), flow(flow), capacity(capacity)
  { }
  flow_edge reverse() const {
    return flow_edge(static_cast<base_edge>(*this).reverse(), capacity);
  }
};

template <class Flow, class Cost>
class flow_cost_edge: public flow_edge<Flow> {
public:
  using vertex_type = typename flow_edge<Flow>::vertex_type;
  using flow_type   = typename flow_edge<Flow>::flow_type;
  using cost_type   = Cost;
  const cost_type cost;
  explicit flow_cost_edge(const flow_edge<Flow> &edge, const cost_type cost):
    flow_edge<Flow>(edge), cost(cost)
  { }
  explicit flow_cost_edge(const vertex_type source, const vertex_type dest, const flow_type capacity, const cost_type cost):
    flow_edge<Flow>(source, dest, capacity), cost(cost)
  { }
  explicit flow_cost_edge(const vertex_type source, const vertex_type dest, const flow_type flow, const flow_type capacity, const cost_type cost):
    flow_edge<Flow>(source, dest, flow, capacity), cost(cost)
  { }
  flow_cost_edge reverse() const {
    return flow_cost_edge(static_cast<flow_edge<Flow>>(*this).reverse(), -cost);
  }
};

/**
 * @title Network
 */
#line 2 "/Users/kodamankod/Desktop/Programming/Library/graph/dinic.cpp"

#include <queue>
#line 5 "/Users/kodamankod/Desktop/Programming/Library/graph/dinic.cpp"

#line 2 "/Users/kodamankod/Desktop/Programming/Library/other/fix_point.cpp"

#line 4 "/Users/kodamankod/Desktop/Programming/Library/other/fix_point.cpp"

template <class Func>
struct fix_point: private Func {
  explicit constexpr fix_point(Func &&func): Func(std::forward<Func>(func)) { }
  template <class... Args>
  constexpr decltype(auto) operator () (Args &&... args) const {
    return Func::operator()(*this, std::forward<Args>(args)...);
  }
};

template <class Func>
constexpr decltype(auto) make_fix_point(Func &&func) {
  return fix_point<Func>(std::forward<Func>(func));
}

/**
 * @title Lambda Recursion
 */
#line 8 "/Users/kodamankod/Desktop/Programming/Library/graph/dinic.cpp"

template <class Network>
class dinic {
public:
  using network_type = Network;
  using vertex_type  = typename Network::vertex_type;
  using edge_type    = typename Network::edge_type;
  using size_type    = typename Network::size_type;
  using flow_type    = typename Network::edge_type::flow_type;
  using height_type  = uint32_t;

  static_assert(std::is_integral<flow_type>::value, "invalid flow type :: non-integral");

private:
  class residual_edge: public edge_type {
  public:
    const size_type rev;
    const bool is_rev;
    explicit residual_edge(const edge_type &edge, const size_type rev, const bool is_rev):
      edge_type(edge), rev(rev), is_rev(is_rev)
    { }
  };

  class node_type {
  public:
    std::vector<residual_edge> edges;
    height_type level;
    size_type iter;
    node_type() = default;
  };

  flow_type M_remain(const residual_edge &edge) {
    return edge.capacity - edge.flow;
  }
  residual_edge &M_cur_edge(node_type &node) {
    return node.edges[node.iter];
  }
  residual_edge &M_rev_edge(const residual_edge &edge) {
    return M_graph[edge.dest].edges[edge.rev];
  }

  void M_bfs(const vertex_type source) {
    for (auto &node: M_graph) {
      node.level = M_graph.size() + 1;
    }
    M_graph[source].level = 0;
    std::queue<vertex_type> queue;
    queue.push(source);
    while (!queue.empty()) {
      const auto vert = queue.front();
      queue.pop();
      for (const auto &edge: M_graph[vert].edges) {
        if (M_remain(edge) > 0) {
          if (M_graph[edge.dest].level == M_graph.size() + 1) {
            M_graph[edge.dest].level = M_graph[vert].level + 1;
            queue.push(edge.dest);
          }
        }
      }
    }
  }

  std::vector<node_type> M_graph;

public:
  dinic() = default;
  explicit dinic(const network_type &net) {
    const auto &graph = net.get();
    M_graph.resize(graph.size());
    for (size_type src = 0; src < graph.size(); ++src) {
      for (const auto &edge: graph[src]) {
        M_graph[src].edges.emplace_back(edge, M_graph[edge.dest].edges.size(), false);
        M_graph[edge.dest].edges.emplace_back(edge.reverse(), M_graph[src].edges.size() - 1, true);
      }
    }
  }

  template <bool ValueOnly = true>
  typename std::enable_if<ValueOnly, flow_type>::type
  max_flow(const vertex_type source, const vertex_type sink) {
    const auto dfs = make_fix_point([&](const auto dfs, 
      const vertex_type vert, const flow_type flow) -> flow_type {
      if (vert == sink) return flow;
      auto &node = M_graph[vert];
      for (; node.iter < node.edges.size(); ++node.iter) {
        auto &edge = M_cur_edge(node);
        if (M_remain(edge) > 0 && node.level < M_graph[edge.dest].level) {
          const auto push = dfs(edge.dest, std::min(flow, M_remain(edge)));
          if (push > 0) {
            edge.flow += push;
            M_rev_edge(edge).flow -= push;
            return push;
          }
        }
      }
      return 0;
    });
    flow_type max_capacity = 0;
    for (auto &node: M_graph) {
      for (auto &edge: node.edges) {
        if (!edge.is_rev) edge.flow = 0;
        else edge.flow = edge.capacity;
        max_capacity = std::max(max_capacity, edge.capacity);
      }
    }
    flow_type flow = 0;
    while (true) {
      M_bfs(source);
      if (M_graph[sink].level == M_graph.size() + 1) {
        return flow;
      }
      for (auto &node: M_graph) {
        node.iter = 0;
      }
      flow_type push;
      while ((push = dfs(source, max_capacity)) > 0) {
        flow += push;
      }
    }
    return flow;
  }

  template <bool ValueOnly = true>
  typename std::enable_if<!ValueOnly, std::pair<flow_type, network_type>>::type
  max_flow(const vertex_type source, const vertex_type sink) {
    const auto flow = max_flow<true>(source, sink);
    network_type graph;
    graph.template add_vertices <false>(M_graph.size());
    for (size_type index = 0; index < M_graph.size(); ++index) {
      for (const auto &edge: M_graph[index].edges) {
        if (!edge.is_rev) {
          graph.add_edge(static_cast<edge_type>(edge));
        }
      }
    }
    return std::make_pair(flow, std::move(graph));
  }

};

/**
 * @title Dinic
 */
#line 57 "main.cpp"

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

int main() {
  i32 H, W;
  std::cin >> H >> W;
  std::vector<i32> A(H), B(W);
  for (auto &x: A) {
    std::cin >> x;
  }
  for (auto &x: B) {
    std::cin >> x;
  }
  std::vector<std::vector<char>> ans(H, std::vector<char>(W, '.'));
  for (auto i: range(0, H)) {
    std::vector<std::vector<i32>> vec(W);
    for (auto j: range(0, W)) {
      if (B[j] > 0) {
        vec[B[j] - 1].push_back(j);
      }
    }
    for (auto j: revrange(0, W)) {
      for (auto x: vec[j]) {
        if (A[i] > 0) {
          ans[i][x] = 'o';
          --A[i];
          --B[x];
        }
      }
    }
    if (A[i] > 0) {
      std::cout << ":(\n";
      return 0;
    }
  }
  if (*std::max_element(B.cbegin(), B.cend()) > 0) {
    std::cout << ":(\n";
    return 0;
  }
  i32 K;
  std::cin >> K;
  while (K--) {
    i32 x, y;
    std::cin >> x >> y;
    --x; --y;
    if (ans[x][y] == 'o') {
      ++A[x];
      ++B[y];
    }
    ans[x][y] = 'x';
  }
  network<flow_edge<i32>> graph;
  const auto S = graph.add_vertex();
  const auto T = graph.add_vertex();
  const auto left = graph.add_vertices(H);
  const auto right = graph.add_vertices(W);
  for (auto i: range(0, H)) {
    if (A[i] > 0) {
      graph.emplace_edge(S, left[i], A[i]);
    }
  }
  for (auto j: range(0, W)) {
    if (B[j] > 0) {
      graph.emplace_edge(right[j], T, B[j]);
    }
  }
  for (auto i: range(0, H)) {
    for (auto j: range(0, W)) {
      if (ans[i][j] == '.') {
        graph.emplace_edge(left[i], right[j], 1);
      }
    }
  }
  const auto [flow, result] = dinic(graph).max_flow<false>(S, T);
  if (flow < std::accumulate(A.cbegin(), A.cend(), 0)) {
    std::cout << ":(\n";
    return 0;
  }
  std::cout << "Yay!\n";
  for (auto i: range(0, H)) {
    for (const auto &e: result[left[i]]) {
      if (e.flow > 0) {
        ans[i][right.to_index(e.dest)] = 'o';
      }
    }
  }
  for (auto &vec: ans) {
    for (auto x: vec) {
      std::cout << x;
    }
    std::cout << '\n';
  }
  return 0;
}
0