結果

問題 No.3107 Listening
ユーザー siro53siro53
提出日時 2024-04-01 21:52:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,928 ms / 2,000 ms
コード長 3,087 bytes
コンパイル時間 3,584 ms
コンパイル使用メモリ 222,792 KB
実行使用メモリ 93,380 KB
最終ジャッジ日時 2024-04-01 21:53:29
合計ジャッジ時間 52,500 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,049 ms
57,472 KB
testcase_04 AC 123 ms
13,568 KB
testcase_05 AC 202 ms
17,152 KB
testcase_06 AC 150 ms
16,128 KB
testcase_07 AC 644 ms
35,968 KB
testcase_08 AC 307 ms
22,656 KB
testcase_09 AC 521 ms
34,432 KB
testcase_10 AC 1,127 ms
58,496 KB
testcase_11 AC 1,444 ms
75,264 KB
testcase_12 AC 334 ms
23,680 KB
testcase_13 AC 1,023 ms
56,832 KB
testcase_14 AC 477 ms
29,312 KB
testcase_15 AC 353 ms
25,856 KB
testcase_16 AC 146 ms
14,080 KB
testcase_17 AC 210 ms
16,128 KB
testcase_18 AC 66 ms
9,728 KB
testcase_19 AC 665 ms
43,008 KB
testcase_20 AC 773 ms
49,536 KB
testcase_21 AC 1,571 ms
84,608 KB
testcase_22 AC 1,052 ms
63,232 KB
testcase_23 AC 1,470 ms
78,592 KB
testcase_24 AC 1,388 ms
77,056 KB
testcase_25 AC 782 ms
45,440 KB
testcase_26 AC 156 ms
16,128 KB
testcase_27 AC 523 ms
38,528 KB
testcase_28 AC 1,176 ms
68,608 KB
testcase_29 AC 1,264 ms
71,424 KB
testcase_30 AC 422 ms
29,952 KB
testcase_31 AC 616 ms
37,632 KB
testcase_32 AC 1,177 ms
62,080 KB
testcase_33 AC 1,844 ms
93,252 KB
testcase_34 AC 1,848 ms
93,252 KB
testcase_35 AC 1,910 ms
93,340 KB
testcase_36 AC 1,846 ms
93,368 KB
testcase_37 AC 1,791 ms
93,252 KB
testcase_38 AC 1,865 ms
93,380 KB
testcase_39 AC 1,875 ms
93,380 KB
testcase_40 AC 1,821 ms
93,380 KB
testcase_41 AC 1,928 ms
93,364 KB
testcase_42 AC 1,873 ms
93,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct GeneralMatching {
    struct E {
        int a, b;
    };
    const vector<vector<int>>& g;
    int n, sz;
    vector<int> p, t, mt;
    vector<E> f;
    int non(int v) { return t[v] != sz or p[v] == -1 ? v : (p[v] = non(p[v])); }
    void R(int a, int b) {
        int d = mt[a];
        mt[a] = b;
        if (d == -1 or mt[d] != a) return;
        if (f[a].b == -1) {
            mt[d] = f[a].a;
            R(f[a].a, d);
        } else {
            R(f[a].a, f[a].b);
            R(f[a].b, f[a].a);
        }
    }
    bool arg(int rt) {
        queue<int> q;
        t[rt] = sz;
        p[rt] = -1;
        f[rt] = E{-1, -1};
        q.push(rt);
        while (!q.empty()) {
            int a = q.front();
            q.pop();
            for (auto b : g[a]) {
                if (b == rt) continue;
                if (mt[b] == -1) {
                    mt[b] = a;
                    R(a, b);
                    return true;
                }
                if (t[b] == sz) {
                    int x = non(a), y = non(b);
                    if (x == y) continue;
                    int z = rt;
                    while (x != rt or y != rt) {
                        if (y != rt) swap(x, y);
                        if (f[x].a == a and f[x].b == b) {
                            z = x;
                            break;
                        }
                        f[x] = E{a, b};
                        x = non(f[mt[x]].a);
                    }
                    for (auto v : {non(a), non(b)}) {
                        while (v != z) {
                            t[v] = sz;
                            p[v] = z;
                            q.push(v);
                            v = non(f[mt[v]].a);
                        }
                    }
                    continue;
                }
                if (t[mt[b]] == sz) continue;
                f[b] = E{-1, -1};
                t[mt[b]] = sz;
                p[mt[b]] = b;
                f[mt[b]] = E{a, -1};
                q.push(mt[b]);
            }
        }
        return false;
    }
    GeneralMatching(const vector<vector<int>>& gg) : g(gg), n(g.size()), sz(0), p(n), t(n, -1), mt(n, -1), f(n) {
        for (int i = 0; i < n; i++) {
            if (mt[i] == -1) {
                sz += arg(i);
            }
        }
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    map<pair<int, int>, int> ID; 
    for(int i = 1; i <= M; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
        ID[minmax(u, v)] = i;
    }

    GeneralMatching GM(G);
    cout << GM.sz << '\n';
    for (int i = 0; i < N; i++) {
        if (i < GM.mt[i]) {
            cout << ID[minmax(i, GM.mt[i])] << '\n';
            // cout << i << ' ' << GM.mt[i] << '\n';
        }
    }
}

/*
https://judge.yosupo.jp/submission/116845
ここからパクりました。本当にごめんなさい...
*/
0