結果

問題 No.2286 Join Hands
ユーザー みここみここ
提出日時 2023-03-07 22:57:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,257 bytes
コンパイル時間 978 ms
コンパイル使用メモリ 88,128 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-10-18 05:37:52
合計ジャッジ時間 5,270 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <queue>
using namespace std;

struct BipartiteMaximumMatching
{
public:
    BipartiteMaximumMatching() {}

    BipartiteMaximumMatching(int p, int q) : p(p), q(q), g(p), match(q, -1) {}

    void add_directed_edge(int a, int b)
    {
        assert(0 <= a && a < p);
        assert(0 <= b && b < q);
        g[a].push_back(b);
    }

    int get_maximum_matching()
    {
        std::vector<bool> covered(p);
        std::vector<int> dist(p);
        std::vector<int> nx(p);
        int max_length;
        int res = 0;

        auto bfs = [&]()
        {
            std::queue<int> que;
            for (int u = 0; u < p; u++)
            {
                if (!covered[u])
                {
                    que.push(u);
                    dist[u] = 0;
                }
                else
                {
                    dist[u] = -1;
                }
            }
            while (que.size())
            {
                int u = que.front();
                que.pop();
                for (int v : g[u])
                {
                    if (match[v] == -1)
                    {
                        max_length = dist[u];
                        return;
                    }
                    if (match[v] != u)
                    {
                        dist[match[v]] = dist[u] + 1;
                        que.push(match[v]);
                    }
                }
            }
        };

        auto dfs = [&](auto self, int u) -> bool
        {
            for (int &i = nx[u]; i < (int)g[u].size(); i++)
            {
                int v = g[u][i];
                int w = match[v];
                if (w == -1 || (dist[w] == dist[u] + 1 && dist[w] <= max_length && self(self, w)))
                {
                    match[v] = u;
                    covered[u] = true;
                    return true;
                }
            }
            return false;
        };

        while (true)
        {
            max_length = -1;
            bfs();
            if (max_length == -1)
            {
                return res;
            }
            nx.assign(p, 0);
            for (int u = 0; u < p; u++)
            {
                res += (!covered[u] && dfs(dfs, u));
            }
        }
    }

    std::vector<std::pair<int, int>> get_matching_edges()
    {
        std::vector<std::pair<int, int>> res;
        for (int v = 0; v < q; v++)
        {
            if (match[v] >= 0)
            {
                res.push_back(std::pair<int, int>(match[v], v));
            }
        }
        return res;
    }

private:
    int p, q;
    std::vector<std::vector<int>> g;
    std::vector<int> match;
};

int main()
{
    int n, m;
    cin >> n >> m;
    BipartiteMaximumMatching g(n, n);
    bool b[200005]{0};
    for (int i = 0; i < m; i++)
    {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        b[u] = b[v] = true;
        g.add_directed_edge(u, v);
        g.add_directed_edge(v, u);
    }
    int ans = g.get_maximum_matching() * 2;
    int c = 0;
    for (int u = 0; u < n; u++)
    {
        c += b[u];
    }
    if (ans == n * 2 - 2 && c == n - 1)
    {
        ans -= 2;
    }
    cout << ans - n << endl;
}
0