// This is free and unencumbered software released into the public domain. // Anyone is free to copy, modify, publish, use, compile, sell, or // distribute this software, either in source code form or as a compiled // binary, for any purpose, commercial or non-commercial, and by any // means. // In jurisdictions that recognize copyright laws, the author or authors // of this software dedicate any and all copyright interest in the // software to the public domain. We make this dedication for the benefit // of the public at large and to the detriment of our heirs and // successors. We intend this dedication to be an overt act of // relinquishment in perpetuity of all present and future rights to this // software under copyright law. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // For more information, please refer to /****************/ /* template.hpp */ /****************/ #include #include #include #include #include #include using std::cerr; using std::cout; using std::endl; using std::max; using std::min; using std::swap; struct BoolName : std::numpunct { std::string t, f; BoolName(std::string t, std::string f) : t(t), f(f) {} std::string do_truename() const { return t; } std::string do_falsename() const { return f; } }; void setBoolName(std::string t, std::string f) { cout.imbue(std::locale(cout.getloc(), new BoolName(t, f))); } struct Initializer { Initializer() { cout << std::fixed << std::setprecision(15) << std::boolalpha; setBoolName("Yes", "No"); } } initializer; struct Input { bool eof; Input() : eof(false) {} operator char() { char v; while (!(this->eof = (std::scanf("%c", &v) != 1)) && std::isspace(v)) { } return v; } operator int() { int v; this->eof = (std::scanf("%d", &v) != 1); return v; } operator long() { long v; this->eof = (std::scanf("%ld", &v) != 1); return v; } operator long long() { long long v; this->eof = (std::scanf("%lld", &v) != 1); return v; } operator unsigned int() { unsigned int v; this->eof = (std::scanf("%u", &v) != 1); return v; } operator unsigned long() { unsigned long v; this->eof = (std::scanf("%lu", &v) != 1); return v; } operator unsigned long long() { unsigned long long v; this->eof = (std::scanf("%llu", &v) != 1); return v; } operator double() { double v; this->eof = (std::scanf("%lf", &v) != 1); return v; } operator long double() { long double v; this->eof = (std::scanf("%Lf", &v) != 1); return v; } void ignore() const { getchar(); } } in; template T abs(T a) { return a >= 0 ? a : -a; } template bool chmin(T &a, const S &b) { return a > b ? a = b, true : false; } template bool chmax(T &a, const S &b) { return a < b ? a = b, true : false; } template std::function cast() { return [](const T &t) { return static_cast(t); }; } template T copy(const T &a) { return T(a); } class ZeroPadding { public: ZeroPadding(int n) : n(n) {} int n; }; std::ostream &operator<<(std::ostream &os, const ZeroPadding &z) { os << std::setw(z.n) << std::setfill('0'); return os; } template constexpr T inf() { return std::numeric_limits::max() / 2 - 1; } /*************/ /* tuple.hpp */ /*************/ #include template class Tuple : public std::tuple { public: Tuple(Input &in) : std::tuple() { (void)in; } }; template class Tuple : public std::tuple { public: Tuple() : std::tuple() {} Tuple(T t, S... s) : std::tuple(t, s...) {} Tuple(const std::tuple &t) : std::tuple(t) {} Tuple(Input &in) { auto a = std::tuple(in); std::tuple b = Tuple(in); std::tuple c = std::tuple_cat(a, b); *this = c; } template auto &get() { return std::get(*this); } template const auto &get() const { return std::get(*this); } }; template Tuple makeTuple(const T &... args) { return Tuple(args...); } namespace std { template class tuple_size> : public std::integral_constant {}; template class tuple_element> { public: using type = tuple_element_t>; }; } // namespace std /*****************/ /* container.hpp */ /*****************/ #include template class Container : public T { private: using S = typename T::value_type; using Itr = typename T::iterator; public: Container() : T() {} Container(int n) : T(n) {} Container(int n, S s) : T(n, s) {} template Container(Itr first, Itr last) : T(first, last) {} Container(const std::initializer_list &v) : T(v) {} Container(int n, Input &in) { std::vector v(n); for (auto &i : v) { i = in; } *this = Container(v.begin(), v.end()); } S max() const { return *std::max_element(this->begin(), this->end()); } template auto max(Function func) const { std::vector> res; for (const auto &i : *this) { res.emplace_back(func(i), i); } return std::max_element(res.begin(), res.end())->second; } S min() const { return *std::min_element(this->begin(), this->end()); } Tuple minmax() const { auto itrs = std::minmax_element(this->begin(), this->end()); return Tuple(*itrs.first, *itrs.second); } template auto min(Function func) const { std::vector> res; for (const auto &i : *this) { res.emplace_back(func(i), i); } return std::min_element(res.begin(), res.end())->second; } int argmax() const { return std::distance(this->begin(), std::max_element(this->begin(), this->end())); } int argmin() const { return std::distance(this->begin(), std::min_element(this->begin(), this->end())); } int find(const S &a) const { return std::distance(this->begin(), std::find(this->begin(), this->end(), a)); } bool contains(const S &a) const { return std::find(this->begin(), this->end(), a) != this->end(); } int size() const { return T::size(); } std::pair equal_range(const S &a) { return std::equal_range(this->begin(), this->end(), a); } template bool all_of(Function func) const { return std::all_of(this->begin(), this->end(), func); } template bool any_of(Function func) const { return std::any_of(this->begin(), this->end(), func); } template bool none_of(Function func) const { return std::none_of(this->begin(), this->end(), func); } int count(const S &s) const { return std::count(this->begin(), this->end(), s); } bool is_sorted() const { return std::is_sorted(this->begin(), this->end()); } void output(std::string sep = "\n", std::string end = "\n") const { bool first = true; for (const auto &i : *this) { if (!first) { cout << sep; } first = false; cout << i; } cout << end; } }; /***********/ /* map.hpp */ /***********/ #include template class Map : public Container> { public: Map() : Container>() {} bool contains(const T &a) const { return this->count(a) != 0; } int count(const T &t) const { return std::map::count(t); } }; /***************/ /* ordered.hpp */ /***************/ template class Ordered { public: template bool operator==(const V &v) const { return !(static_cast(v) < static_cast(*this) || static_cast(*this) < static_cast(v)); } template bool operator!=(const V &v) const { return static_cast(v) < static_cast(*this) || static_cast(*this) < static_cast(v); } template bool operator>(const V &v) const { return static_cast(v) < static_cast(*this); } template bool operator<=(const V &v) const { return !(static_cast(v) < static_cast(*this)); } template bool operator>=(const V &v) const { return !(static_cast(*this) < static_cast(v)); } }; /**************/ /* vector.hpp */ /**************/ #include template class Vector : public Container>, public Ordered> { public: Vector() = default; Vector(const Vector &v) = default; Vector(int n) : Container>(n) {} Vector(int n, T t) : Container>(n, t) {} template Vector(Itr first, Itr last) : Container>(first, last) {} Vector(const std::initializer_list &v) : Container>(v) {} Vector(int n, Input &in) : Container>(n, in) {} Vector &operator+=(const Vector &v) { if (this->size() < v.size()) { this->resize(v.size()); } for (int i = 0; i < v.size(); ++i) { (*this)[i] += v[i]; } return *this; } Vector &operator+=(const T &v) { for (auto &i : *this) { i += v; } return *this; } Vector &operator-=(const Vector &v) { if (this->size() < v.size()) { this->resize(v.size()); } for (int i = 0; i < v.size(); ++i) { (*this)[i] -= v[i]; } return *this; } Vector &operator-=(const T &v) { for (auto &i : *this) { i -= v; } return *this; } Vector &operator*=(const Vector &v) { for (int i = 0; i < this->size(); ++i) { (*this)[i] *= v[i]; } return *this; } Vector &operator*=(const T &v) { for (auto &i : *this) { i *= v; } return *this; } Vector &operator/=(const Vector &v) { for (int i = 0; i < this->size(); ++i) { (*this)[i] /= v[i]; } return *this; } Vector &operator/=(const T &v) { for (auto &i : *this) { i /= v; } return *this; } Vector &operator%=(const Vector &v) { for (int i = 0; i < this->size(); ++i) { (*this)[i] %= v[i]; } return *this; } Vector &operator%=(const T &v) { for (auto &i : *this) { i %= v; } return *this; } Vector operator+(const Vector &v) const { return Vector(*this) += v; } Vector operator+(const T &v) const { return Vector(*this) += v; } Vector operator-(const Vector &v) const { return Vector(*this) -= v; } Vector operator-(const T &v) const { return Vector(*this) -= v; } Vector operator*(const Vector &v) const { return Vector(*this) *= v; } Vector operator*(const T &v) const { return Vector(*this) *= v; } Vector operator/(const Vector &v) const { return Vector(*this) /= v; } Vector operator/(const T &v) const { return Vector(*this) /= v; } Vector operator%(const Vector &v) const { return Vector(*this) %= v; } Vector operator%(const T &v) const { return Vector(*this) %= v; } bool operator<(const Vector &v) const { if (this->size() != v.size()) { return this->size() < v.size(); } for (int i = 0; i < this->size(); ++i) { if ((*this)[i] != v[i]) { return (*this)[i] < v[i]; } } return false; } Vector operator-() const { return *this * -1; } T inner_product(const Vector &v) const { return std::inner_product(this->begin(), this->end(), v.begin(), T(0)); } Vector &partial_sort(int k, bool reverse = false) { if (!reverse) { std::partial_sort(this->begin(), this->begin() + k, this->end()); } else { std::partial_sort(this->begin(), this->begin() + k, this->end(), std::greater()); } return *this; } Vector &sort() { std::sort(this->begin(), this->end()); return *this; } template Vector &sort(Function func) { std::sort(this->begin(), this->end(), func); return *this; } Vector &rsort() { std::sort(this->rbegin(), this->rend()); return *this; } Vector argsort() const { Vector> v; for (int i = 0; i < this->size(); ++i) { v.emplace_back((*this)[i], i); } v.sort(); auto f = [](const Tuple &t) { return t.template get<1>(); }; return v.transform(f); } Vector &nth_element(int n, bool reverse = false) { if (!reverse) { std::nth_element(this->begin(), this->begin() + n, this->end()); } else { std::nth_element(this->begin(), this->begin() + n, this->end(), std::greater()); } return *this; } Vector subvector(int a) const { return Vector(this->begin(), this->begin() + a); } Vector subvector(int a, int b) const { return Vector(this->begin() + a, this->begin() + b); } template auto transform(Function func) const { Vector res; std::transform(this->begin(), this->end(), std::back_inserter(res), func); return res; } Vector partial_sum() const { Vector res; std::partial_sum(this->begin(), this->end(), std::back_inserter(res)); return res; } template Vector partial_sum(Function func) const { Vector res; std::partial_sum(this->begin(), this->end(), std::back_inserter(res), func); return res; } Vector &reverse() { std::reverse(this->begin(), this->end()); return *this; } template int count_if(Function func) const { return std::count_if(this->begin(), this->end(), func); } Vector adjacent_difference() const { Vector res; std::adjacent_difference(this->begin(), this->end(), std::back_inserter(res)); return res; } T lower_bound(T t) const { return std::lower_bound(this->begin(), this->end(), t) - this->begin(); } T upper_bound(T t) const { return std::upper_bound(this->begin(), this->end(), t) - this->begin(); } T accumulate() const { return std::accumulate(this->begin(), this->end(), T()); } template S accumulate(S n, Function func) const { return std::accumulate(this->begin(), this->end(), n, func); } template static Vector makeVector(Int n) { return Vector(n); } template static Vector makeVector(Input &in, Int n) { return Vector(n, in); } template static auto makeVector(Input &in, Int n, Ints... ints) { Vector res; for (int i = 0; i < n; ++i) { res.emplace_back(makeVector(in, ints...)); } return res; } template static auto makeVector(Int n, Ints... ints) { Vector res; for (int i = 0; i < n; ++i) { res.emplace_back(makeVector(ints...)); } return res; } Vector &unique() { this->erase(std::unique(this->begin(), this->end()), this->end()); return *this; } bool next_permutation() { return std::next_permutation(this->begin(), this->end()); } Vector &rotate(int n) { std::rotate(this->begin(), this->begin() + n, this->end()); return *this; } Map countAll() const { Map res; for (const auto &i : *this) { ++res[i]; } return res; } T matmul(const T &a) const { return this->transform([&](const T &i) { return i.inner_product(a); }); } }; template Vector iota(int n, T m = 0) { Vector v(n); std::iota(v.begin(), v.end(), m); return v; } template void read(Vector &t, Vector &s) { for (int i = 0; i < t.size(); ++i) { t[i] = T(in); s[i] = S(in); } } template void read(Vector &t, Vector &s, Vector &u) { for (int i = 0; i < t.size(); ++i) { t[i] = T(in); s[i] = S(in); u[i] = U(in); } } template Vector operator+(const T &a, const Vector &b) { return b + a; } template Vector operator-(const T &a, const Vector &b) { return -b + a; } template Vector operator*(const T &a, const Vector &b) { return b * a; } /*******************/ /* graph/graph.hpp */ /*******************/ struct Edge { using CostType = int; const static int cost = 1; int to; Edge(int to = -1) : to(to) {} Edge(Input &in) : to(in) {} bool isNone() const { return to == -1; } operator int() const { return to; } }; std::ostream &operator<<(std::ostream &s, const Edge &edge) { s << edge.to; return s; } template struct WeightedEdge : public Edge { using CostType = Cost; Cost cost; WeightedEdge(int to = -1, Cost cost = 0) : Edge(to), cost(cost) {} WeightedEdge(Input &in) : Edge(in), cost(in) {} }; template std::ostream &operator<<(std::ostream &s, const WeightedEdge &edge) { s << edge.to << ',' << edge.cost; return s; } template struct ResidualEdge : public Edge { using CapacityType = Capacity; Capacity cap; int rev; ResidualEdge(int to = -1, Capacity cap = 0) : Edge(to), cap(cap) {} ResidualEdge(Input &in) { Edge edge(in); Capacity cap(in); *this = ResidualEdge(edge, cap); } ResidualEdge reverse(int from) const { return ResidualEdge(from, 0); } }; template struct WeightedResidualEdge : public ResidualEdge { Cost cost; WeightedResidualEdge(int to = -1, Capacity cap = 0, Cost cost = 0) : ResidualEdge(to, cap), cost(cost) {} WeightedResidualEdge(Input &in) { ResidualEdge edge(in); Cost cost(in); *this = WeightedResidualEdge(edge, cost); } WeightedResidualEdge reverse(int from) const { return WeightedResidualEdge(from, 0, -cost); } }; template struct FullEdge : public Edge { int from; FullEdge() = default; FullEdge(const int from, const Edge &edge) : Edge(edge), from(from) {} FullEdge(Input &in) { int from(in); Edge edge(in); *this = FullEdge(from, edge); } }; template std::ostream &operator<<(std::ostream &s, const FullEdge &edge) { s << '(' << edge.from << ',' << Edge(edge) << ')'; return s; } template class Graph { public: using EdgeType = Edge; virtual int size() const = 0; template void addEdge(int from, int to, Args...) { (void)from; (void)to; } template void addUndirectedEdge(int from, int to, Args...) { (void)from; (void)to; } Vector> getAllEdges() const { Vector> res; for (int i = 0; i < size(); ++i) { for (const auto &edge : getEdges(i)) { res.emplace_back(i, edge); } } return res; } virtual Vector getEdges(int from) const = 0; virtual Edge getEdge(int from, int to) const = 0; virtual bool hasEdge(int from, int to) const = 0; int getDegree(int v) const { return getEdges(v).size(); } Vector getIndegree() const { Vector degree(size()); for (const auto &edge : getAllEdges()) { ++degree[edge.to]; } return degree; } }; template Graph readGraph(Input &in, int n, int m, bool undirected, bool one_origin) { Graph graph(n); for (int i = 0; i < m; ++i) { FullEdge edge(in); if (one_origin) { --edge.from; --edge.to; } if (undirected) { graph.addUndirectedEdge(edge); } else { graph.addEdge(edge); } } return graph; } /****************************/ /* graph/adjacency_list.hpp */ /****************************/ template class AdjacencyList : public Graph { protected: Vector> graph; public: using EdgeType = Edge; AdjacencyList() = default; AdjacencyList(int n) : graph(n) {} AdjacencyList(Input &in, bool undirected = true, bool one_origin = true) { int n(in), m(in); *this = readGraph>(in, n, m, undirected, one_origin); } AdjacencyList(Input &in, int n, int m, bool undirected = true, bool one_origin = true) { *this = readGraph>(in, n, m, undirected, one_origin); } int size() const { return graph.size(); } template void addEdge(int from, int to, Args... args) { graph[from].emplace_back(to, args...); } void addEdge(const FullEdge &edge) { graph[edge.from].emplace_back(edge); } template void addUndirectedEdge(int from, int to, Args... args) { addEdge(from, to, args...); addEdge(to, from, args...); } void addUndirectedEdge(FullEdge edge) { graph[edge.from].emplace_back(edge); swap(edge.from, edge.to); graph[edge.from].emplace_back(edge); } Vector getEdges(int from) const { return graph[from]; } Edge getEdge(int from, int to) const { Edge res; for (const auto &edge : graph[from]) { if (edge.to == to) { res = edge; } } return res; } bool hasEdge(int from, int to) const { for (const auto &edge : graph[from]) { if (edge.to == to) { return true; } } return false; } Vector &operator[](int v) { return graph[v]; } const Vector &operator[](int v) const { return graph[v]; } }; /****************************/ /* graph/residual_graph.hpp */ /****************************/ template class ResidualGraph : public AdjacencyList { public: ResidualGraph(int n) : AdjacencyList(n) {} ResidualGraph(Input &in, int n, int m, bool undirected = true, bool one_origin = true) { *this = readGraph>(in, n, m, undirected, one_origin); } template void addEdge(int from, int to, Args... args) { Edge edge(to, args...); Edge rev = edge.reverse(from); edge.rev = this->graph[to].size(); rev.rev = this->graph[from].size(); this->graph[from].emplace_back(edge); this->graph[to].emplace_back(rev); } void addEdge(const FullEdge &e) { Edge edge = e; Edge rev = edge.reverse(e.from); edge.rev = this->graph[edge.to].size(); rev.rev = this->graph[e.from].size(); this->graph[e.from].emplace_back(edge); this->graph[edge.to].emplace_back(rev); } template void addUndirectedEdge(int from, int to, Args... args) { addEdge(from, to, args...); this->graph[to].back().cap = this->graph[from].back().cap; } void addUndirectedEdge(const FullEdge &e) { Edge edge = e; addEdge(e.from, edge); this->graph[edge.to].back().cap = this->graph[e.from].back().cap; } void flow(int v, int i, typename Edge::CapacityType f) { auto &e = this->graph[v][i]; e.cap -= f; this->graph[e.to][e.rev].cap += f; } }; /*************/ /* queue.hpp */ /*************/ #include template class Queue : public std::queue { public: Queue() : std::queue() {} Queue(const std::initializer_list &q) : std::queue(q) {} T front() { T res = std::queue::front(); std::queue::pop(); return res; } T peek() const { return std::queue::top(); } void pop() const { assert(false); } }; /**********************/ /* graph/max_flow.hpp */ /**********************/ template class MaxFlow { private: ResidualGraph> &graph; Vector level, iter; void bfs(int source) { fill(level.begin(), level.end(), -1); level[source] = 0; Queue que; que.push(source); while (!que.empty()) { int v = que.front(); for (const auto &edge : graph[v]) { if (edge.cap == 0 || level[edge.to] >= 0) { continue; } level[edge.to] = level[v] + 1; que.push(edge.to); } } } int dfs(int v, int sink, Capacity flow) { if (v == sink) { return flow; } for (int &i = iter[v]; i < int(graph[v].size()); ++i) { auto &edge = graph[v][i]; if (edge.cap == 0 || level[v] >= level[edge.to]) { continue; } Capacity f = dfs(edge.to, sink, min(flow, edge.cap)); if (f == 0) { continue; } graph.flow(v, i, f); return f; } return 0; } public: MaxFlow(ResidualGraph> &graph) : graph(graph) {} Capacity solve(int source, int sink) { level = Vector(graph.size(), 0); iter = Vector(graph.size(), 0); Capacity flow = 0, f; while (true) { bfs(source); if (level[sink] < 0) { return flow; } fill(iter.begin(), iter.end(), 0); while ((f = dfs(source, sink, inf())) > 0) { flow += f; } } } }; template Capacity maxFlow(ResidualGraph> &graph, int source, int sink) { MaxFlow dinic(graph); return dinic.solve(source, sink); } /*********************/ /* unordered_set.hpp */ /*********************/ #include template class UnorderedSet : public Container> { public: UnorderedSet() : Container>() {} template UnorderedSet(Itr first, Itr last) : Container>(first, last) {} UnorderedSet(const std::initializer_list &s) : Container>(s) {} template UnorderedSet &operator&=(const S &a) { UnorderedSet r; if (this->size() < a.size()) { for (const auto &i : *this) { if (a.in(i)) { r.emplace(i); } } } else { for (const auto &i : a) { if (this->in(i)) { r.emplace(i); } } } *this = r; return *this; } int count(const T &a) const { return std::unordered_set::count(a); } int contains(const T &a) const { return count(a) > 0; } }; /************/ /* main.cpp */ /************/ int main() { setBoolName("SHIROBAKO", "BANSAKUTSUKITA"); int w(in), n(in); ResidualGraph> graph(w + n + 2); int id = 0; int source = id++; int sink = id++; Vector a(n); for (int &i : a) { i = id++; } for (int i = 0; i < n; ++i) { int j(in); graph.addEdge(source, a[i], j); } int m(in); Vector b(m); for (int &i : b) { i = id++; } for (int i = 0; i < m; ++i) { int c(in); graph.addEdge(b[i], sink, c); } for (int i = 0; i < m; ++i) { int q(in); UnorderedSet x; for (int j = 0; j < q; ++j) { int k(in); x.insert(k - 1); } for (int j = 0; j < n; ++j) { if (x.count(j)) { continue; } graph.addEdge(a[j], b[i], w); } } MaxFlow mf(graph); cout << (mf.solve(source, sink) >= w) << endl; }