結果

問題 No.1244 Black Segment
ユーザー RTnFRTnF
提出日時 2020-10-03 09:53:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 5,087 bytes
コンパイル時間 2,422 ms
コンパイル使用メモリ 213,188 KB
実行使用メモリ 41,708 KB
最終ジャッジ日時 2023-09-25 04:53:01
合計ジャッジ時間 6,715 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 57 ms
23,016 KB
testcase_15 AC 4 ms
4,448 KB
testcase_16 AC 57 ms
23,612 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 80 ms
20,348 KB
testcase_19 AC 114 ms
27,228 KB
testcase_20 AC 113 ms
28,312 KB
testcase_21 AC 92 ms
27,092 KB
testcase_22 AC 117 ms
27,692 KB
testcase_23 AC 112 ms
26,604 KB
testcase_24 AC 108 ms
29,104 KB
testcase_25 AC 109 ms
29,828 KB
testcase_26 AC 97 ms
25,168 KB
testcase_27 AC 106 ms
22,636 KB
testcase_28 AC 109 ms
29,044 KB
testcase_29 AC 94 ms
26,232 KB
testcase_30 AC 110 ms
29,884 KB
testcase_31 AC 110 ms
26,840 KB
testcase_32 AC 112 ms
26,244 KB
testcase_33 AC 109 ms
27,048 KB
testcase_34 AC 139 ms
37,392 KB
testcase_35 AC 138 ms
41,708 KB
testcase_36 AC 130 ms
36,996 KB
testcase_37 AC 125 ms
34,072 KB
testcase_38 AC 118 ms
30,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region template
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
template <typename T>
using pqrev = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for (ll i = 0, i##_end = (n); i < i##_end; i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for (ll i = (a), i##_end = (b); i < i##_end; i++)
#define reprb(i, a, b) for (ll i = (b)-1, i##_end = (a); i >= i##_end; i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
//*
constexpr ll MOD = 1000000007;
/*/
constexpr ll MOD = 998244353;
//*/
constexpr ll INF = 1e+18;
constexpr ld EPS = 1e-12L;
constexpr ld PI = 3.14159265358979323846L;
constexpr ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
constexpr ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
template <typename S, typename T>
inline bool chmax(S &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename S, typename T>
inline bool chmin(S &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
template <typename T>
bool print_(const T &a) {
  cout << a;
  return true;
}
template <typename T>
bool print_(const vector<T> &vec) {
  for (auto &a : vec) {
    cout << a;
    if (&a != &vec.back()) {
      cout << " ";
    }
  }
  return false;
}
template <typename T>
bool print_(const vector<vector<T>> &vv) {
  for (auto &v : vv) {
    for (auto &a : v) {
      cout << a;
      if (&a != &v.back()) {
        cout << " ";
      }
    }
    if (&v != &vv.back()) {
      cout << "\n";
    }
  }
  return false;
}
void print() { cout << "\n"; }
template <typename Head, typename... Tail>
void print(Head &&head, Tail &&... tail) {
  bool f = print_(head);
  if (sizeof...(tail) != 0) {
    cout << (f ? " " : "\n");
  }
  print(forward<Tail>(tail)...);
}
#pragma endregion

// Primal-Dual FE(log V)
// https://ei1333.github.io/luzhiled/snippets/graph/primal-dual.html
template <typename flow_t = ll, typename cost_t = ll> struct GraphMCF {
  struct edge {
    int to;
    flow_t cap;
    cost_t cost;
    int rev;
    bool isrev;
  };
  vector<vector<edge>> graph;
  vector<cost_t> potential, min_cost;
  vector<int> prevv, preve;
  const cost_t MAX;

  GraphMCF(int V) : graph(V), MAX(sqrtl(numeric_limits<cost_t>::max())) {}

  void add_edge(int from, int to, flow_t cap, cost_t cost) {
    graph[from].emplace_back(
      (edge){to, cap, cost, (int)graph[to].size(), false});
    graph[to].emplace_back(
      (edge){from, 0, -cost, (int)graph[from].size() - 1, true});
  }

  cost_t flow(int s, int t, flow_t f) {
    int V = (int)graph.size();
    cost_t ret = 0;
    using Pi = pair<cost_t, int>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    potential.assign(V, 0);
    preve.assign(V, -1);
    prevv.assign(V, -1);

    while (f > 0) {
      min_cost.assign(V, MAX);
      que.emplace(0, s);
      min_cost[s] = 0;
      while (!que.empty()) {
        Pi p = que.top();
        que.pop();
        if (min_cost[p.second] < p.first) continue;
        for (int i = 0; i < int(graph[p.second].size()); i++) {
          edge &e = graph[p.second][i];
          cost_t nextCost =
            min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];
          if (e.cap > 0 && min_cost[e.to] > nextCost) {
            min_cost[e.to] = nextCost;
            prevv[e.to] = p.second, preve[e.to] = i;
            que.emplace(min_cost[e.to], e.to);
          }
        }
      }
      if (min_cost[t] == MAX) return -1;
      for (int v = 0; v < V; v++) potential[v] += min_cost[v];
      flow_t addflow = f;
      for (int v = t; v != s; v = prevv[v]) {
        addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
      }
      f -= addflow;
      ret += addflow * potential[t];
      for (int v = t; v != s; v = prevv[v]) {
        edge &e = graph[prevv[v]][preve[v]];
        e.cap -= addflow;
        graph[v][e.rev].cap += addflow;
      }
    }
    return ret;
  }

  void output() {
    for (int i = 0; i < graph.size(); i++) {
      for (auto &e : graph[i]) {
        if (e.isrev) continue;
        auto &rev_e = graph[e.to][e.rev];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/"
             << rev_e.cap + e.cap << ")" << endl;
      }
    }
  }
};


int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  ll n, m, a, b;
  cin >> n >> m >> a >> b;
  GraphMCF g(n+3);
  rep(i, m){
    ll l, r;
    cin >> l >> r;
    g.add_edge(l-1, r, 1, 1);
    g.add_edge(r, l-1, 1, 1);
  }
  rep(i, a) g.add_edge(n+1, i, 1, 0);
  repr(i, b, n+1) g.add_edge(i, n+2, 1, 0);
  print(g.flow(n+1, n+2, 1));
}
0