結果

問題 No.2464 To DAG
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-09-08 21:29:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,989 bytes
コンパイル時間 1,148 ms
コンパイル使用メモリ 110,168 KB
実行使用メモリ 33,040 KB
最終ジャッジ日時 2024-06-26 14:20:10
合計ジャッジ時間 7,092 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 87 ms
18,732 KB
testcase_03 WA -
testcase_04 AC 1,128 ms
23,620 KB
testcase_05 AC 742 ms
22,864 KB
testcase_06 AC 672 ms
21,312 KB
testcase_07 AC 1,187 ms
19,172 KB
testcase_08 AC 990 ms
19,004 KB
testcase_09 AC 707 ms
17,108 KB
testcase_10 AC 527 ms
16,600 KB
testcase_11 AC 473 ms
12,944 KB
testcase_12 AC 371 ms
11,200 KB
testcase_13 WA -
testcase_14 AC 162 ms
20,004 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 132 ms
18,112 KB
testcase_23 AC 134 ms
18,772 KB
testcase_24 AC 116 ms
19,708 KB
testcase_25 AC 132 ms
19,424 KB
testcase_26 AC 64 ms
8,204 KB
testcase_27 AC 356 ms
33,040 KB
testcase_28 AC 389 ms
30,164 KB
testcase_29 AC 261 ms
29,664 KB
testcase_30 AC 510 ms
19,244 KB
testcase_31 AC 1,168 ms
20,432 KB
testcase_32 AC 1,610 ms
18,456 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 98 ms
17,388 KB
testcase_36 AC 98 ms
16,924 KB
testcase_37 AC 74 ms
19,536 KB
testcase_38 AC 74 ms
15,736 KB
testcase_39 AC 7 ms
9,216 KB
testcase_40 AC 7 ms
9,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


template <class Flow> struct MaxFlow {
  // Watch out when using types other than int and long long.
  static constexpr Flow FLOW_EPS = 1e-10L;
  static constexpr Flow FLOW_INF = std::numeric_limits<Flow>::max();
  
  int n, m;
  vector<int> ptr, nxt, zu;
  vector<Flow> capa;

  explicit MaxFlow(int n_) : n(n_), m(0), ptr(n_, -1) {}
  void ae(int u, int v, Flow w0, Flow w1 = 0) {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    assert(0 <= w0);
    assert(0 <= w1);
    nxt.push_back(ptr[u]); zu.push_back(v); capa.push_back(w0); ptr[u] = m++;
    nxt.push_back(ptr[v]); zu.push_back(u); capa.push_back(w1); ptr[v] = m++;
  }

  vector<int> see, lev, que;

  Flow augment(int u, int t, Flow limFlow) {
    if (u == t) return limFlow;
    for (int &i = see[u]; ~i; i = nxt[i]) if (capa[i] > FLOW_EPS) {
      const int v = zu[i];
      if (lev[u] < lev[v]) {
        const Flow f = augment(v, t, min(limFlow, capa[i]));
        if (f > FLOW_EPS) { capa[i] -= f; capa[i ^ 1] += f; return f; }
      }
    }
    return 0;
  }
  bool bfs(int s, int t) {
    for (int u = 0; u < n; ++u) { see[u] = ptr[u]; lev[u] = -1; }
    auto head = que.begin(), tail = que.begin();
    for (lev[*tail++ = s] = 0; head != tail; ) {
      const int u = *head++;
      for (int i = ptr[u]; ~i; i = nxt[i]) if (capa[i] > FLOW_EPS) {
        const int v = zu[i];
        if (!~lev[v]) {
          lev[*tail++ = v] = lev[u] + 1;
          if (v == t) return true;
        }
      }
    }
    return false;
  }
  Flow run(int s, int t, Flow limFlow = FLOW_INF) {
    see.resize(n); lev.resize(n); que.resize(n);
    Flow flow = 0;
    for (; flow + FLOW_EPS < limFlow && bfs(s, t); ) {
      for (Flow f; (f = augment(s, t, limFlow - flow)) > FLOW_EPS; flow += f) {}
    }
    return flow;
  }
};

////////////////////////////////////////////////////////////////////////////////


int N, M;
vector<int> A, B;

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    A.resize(M);
    B.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
    }
    
    vector<int> bal(N, 0);
    for (int i = 0; i < M; ++i) {
      ++bal[A[i]];
      --bal[B[i]];
    }
    MaxFlow<int> mf(N + 2);
    const int src = N, snk = N + 1;
    for (int i = 0; i < M; ++i) {
      mf.ae(A[i], B[i], 1);
    }
    for (int u = 0; u < N; ++u) {
      if (bal[u] > 0) mf.ae(src, u, +bal[u]);
      if (bal[u] < 0) mf.ae(u, snk, -bal[u]);
    }
    mf.run(src, snk);
    
    vector<int> ans;
    for (int i = 0; i < M; ++i) if (!mf.capa[i << 1]) {
      ans.push_back(i);
    }
    printf("%d %d\n", N, (int)ans.size());
    for (const int i : ans) {
      printf("%d %d\n", A[i] + 1, B[i] + 1);
    }
cerr<<"========"<<endl;
  }
  return 0;
}
0