結果

問題 No.2286 Join Hands
ユーザー rgnerdplayerrgnerdplayer
提出日時 2023-05-03 17:41:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,418 bytes
コンパイル時間 2,469 ms
コンパイル使用メモリ 209,912 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-05-01 14:59:13
合計ジャッジ時間 3,899 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 5 ms
6,940 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 5 ms
6,940 KB
testcase_13 AC 4 ms
6,940 KB
testcase_14 AC 5 ms
6,944 KB
testcase_15 AC 5 ms
6,944 KB
testcase_16 AC 5 ms
6,940 KB
testcase_17 AC 4 ms
6,940 KB
testcase_18 AC 4 ms
6,944 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 4 ms
6,944 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 3 ms
6,944 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 3 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 1 ms
6,940 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 WA -
testcase_36 AC 1 ms
6,944 KB
testcase_37 WA -
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 WA -
testcase_41 AC 1 ms
6,940 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 WA -
testcase_44 AC 2 ms
6,940 KB
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 4 ms
6,940 KB
testcase_48 AC 3 ms
6,940 KB
testcase_49 AC 3 ms
6,944 KB
testcase_50 WA -
testcase_51 AC 2 ms
6,948 KB
testcase_52 AC 3 ms
6,940 KB
testcase_53 AC 4 ms
6,940 KB
testcase_54 AC 3 ms
6,940 KB
testcase_55 AC 3 ms
6,940 KB
testcase_56 AC 4 ms
6,944 KB
testcase_57 AC 3 ms
6,940 KB
testcase_58 AC 4 ms
6,940 KB
testcase_59 AC 3 ms
6,944 KB
testcase_60 AC 3 ms
6,940 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);

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

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

        cout << ans - 2 * (ans == n - 2) << '\n';
    };
    
    solve();
    
    return 0;
}
0