結果

問題 No.2286 Join Hands
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-04-29 00:07:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,541 bytes
コンパイル時間 1,388 ms
コンパイル使用メモリ 118,504 KB
実行使用メモリ 23,464 KB
最終ジャッジ日時 2024-11-17 22:51:36
合計ジャッジ時間 85,197 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
14,580 KB
testcase_01 AC 2 ms
17,360 KB
testcase_02 AC 3 ms
14,708 KB
testcase_03 AC 3 ms
17,504 KB
testcase_04 AC 2 ms
14,712 KB
testcase_05 AC 3 ms
17,396 KB
testcase_06 AC 3 ms
16,624 KB
testcase_07 AC 3 ms
15,760 KB
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 AC 3 ms
14,712 KB
testcase_29 AC 2 ms
17,556 KB
testcase_30 AC 3 ms
16,628 KB
testcase_31 AC 3 ms
17,276 KB
testcase_32 AC 2 ms
16,156 KB
testcase_33 AC 2 ms
16,628 KB
testcase_34 AC 2 ms
14,712 KB
testcase_35 AC 2 ms
16,624 KB
testcase_36 AC 2 ms
16,752 KB
testcase_37 AC 2 ms
16,748 KB
testcase_38 AC 2 ms
16,760 KB
testcase_39 AC 2 ms
16,160 KB
testcase_40 AC 2 ms
14,584 KB
testcase_41 AC 2 ms
7,896 KB
testcase_42 AC 2 ms
9,932 KB
testcase_43 AC 2 ms
9,808 KB
testcase_44 AC 1 ms
7,764 KB
testcase_45 AC 2 ms
7,760 KB
testcase_46 AC 1 ms
7,892 KB
testcase_47 AC 162 ms
8,652 KB
testcase_48 AC 227 ms
8,516 KB
testcase_49 AC 947 ms
8,780 KB
testcase_50 TLE -
testcase_51 AC 5 ms
8,608 KB
testcase_52 TLE -
testcase_53 TLE -
testcase_54 TLE -
testcase_55 TLE -
testcase_56 AC 7 ms
8,648 KB
testcase_57 AC 6 ms
8,776 KB
testcase_58 TLE -
testcase_59 TLE -
testcase_60 AC 8 ms
17,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#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; }


