結果

問題 No.2286 Join Hands
ユーザー lgswdnlgswdn
提出日時 2024-03-17 10:27:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 2,105 bytes
コンパイル時間 2,390 ms
コンパイル使用メモリ 203,308 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-17 10:27:46
合計ジャッジ時間 5,132 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 11 ms
6,676 KB
testcase_09 AC 12 ms
6,676 KB
testcase_10 AC 25 ms
6,676 KB
testcase_11 AC 17 ms
6,676 KB
testcase_12 AC 19 ms
6,676 KB
testcase_13 AC 21 ms
6,676 KB
testcase_14 AC 15 ms
6,676 KB
testcase_15 AC 17 ms
6,676 KB
testcase_16 AC 15 ms
6,676 KB
testcase_17 AC 10 ms
6,676 KB
testcase_18 AC 10 ms
6,676 KB
testcase_19 AC 5 ms
6,676 KB
testcase_20 AC 7 ms
6,676 KB
testcase_21 AC 10 ms
6,676 KB
testcase_22 AC 6 ms
6,676 KB
testcase_23 AC 3 ms
6,676 KB
testcase_24 AC 9 ms
6,676 KB
testcase_25 AC 5 ms
6,676 KB
testcase_26 AC 4 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 3 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 3 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 3 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
testcase_39 AC 2 ms
6,676 KB
testcase_40 AC 2 ms
6,676 KB
testcase_41 AC 2 ms
6,676 KB
testcase_42 AC 2 ms
6,676 KB
testcase_43 AC 2 ms
6,676 KB
testcase_44 AC 2 ms
6,676 KB
testcase_45 AC 3 ms
6,676 KB
testcase_46 AC 2 ms
6,676 KB
testcase_47 AC 4 ms
6,676 KB
testcase_48 AC 4 ms
6,676 KB
testcase_49 AC 4 ms
6,676 KB
testcase_50 AC 4 ms
6,676 KB
testcase_51 AC 3 ms
6,676 KB
testcase_52 AC 3 ms
6,676 KB
testcase_53 AC 4 ms
6,676 KB
testcase_54 AC 5 ms
6,676 KB
testcase_55 AC 4 ms
6,676 KB
testcase_56 AC 3 ms
6,676 KB
testcase_57 AC 4 ms
6,676 KB
testcase_58 AC 4 ms
6,676 KB
testcase_59 AC 4 ms
6,676 KB
testcase_60 AC 4 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 10;
int hd[10010], to[N], nx[N], W[N], tt = 1;
void add(int u, int v, int f) { to[++tt] = v, nx[tt] = hd[u], hd[u] = tt, W[tt] = f; }
void link(int u, int v, int f) { add(u, v, f), add(v, u, 0); }
int dep[N], cur[10010], q[N], l, r, S, T;
bool bfs() {
    q[l = r = 1] = S;
    memset(dep, 0, sizeof dep);
    dep[S] = 1;
    memcpy(cur, hd, sizeof cur);
    while (l <= r) {
        int u = q[l++];
        // cerr<<"POP: "<<u<<endl;
        for (int i = hd[u]; i; i = nx[i]) {
            int v = to[i];
            // cerr<<u<<"->"<<i<<" "<<W[i]<<" "<<v<<" "<<dep[v]<<endl;
            if (W[i] && !dep[v]) {
                dep[v] = dep[u] + 1;
                q[++r] = v;
            }
        }
    }
    return dep[T];
}
int dfs(int u, int f) {
    // cerr<<"DFS: "<<u<<" "<<f<<endl;
    if (u == T)
        return f;
    int r = 0, k;
    for (int& i = cur[u]; i; i = nx[i]) {
        int v = to[i];
        if (dep[v] == dep[u] + 1 && W[i]) {
            k = dfs(v, min(f, W[i]));
            r += k, f -= k;
            W[i] -= k, W[i ^ 1] += k;
        }
    }
    return r;
}
int Dinic() {
    int flow = 0;
    while (bfs()) /*cerr<<flow<<" : ",*/
        flow += dfs(S, 0x3f3f3f3f);
    return flow;
}
int ban[N];
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int C, Tr=1;
    while (Tr--) {
        int n, m;
        cin >> n >> m;
        tt = 1, S = n * 2 + 1, T = n * 2 + 2;
        memset(hd, 0, sizeof hd);
        memset(ban, 0, sizeof ban);
        for (int i = 1; i <= n; ++i) link(S, i, 1);
        for (int i = 1; i <= n; ++i) link(n + i, T, 1);
        while (m--) {
            int u, v;
            cin >> u >> v;
            link(u, v + n, 1);
            link(v, u + n, 1);
            ban[u] = ban[v] = 1;
        }
        int cnt = 0;
        for (int i = 1; i <= n; ++i) cnt += !ban[i];
        int res = Dinic();
        if (cnt == 1 && res == n - 1) {
            res--;
            // cerr<<"!!\n";
        }
        // cerr<<res<<endl;
        cout << res * 2 - n << endl;
    }
    return 0;
}
0