結果

問題 No.2464 To DAG
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-09-08 21:29:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,989 bytes
コンパイル時間 1,607 ms
コンパイル使用メモリ 108,216 KB
実行使用メモリ 31,412 KB
最終ジャッジ日時 2023-09-08 21:29:59
合計ジャッジ時間 7,968 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 85 ms
20,996 KB
testcase_03 TLE -
testcase_04 AC 1,656 ms
22,472 KB
testcase_05 AC 1,069 ms
21,992 KB
testcase_06 AC 860 ms
22,480 KB
testcase_07 AC 1,544 ms
17,628 KB
testcase_08 AC 1,486 ms
18,136 KB
testcase_09 AC 878 ms
16,240 KB
testcase_10 AC 699 ms
17,052 KB
testcase_11 AC 500 ms
12,688 KB
testcase_12 AC 394 ms
10,760 KB
testcase_13 WA -
testcase_14 AC 186 ms
19,704 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 143 ms
17,176 KB
testcase_23 AC 137 ms
14,880 KB
testcase_24 AC 117 ms
14,584 KB
testcase_25 AC 140 ms
18,456 KB
testcase_26 AC 63 ms
8,160 KB
testcase_27 AC 522 ms
30,960 KB
testcase_28 AC 593 ms
31,068 KB
testcase_29 AC 418 ms
31,412 KB
testcase_30 AC 633 ms
18,744 KB
testcase_31 AC 1,458 ms
19,744 KB
testcase_32 TLE -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 94 ms
15,940 KB
testcase_36 AC 96 ms
16,376 KB
testcase_37 AC 73 ms
17,360 KB
testcase_38 AC 72 ms
16,340 KB
testcase_39 AC 7 ms
8,816 KB
testcase_40 AC 8 ms
9,160 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