結果

問題 No.2286 Join Hands
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-04-28 23:01:44
言語 D
(dmd 2.106.1)
結果
RE  
実行時間 -
コード長 3,309 bytes
コンパイル時間 1,013 ms
コンパイル使用メモリ 104,288 KB
実行使用メモリ 6,440 KB
最終ジャッジ日時 2023-09-04 20:12:57
合計ジャッジ時間 6,106 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 RE -
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 RE -
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 15 ms
4,376 KB
testcase_09 AC 16 ms
4,380 KB
testcase_10 AC 23 ms
4,384 KB
testcase_11 AC 22 ms
4,380 KB
testcase_12 AC 23 ms
4,384 KB
testcase_13 AC 25 ms
4,384 KB
testcase_14 AC 23 ms
4,496 KB
testcase_15 AC 23 ms
4,384 KB
testcase_16 AC 21 ms
4,384 KB
testcase_17 AC 15 ms
4,384 KB
testcase_18 AC 19 ms
4,888 KB
testcase_19 AC 12 ms
4,384 KB
testcase_20 AC 14 ms
5,580 KB
testcase_21 AC 20 ms
6,440 KB
testcase_22 AC 12 ms
4,388 KB
testcase_23 AC 10 ms
4,380 KB
testcase_24 AC 16 ms
5,012 KB
testcase_25 AC 11 ms
4,380 KB
testcase_26 AC 10 ms
4,376 KB
testcase_27 AC 11 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,384 KB
testcase_30 AC 2 ms
4,384 KB
testcase_31 RE -
testcase_32 RE -
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 AC 2 ms
4,384 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 RE -
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 RE -
testcase_44 AC 1 ms
4,380 KB
testcase_45 RE -
testcase_46 RE -
testcase_47 AC 15 ms
4,384 KB
testcase_48 AC 15 ms
4,920 KB
testcase_49 AC 15 ms
4,384 KB
testcase_50 RE -
testcase_51 AC 7 ms
4,376 KB
testcase_52 AC 13 ms
6,204 KB
testcase_53 AC 16 ms
4,384 KB
testcase_54 AC 15 ms
4,420 KB
testcase_55 AC 16 ms
5,648 KB
testcase_56 AC 15 ms
4,380 KB
testcase_57 RE -
testcase_58 RE -
testcase_59 RE -
testcase_60 AC 15 ms
5,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


class MaxFlow(Capa, Capa CAPA_EPS = 0, Capa CAPA_INF = 10^^9) {
  int n, m;
  int[][] g;
  int[] zu;
  Capa[] capa;
  Capa tof;
  int[] lev, see, que;
  this(int n) {
    this.n = n; m = 0; g = new int[][n]; zu = []; capa = [];
    lev = new int[n]; see = new int[n]; que = new int[n];
  }
  void ae(int u, int v, Capa w0, Capa w1 = 0) {
    g[u] ~= m; zu ~= v; capa ~= w0; ++m;
    g[v] ~= m; zu ~= u; capa ~= w1; ++m;
  }
  Capa augment(int src, int ink, Capa flo) {
    if (src == ink) return flo;
    foreach (i; g[src][see[src] .. $]) {
      if (capa[i] > CAPA_EPS && lev[src] < lev[zu[i]]) {
        Capa f = augment(zu[i], ink, min(flo, capa[i]));
        if (f > CAPA_EPS) { capa[i] -= f; capa[i ^ 1] += f; return f; }
      }
      ++see[src];
    }
    return 0;
  }
  bool dinic(int src, int ink, Capa flo = CAPA_INF) {
    for (tof = 0; tof + CAPA_EPS < flo; ) {
      int qb, qe;
      lev[] = -1;
     dinicBFS:
      for (lev[src] = 0, que[qe++] = src; qb != qe; ) {
        const u = que[qb++];
        foreach (i; g[u]) {
          const v = zu[i];
          if (capa[i] > CAPA_EPS && lev[v] == -1) {
            lev[v] = lev[u] + 1; que[qe++] = v;
            if (v == ink) break dinicBFS;
          }
        }
      }
      if (lev[ink] == -1) return false;
      see[] = 0;
      for (; ; ) {
        Capa f = augment(src, ink, flo - tof);
        if (f <= CAPA_EPS) break;
        tof += f;
      }
    }
    return true;
  }
}


void main() {
  try {
    for (; ; ) {
      const N = readInt;
      const M = readInt;
      auto A = new int[M];
      auto B = new int[M];
      foreach (i; 0 .. M) {
        A[i] = readInt - 1;
        B[i] = readInt - 1;
      }
      
      auto mf = new MaxFlow!int(2 + N + N);
      foreach (u; 0 .. N) {
        mf.ae(0, 2 + u, 1);
        mf.ae(2 + N + u, 1, 1);
      }
      foreach (i; 0 .. M) {
        mf.ae(2 + A[i], 2 + N + B[i], 1);
        mf.ae(2 + B[i], 2 + N + A[i], 1);
      }
      mf.dinic(0, 1);
      const ans = mf.tof;
      debug {
        writeln("ans = ", ans);
      }
      writeln(2 * ans - N);
      if (ans == N - 1) {
        assert(false);
      }
    }
  } catch (EOFException e) {
  }
}
0