cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: グラフ/フロー/最大流/最小流量制約付き最大流
(test/graph/flow/maximum_flow/maximum_flow_with_lower_bound_constraint.test.cpp)

Depends on

Code

/*
 * @title グラフ/フロー/最大流/最小流量制約付き最大流
 *
 * verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1615
 */

#include <iostream>
#include <vector>

#include "emthrm/graph/flow/maximum_flow/dinic.hpp"
#include "emthrm/graph/flow/maximum_flow/maximum_flow_with_lower_bound_constraint.hpp"

int main() {
  while (true) {
    int n, m;
    std::cin >> n >> m;
    if (n == 0 && m == 0) [[unlikely]] break;
    std::vector<int> u(m), v(m);
    for (int i = 0; i < m; ++i) {
      std::cin >> u[i] >> v[i];
      --u[i]; --v[i];
    }
    const int s = m + n, t = m + n + 1;
    const auto solve =
        [m, n, s, t, &u, &v](const int lb, const int ub) -> bool {
          emthrm::MaximumFlowWithLowerBoundConstraint<emthrm::Dinic, int>
              lower_bound_constraint(m + n + 2);
          for (int i = 0; i < m; ++i) {
            lower_bound_constraint.add_edge(s, i, 1, 1);
          }
          for (int i = 0; i < n; ++i) {
            lower_bound_constraint.add_edge(m + i, t, lb, ub);
          }
          for (int i = 0; i < m; ++i) {
            lower_bound_constraint.add_edge(i, m + u[i], 0, 1);
            lower_bound_constraint.add_edge(i, m + v[i], 0, 1);
          }
          return lower_bound_constraint.solve(s, t) != -1;
        };
    int lb = 0, ub = n;
    for (int i = 0, j = 1; i < n; ++i) {
      while (j <= n && !solve(i, j)) ++j;
      if (j > n) break;
      if (ub - lb >= j - i) {
        lb = i;
        ub = j;
      }
      if (i == j) ++j;
    }
    std::cout << lb << ' ' << ub << '\n';
  }
  return 0;
}
#line 1 "test/graph/flow/maximum_flow/maximum_flow_with_lower_bound_constraint.test.cpp"
/*
 * @title グラフ/フロー/最大流/最小流量制約付き最大流
 *
 * verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1615
 */

#include <iostream>
#include <vector>

#line 1 "include/emthrm/graph/flow/maximum_flow/dinic.hpp"



#include <algorithm>
#include <limits>
#include <queue>
#include <utility>
#line 9 "include/emthrm/graph/flow/maximum_flow/dinic.hpp"

namespace emthrm {

template <typename T>
struct Dinic {
  struct Edge {
    int dst, rev;
    T cap;
    explicit Edge(const int dst, const T cap, const int rev)
        : dst(dst), rev(rev), cap(cap) {}
  };

  std::vector<std::vector<Edge>> graph;

  explicit Dinic(const int n) : graph(n), level(n), itr(n) {}

  void add_edge(const int src, const int dst, const T cap) {
    graph[src].emplace_back(dst, cap, graph[dst].size());
    graph[dst].emplace_back(src, 0, graph[src].size() - 1);
  }

  T maximum_flow(const int s, const int t,
                 T limit = std::numeric_limits<T>::max()) {
    T res = 0;
    while (limit > 0) {
      std::fill(level.begin(), level.end(), -1);
      level[s] = 0;
      std::queue<int> que;
      que.emplace(s);
      while (!que.empty()) {
        const int ver = que.front();
        que.pop();
        for (const Edge& e : graph[ver]) {
          if (level[e.dst] == -1 && e.cap > 0) {
            level[e.dst] = level[ver] + 1;
            que.emplace(e.dst);
          }
        }
      }
      if (level[t] == -1) break;
      std::fill(itr.begin(), itr.end(), 0);
      while (limit > 0) {
        const T f = dfs(s, t, limit);
        if (f == 0) break;
        limit -= f;
        res += f;
      }
    }
    return res;
  }

 private:
  std::vector<int> level, itr;