namespace MCF {

using Capa = int;
using Cost = Int;
constexpr int MAX_N = 100'010;
constexpr int MAX_M = 100'010;
constexpr int QUE_SIZE = 1 << (32 - __builtin_clz(MAX_N));
constexpr int BELLMAN_FORD_NUM_ITERS = 10;
constexpr int LOG_SCALING = 4;

int n, m, ptr[MAX_N], cur[MAX_N], next[MAX_M << 1], zu[MAX_M << 1];
bool on[MAX_N];
int que[QUE_SIZE], qb, qe;
Capa capa[MAX_M << 1], ex[MAX_N];
Cost cost[MAX_M << 1], pot[MAX_N], pot0[MAX_N];

void init(int n_) {
  n = n_; m = 0; memset(ptr, ~0, n * sizeof(int)); memset(ex, 0, n * sizeof(Capa));
}
void ae(int u, int v, Capa c, Cost d) {
  d *= (n + 1);
  next[m] = ptr[u]; ptr[u] = m; zu[m] = v; capa[m] = c; cost[m] = d; ++m;
  next[m] = ptr[v]; ptr[v] = m; zu[m] = u; capa[m] = 0; cost[m] = -d; ++m;
}

bool bellmanFord(Cost eps) {
  memcpy(pot0, pot, n * sizeof(Cost));
  for (int iter = 0; iter < BELLMAN_FORD_NUM_ITERS; ++iter) {
    bool upd = false;
    for (int i = 0; i < m; ++i) {
      if (capa[i] > 0) {
        const int u = zu[i ^ 1], v = zu[i];
        if (pot0[v] > pot0[u] + cost[i] + eps) {
          pot0[v] = pot0[u] + cost[i] + eps;
          upd = true;
        }
      }
    }
    if (!upd) {
      memcpy(pot, pot0, n * sizeof(Cost));
      return true;
    }
  }
  return false;
}

Cost solve() {
  Cost minCost = 0;
  for (int i = 0; i < m; i += 2) if (minCost > cost[i]) minCost = cost[i];
  Cost eps = 1;
  for (; eps < -minCost; eps <<= LOG_SCALING) {}
  memset(pot, 0, n * sizeof(Cost));
  for (; eps >>= LOG_SCALING; ) {
    if (bellmanFord(eps)) continue;
    for (int i = 0; i < m; i += 2) {
      const int u = zu[i ^ 1], v = zu[i];
      const Cost d = cost[i] + pot[u] - pot[v];
      if (capa[i] > 0 && d < 0) {
        Capa &c = capa[i]; ex[u] -= c; ex[v] += c; capa[i ^ 1] += c; c = 0;
      } else if (capa[i ^ 1] > 0 && -d < 0) {
        Capa &c = capa[i ^ 1]; ex[v] -= c; ex[u] += c; capa[i] += c; c = 0;
      }
    }
    memcpy(cur, ptr, n * sizeof(int));
    qb = qe = 0;
    for (int u = 0; u < n; ++u) if (ex[u] > 0) {
      que[qe] = u; ++qe &= QUE_SIZE - 1;
    }
    for (; qb != qe; ) {
      const int u = que[qb]; ++qb &= QUE_SIZE - 1;
      for (int &i = cur[u]; ~i; i = next[i]) {
        if (capa[i] > 0) {
          const int v = zu[i];
          if (cost[i] + pot[u] - pot[v] < 0) {
            const Capa c = min(ex[u], capa[i]);
            if (ex[v] <= 0 && ex[v] + c > 0) {
              que[qe] = v; ++qe &= QUE_SIZE - 1;
            }
            ex[u] -= c; ex[v] += c; capa[i] -= c; capa[i ^ 1] += c;
            if (ex[u] == 0) break;
          }
        }
      }
      if (ex[u] > 0) {
        bool relabeled = false;
        for (int i = ptr[u]; ~i; i = next[i]) {
          if (capa[i] > 0) {
            const Cost p = pot[zu[i]] - cost[i] - eps;
            if (!relabeled || pot[u] < p) {
              relabeled = true; pot[u] = p;
            }
          }
        }
        cur[u] = ptr[u]; que[qe] = u; ++qe &= QUE_SIZE - 1;
      }
    }
  }
  Cost totalCost = 0;
  for (int i = 0; i < m; i += 2) totalCost += cost[i] * capa[i ^ 1];
  return totalCost / (n + 1);
}

}  // namespace MCF


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];
    }
    
    MCF::init(N + N + N + N);
    auto out = [&](int u) -> int { return u; };
    auto in  = [&](int u) -> int { return N + u; };
    auto toL = [&](int u) -> int { return N + N + u; };
    auto toR = [&](int u) -> int { return N + N + N + u; };
    const int big = 2 * N + 10;
    for (int u = 0; u < N; ++u) {
      MCF::ae(in(u), out(u), 1, -big);
      MCF::ae(toL(u), in(u), 1, 0);
      MCF::ae(toR(u), in(u), 1, 0);
      // discard
      if (u - 1 >= 0) MCF::ae(out(u), toL(u - 1), 1, 0);
      if (u + 1 <  N) MCF::ae(out(u), toR(u + 1), 1, 0);
    }
    for (int u = 0; u < N - 1; ++u) {
      MCF::ae(toL(u + 1), toL(u), N, 0);
      MCF::ae(toR(u), toR(u + 1), N, 0);
    }
    // use
    for (int i = 0; i < M; ++i) {
      MCF::ae(out(A[i]), in(B[i]), 1, -1);
      MCF::ae(out(B[i]), in(A[i]), 1, -1);
    }
    
    const int ans = -(MCF::solve() + N * big);
cerr<<"ans = "<<ans<<endl;
    printf("%d\n", 2 * ans - N);
  }
  return 0;
}
0