結果

問題 No.3107 Listening
ユーザー harurunharurun
提出日時 2024-03-07 04:44:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,999 bytes
コンパイル時間 6,895 ms
コンパイル使用メモリ 440,612 KB
実行使用メモリ 94,340 KB
最終ジャッジ日時 2024-04-01 20:52:14
合計ジャッジ時間 46,450 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 1,247 ms
55,024 KB
testcase_04 AC 152 ms
13,204 KB
testcase_05 AC 466 ms
16,580 KB
testcase_06 AC 211 ms
15,476 KB
testcase_07 AC 1,205 ms
34,668 KB
testcase_08 AC 806 ms
22,392 KB
testcase_09 AC 668 ms
36,332 KB
testcase_10 AC 920 ms
55,992 KB
testcase_11 AC 1,399 ms
73,648 KB
testcase_12 AC 722 ms
23,320 KB
testcase_13 AC 1,204 ms
54,552 KB
testcase_14 AC 925 ms
30,156 KB
testcase_15 AC 335 ms
26,332 KB
testcase_16 AC 270 ms
13,744 KB
testcase_17 AC 581 ms
15,904 KB
testcase_18 AC 93 ms
9,500 KB
testcase_19 AC 869 ms
42,024 KB
testcase_20 AC 805 ms
48,884 KB
testcase_21 TLE -
testcase_22 AC 1,240 ms
70,248 KB
testcase_23 TLE -
testcase_24 AC 1,305 ms
74,896 KB
testcase_25 AC 1,373 ms
44,260 KB
testcase_26 AC 490 ms
15,864 KB
testcase_27 AC 623 ms
38,912 KB
testcase_28 AC 1,386 ms
70,920 KB
testcase_29 AC 1,727 ms
72,340 KB
testcase_30 AC 841 ms
29,952 KB
testcase_31 AC 1,017 ms
38,328 KB
testcase_32 AC 1,323 ms
58,896 KB
testcase_33 TLE -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <queue>
#include <string>
#include <iomanip>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <utility>
#include <stack>
#include <random>
#include <complex>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
using cpp_int=boost::multiprecision::cpp_int;
#endif
#if __has_include(<boost/multiprecision/cpp_dec_float.hpp>)
#include <boost/multiprecision/cpp_dec_float.hpp>
template<unsigned size>using cpp_float=boost::multiprecision::number<boost::multiprecision::cpp_dec_float<size>>;
template<unsigned size>using cpp_double=boost::multiprecision::number<boost::multiprecision::cpp_dec_float<size,long long>>;
#endif
using namespace std;
using ll=long long;
#define read(x) cin>>(x);
#define readll(x) ll (x);cin>>(x);
#define readS(x) string (x);cin>>(x);
#define readvll(x,N) vector<ll> (x)((N));for(int i=0;i<(N);i++){cin>>(x)[i];}
#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)
#define rep2d(i,j,H,W) for(ll (i)=0;(i)<(H);(i)++)for(ll (j)=0;(j)<(W);j++)
#define is_in(x,y) (0<=(x) && (x)<H && 0<=(y) && (y)<W)
#define yn {cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define double_out(x) fixed << setprecision(x)
template<class T> inline void erase_duplicate(vector<T>& A){sort(A.begin(),A.end());A.erase(unique(A.begin(),A.end()),A.end());}
inline ll powll(ll x,ll n){ll r=1;while(n>0){if(n&1){r*=x;};x*=x;n>>=1;};return r;}

// https://ei1333.github.io/library/graph/flow/gabow-edmonds.hpp.html
// 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;
  }
};

inline ll conv(ll x,ll y){
  return 10'000'000LL*x+y;
}

int main(){
  ll N,M;
  cin>>N>>M;
  GabowEdmonds GE(N);
  unordered_map<ll,ll> index;
  for(ll i=0;i<M;i++){
    ll u,v;
    cin>>u>>v;
    u--;v--;
    ll vmin=min(u,v),vmax=max(u,v);
    GE.add_edge(vmin,vmax);
    index[conv(vmin,vmax)]=i+1;
  }
  auto res=GE.max_matching();
  cout<<res.size()<<endl;
  for(ll i=0;i<res.size();i++){
    auto p=res[i];
    ll vmin=min(p.first,p.second);
    ll vmax=max(p.first,p.second);
    cout<<index[conv(vmin,vmax)]<<endl;
  }
}
0