結果

問題 No.2286 Join Hands
ユーザー KudeKude
提出日時 2023-04-28 22:29:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 3,850 bytes
コンパイル時間 4,878 ms
コンパイル使用メモリ 274,636 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 23:58:51
合計ジャッジ時間 6,689 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 7 ms
5,376 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 8 ms
5,376 KB
testcase_13 AC 7 ms
5,376 KB
testcase_14 AC 10 ms
5,376 KB
testcase_15 AC 9 ms
5,376 KB
testcase_16 AC 11 ms
5,376 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 11 ms
5,376 KB
testcase_19 AC 7 ms
5,376 KB
testcase_20 AC 9 ms
5,376 KB
testcase_21 AC 11 ms
5,376 KB
testcase_22 AC 8 ms
5,376 KB
testcase_23 AC 6 ms
5,376 KB
testcase_24 AC 10 ms
5,376 KB
testcase_25 AC 6 ms
5,376 KB
testcase_26 AC 6 ms
5,376 KB
testcase_27 AC 6 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 11 ms
5,376 KB
testcase_48 AC 12 ms
5,376 KB
testcase_49 AC 12 ms
5,376 KB
testcase_50 AC 11 ms
5,376 KB
testcase_51 AC 3 ms
5,376 KB
testcase_52 AC 4 ms
5,376 KB
testcase_53 AC 12 ms
5,376 KB
testcase_54 AC 5 ms
5,376 KB
testcase_55 AC 6 ms
5,376 KB
testcase_56 AC 12 ms
5,376 KB
testcase_57 AC 12 ms
5,376 KB
testcase_58 AC 12 ms
5,376 KB
testcase_59 AC 12 ms
5,376 KB
testcase_60 AC 5 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;



// https://ei1333.github.io/library/graph/flow/gabow-edmonds.hpp

#line 1 "graph/flow/gabow-edmonds.hpp"
// https://qiita.com/Kutimoti_T/items/5b579773e0a24d650bdf

/**
 * @brief Gabow Edmonds(一般グラフの最大マッチング)
 * @docs docs/gabow-edmonds.md
 */
struct GabowEdmonds {

  struct edge {
    int to, idx;
  };

  vector< vector< edge > > g;
  vector< pair< int, int > > edges;
  vector< int > mate, label, first;
  queue< int > que;

  GabowEdmonds(int n) : g(n + 1), mate(n + 1), label(n + 1, -1), first(n + 1) {}

  void add_edge(int u, int v) {
    ++u, ++v;
    g[u].push_back((edge) {v, (int) (edges.size() + g.size())});
    g[v].push_back((edge) {u, (int) (edges.size() + g.size())});
    edges.emplace_back(u, v);
  }

  int find(int x) {
    if(label[first[x]] < 0) return first[x];
    first[x] = find(first[x]);
    return first[x];
  }

  void rematch(int v, int w) {
    int t = mate[v];
    mate[v] = w;
    if(mate[t] != v) return;
    if(label[v] < (int)g.size()) {
      mate[t] = label[v];
      rematch(label[v], t);
    } else {
      int x = edges[label[v] - g.size()].first;
      int y = edges[label[v] - g.size()].second;
      rematch(x, y);
      rematch(y, x);
    }
  }

  void assign_label(int x, int y, int num) {
    int r = find(x);
    int s = find(y);
    int join = 0;
    if(r == s) return;
    label[r] = -num;
    label[s] = -num;
    while(true) {
      if(s != 0) swap(r, s);
      r = find(label[mate[r]]);
      if(label[r] == -num) {
        join = r;
        break;
      }
      label[r] = -num;
    }
    int v = first[x];
    while(v != join) {
      que.push(v);
      label[v] = num;
      first[v] = join;
      v = first[label[mate[v]]];
    }
    v = first[y];
    while(v != join) {
      que.push(v);
      label[v] = num;
      first[v] = join;
      v = first[label[mate[v]]];
    }
  }

  bool augment_check(int u) {
    que = queue< int >();
    first[u] = 0;
    label[u] = 0;
    que.push(u);
    while(!que.empty()) {
      int x = que.front();
      que.pop();
      for(auto e : g[x]) {
        int y = e.to;
        if(mate[y] == 0 && y != u) {
          mate[y] = x;
          rematch(x, y);
          return true;
        } else if(label[y] >= 0) {
          assign_label(x, y, e.idx);
        } else if(label[mate[y]] < 0) {
          label[mate[y]] = x;
          first[mate[y]] = y;
          que.push(mate[y]);
        }
      }
    }
    return false;
  }

  vector< pair< int, int > > max_matching() {
    for(int i = 1; i < (int)g.size(); i++) {
      if(mate[i] != 0) continue;
      if(augment_check(i)) label.assign(g.size(), -1);
    }
    vector< pair< int, int > > ret;
    for(int i = 1; i < (int)g.size(); i++) {
      if(i < mate[i]) ret.emplace_back(i - 1, mate[i] - 1);
    }
    return ret;
  }
};



int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  GabowEdmonds g(2 * n);
  VI deg(n);
  rep(_, m) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    rep(i, 2) rep(j, 2) g.add_edge(2 * u + i, 2 * v + j);
    deg[u]++;
    deg[v]++;
  }
  int ans = 2 * g.max_matching().size();
  if (ans == 2 * (n - 1) && *min_element(all(deg)) == 0) {
    ans -= 2;
  }
  cout << ans - n << '\n';
}
0