結果

問題 No.2286 Join Hands
ユーザー rgnerdplayerrgnerdplayer
提出日時 2023-05-03 17:43:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 2,558 bytes
コンパイル時間 2,492 ms
コンパイル使用メモリ 210,148 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-01 15:00:19
合計ジャッジ時間 4,018 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 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 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 3 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 1 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 3 ms
5,376 KB
testcase_48 AC 4 ms
5,376 KB
testcase_49 AC 4 ms
5,376 KB
testcase_50 AC 4 ms
5,376 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 3 ms
5,376 KB
testcase_53 AC 3 ms
5,376 KB
testcase_54 AC 3 ms
5,376 KB
testcase_55 AC 3 ms
5,376 KB
testcase_56 AC 4 ms
5,376 KB
testcase_57 AC 3 ms
5,376 KB
testcase_58 AC 4 ms
5,376 KB
testcase_59 AC 3 ms
5,376 KB
testcase_60 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using i64 = long long;

struct BipartiteMatching {
    int n, m;
    vector<vector<int>> g;
    vector<int> l, r, dis, cur;
    BipartiteMatching(int n, int m) : n(n), m(m), g(n), l(n, -1), r(m, -1), dis(n), cur(n) {}
    void addEdge(int u, int v) { g[u].push_back(v); }
    void bfs() {
        vector<int> q;
        for (int u = 0; u < n; u++) {
            if (l[u] == -1) {
                q.push_back(u), dis[u] = 0;
            } else {
                dis[u] = -1;
            }
        }
        for (int i = 0; i < int(q.size()); i++) {
            int u = q[i];
            for (auto v : g[u]) {
                if (r[v] != -1 && dis[r[v]] == -1) {
                    dis[r[v]] = dis[u] + 1;
                    q.push_back(r[v]);
                }
            }
        }
    }
    bool dfs(int u) {
        for (int &i = cur[u]; i < int(g[u].size()); i++) {
            int v = g[u][i];
            if (r[v] == -1 || dis[r[v]] == dis[u] + 1 && dfs(r[v])) {
                l[u] = v, r[v] = u;
                return true;
            }
        }
        return false;
    }
    int maxMatching() {
        int match = 0;
        while (true) {
            bfs();
            fill(cur.begin(), cur.end(), 0);
            int cnt = 0;
            for (int u = 0; u < n; u++) {
                if (l[u] == -1) {
                    cnt += dfs(u);
                }
            }
            if (!cnt) {
                break;
            }
            match += cnt;
        }
        return match;
    }
    auto minVertexCover() {
        vector<int> L, R;
        for (int u = 0; u < n; u++) {
            if (dis[u] == -1) {
                L.push_back(u);
            } else if (l[u] != -1) {
                R.push_back(l[u]);
            }
        }
        return pair(L, R);
    }
};

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    auto solve = [&]() {
        int n, m;
        cin >> n >> m;

        BipartiteMatching bm(n, n);
        vector<int> deg(n);

        for (int i = 0; i < m; i++) {
            int u, v;
            cin >> u >> v;
            bm.addEdge(--u, --v);
            bm.addEdge(v, u);
            deg[u]++, deg[v]++;
        }

        int ans = 2 * bm.maxMatching() - n;

        if (ans == n - 2 && count(deg.begin(), deg.end(), 0) > 0) {
            ans -= 2;
        }

        cout << ans << '\n';
    };
    
    solve();
    
    return 0;
}
0