結果

問題 No.1244 Black Segment
ユーザー RTnFRTnF
提出日時 2020-10-03 09:53:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 5,087 bytes
コンパイル時間 2,172 ms
コンパイル使用メモリ 216,908 KB
実行使用メモリ 41,696 KB
最終ジャッジ日時 2024-07-18 04:09:25
合計ジャッジ時間 5,162 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 4 ms
6,944 KB
testcase_14 AC 44 ms
22,784 KB
testcase_15 AC 4 ms
6,940 KB
testcase_16 AC 46 ms
23,552 KB
testcase_17 AC 4 ms
6,944 KB
testcase_18 AC 54 ms
20,432 KB
testcase_19 AC 80 ms
27,400 KB
testcase_20 AC 84 ms
28,324 KB
testcase_21 AC 69 ms
27,132 KB
testcase_22 AC 90 ms
27,728 KB
testcase_23 AC 88 ms
26,768 KB
testcase_24 AC 87 ms
29,132 KB
testcase_25 AC 84 ms
29,764 KB
testcase_26 AC 80 ms
25,288 KB
testcase_27 AC 80 ms
22,656 KB
testcase_28 AC 91 ms
29,128 KB
testcase_29 AC 72 ms
26,328 KB
testcase_30 AC 89 ms
30,004 KB
testcase_31 AC 79 ms
26,992 KB
testcase_32 AC 85 ms
26,176 KB
testcase_33 AC 82 ms
27,092 KB
testcase_34 AC 114 ms
37,180 KB
testcase_35 AC 110 ms
41,696 KB
testcase_36 AC 102 ms
37,124 KB
testcase_37 AC 97 ms
34,096 KB
testcase_38 AC 82 ms
30,700 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