  T dfs(const int ver, const int t, const T flow) {
    if (ver == t) return flow;
    for (; std::cmp_less(itr[ver], graph[ver].size()); ++itr[ver]) {
      Edge& e = graph[ver][itr[ver]];
      if (level[ver] < level[e.dst] && e.cap > 0) {
        const T tmp = dfs(e.dst, t, std::min(flow, e.cap));
        if (tmp > 0) {
          e.cap -= tmp;
          graph[e.dst][e.rev].cap += tmp;
          return tmp;
        }
      }
    }
    return 0;
  }
};

}  // namespace emthrm


#line 1 "include/emthrm/graph/flow/maximum_flow/maximum_flow_with_lower_bound_constraint.hpp"



#line 1 "include/emthrm/graph/flow/maximum_flow/maximum_flow.hpp"
/**
 * @title 最大流コンセプト
 */

#ifndef EMTHRM_GRAPH_FLOW_MAXIMUM_FLOW_MAXIMUM_FLOW_HPP_
#define EMTHRM_GRAPH_FLOW_MAXIMUM_FLOW_MAXIMUM_FLOW_HPP_

#include <concepts>
#line 10 "include/emthrm/graph/flow/maximum_flow/maximum_flow.hpp"

namespace emthrm {

template <template <typename> class C, typename T>
concept MaximumFlow = requires (C<T> mf) {
  {mf.add_edge(std::declval<int>(), std::declval<int>(), std::declval<T>())}
      -> std::same_as<void>;
  {mf.maximum_flow(std::declval<int>(), std::declval<int>())}
      -> std::same_as<T>;
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_FLOW_MAXIMUM_FLOW_MAXIMUM_FLOW_HPP_
#line 5 "include/emthrm/graph/flow/maximum_flow/maximum_flow_with_lower_bound_constraint.hpp"

namespace emthrm {

template <template <typename> class C, typename T>
requires MaximumFlow<C, T>
struct MaximumFlowWithLowerBoundConstraint {
  explicit MaximumFlowWithLowerBoundConstraint(const int n)
      : n(n), sum_lb(0), mf(n + 2) {}

  void add_edge(const int src, const int dst, const T lb, const T ub) {
    mf.add_edge(src, dst, ub - lb);
    mf.add_edge(n, dst, lb);
    mf.add_edge(src, n + 1, lb);
    sum_lb += lb;
  }

  T solve(const int s, const int t) {
    const T a = mf.maximum_flow(n, n + 1);
    const T b = mf.maximum_flow(s, n + 1);
    const T c = mf.maximum_flow(n, t);
    const T d = mf.maximum_flow(s, t);
    return a + b == sum_lb && b == c ? b + d : -1;
  }

 private:
  const int n;
  T sum_lb;
  C<T> mf;
};

}  // namespace emthrm


#line 12 "test/graph/flow/maximum_flow/maximum_flow_with_lower_bound_constraint.test.cpp"

int main() {
  while (true) {
    int n, m;
    std::cin >> n >> m;
    if (n == 0 && m == 0) [[unlikely]] break;
    std::vector<int> u(m), v(m);
    for (int i = 0; i < m; ++i) {
      std::cin >> u[i] >> v[i];
      --u[i]; --v[i];
    }
    const int s = m + n, t = m + n + 1;
    const auto solve =
        [m, n, s, t, &u, &v](const int lb, const int ub) -> bool {
          emthrm::MaximumFlowWithLowerBoundConstraint<emthrm::Dinic, int>
              lower_bound_constraint(m + n + 2);
          for (int i = 0; i < m; ++i) {
            lower_bound_constraint.add_edge(s, i, 1, 1);
          }
          for (int i = 0; i < n; ++i) {
            lower_bound_constraint.add_edge(m + i, t, lb, ub);
          }
          for (int i = 0; i < m; ++i) {
            lower_bound_constraint.add_edge(i, m + u[i], 0, 1);
            lower_bound_constraint.add_edge(i, m + v[i], 0, 1);
          }
          return lower_bound_constraint.solve(s, t) != -1;
        };
    int lb = 0, ub = n;
    for (int i = 0, j = 1; i < n; ++i) {
      while (j <= n && !solve(i, j)) ++j;
      if (j > n) break;
      if (ub - lb >= j - i) {
        lb = i;
        ub = j;
      }
      if (i == j) ++j;
    }
    std::cout << lb << ' ' << ub << '\n';
  }
  return 0;
}
Back to top page