結果

問題 No.3107 Listening
ユーザー kotatsugamekotatsugame
提出日時 2024-04-01 21:37:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,274 bytes
コンパイル時間 1,432 ms
コンパイル使用メモリ 102,228 KB
実行使用メモリ 116,808 KB
最終ジャッジ日時 2024-04-01 21:38:00
合計ジャッジ時間 38,523 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,480 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1,108 ms
71,176 KB
testcase_04 AC 119 ms
15,688 KB
testcase_05 AC 412 ms
19,652 KB
testcase_06 AC 164 ms
18,924 KB
testcase_07 AC 1,058 ms
44,188 KB
testcase_08 AC 706 ms
26,016 KB
testcase_09 AC 551 ms
40,964 KB
testcase_10 AC 943 ms
73,380 KB
testcase_11 AC 1,376 ms
92,640 KB
testcase_12 AC 683 ms
27,532 KB
testcase_13 AC 1,138 ms
70,356 KB
testcase_14 AC 795 ms
36,492 KB
testcase_15 AC 331 ms
31,848 KB
testcase_16 AC 224 ms
15,964 KB
testcase_17 AC 489 ms
18,292 KB
testcase_18 AC 74 ms
11,080 KB
testcase_19 AC 798 ms
51,516 KB
testcase_20 AC 779 ms
60,856 KB
testcase_21 TLE -
testcase_22 AC 1,176 ms
77,808 KB
testcase_23 AC 1,905 ms
96,464 KB
testcase_24 AC 1,333 ms
94,440 KB
testcase_25 AC 1,250 ms
54,712 KB
testcase_26 AC 487 ms
18,384 KB
testcase_27 AC 547 ms
47,100 KB
testcase_28 AC 1,282 ms
83,964 KB
testcase_29 AC 1,679 ms
88,048 KB
testcase_30 AC 766 ms
37,048 KB
testcase_31 AC 864 ms
45,636 KB
testcase_32 AC 1,309 ms
76,792 KB
testcase_33 TLE -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:143:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  143 |         for(auto[u,v]:ans)cout<<idx[minmax(u,v)]+1<<"\n";
      |                 ^

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cassert>
using namespace std;
//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;
  }
};
#include<map>
map<pair<int,int>,int>idx;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,M;
	cin>>N>>M;
	GabowEdmonds G(N);
	for(int i=0;i<M;i++)
	{
		int u,v;cin>>u>>v;
		u--,v--;
		G.add_edge(u,v);
		idx[minmax(u,v)]=i;
	}
	vector<pair<int,int> >ans=G.max_matching();
	cout<<ans.size()<<"\n";
	for(auto[u,v]:ans)cout<<idx[minmax(u,v)]+1<<"\n";
}
0