結果

問題 No.2288 Somen Sliders
ユーザー みここみここ
提出日時 2023-04-26 23:11:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 4,002 bytes
コンパイル時間 1,176 ms
コンパイル使用メモリ 96,828 KB
実行使用メモリ 11,588 KB
最終ジャッジ日時 2024-04-28 22:52:26
合計ジャッジ時間 4,564 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 103 ms
8,736 KB
testcase_11 AC 48 ms
6,016 KB
testcase_12 AC 102 ms
9,196 KB
testcase_13 AC 123 ms
11,588 KB
testcase_14 AC 125 ms
11,360 KB
testcase_15 AC 109 ms
9,968 KB
testcase_16 AC 49 ms
6,380 KB
testcase_17 AC 40 ms
5,376 KB
testcase_18 AC 61 ms
7,312 KB
testcase_19 AC 67 ms
7,900 KB
testcase_20 AC 67 ms
8,036 KB
testcase_21 AC 67 ms
8,008 KB
testcase_22 AC 114 ms
9,400 KB
testcase_23 AC 86 ms
7,472 KB
testcase_24 AC 6 ms
5,376 KB
testcase_25 AC 42 ms
6,400 KB
testcase_26 AC 12 ms
5,376 KB
testcase_27 AC 37 ms
5,376 KB
testcase_28 AC 27 ms
5,376 KB
testcase_29 AC 16 ms
5,376 KB
testcase_30 AC 57 ms
8,528 KB
testcase_31 AC 19 ms
5,376 KB
testcase_32 AC 17 ms
5,376 KB
testcase_33 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cassert>
#include <queue>
using namespace std;
typedef long long ll;

template <typename T = int>
struct stable_matching
{
public:
    struct Edge
    {
        int a, b;
        int preference_a, preference_b; // The smaller the value, the higher the priority.
        bool is_matched;
        T cost;
        int id;

        bool operator<(const Edge &e) const
        {
            return preference_a < e.preference_a;
        }
    };

    stable_matching() {}

    stable_matching(int p, int q) : p(p), q(q), m(0)
    {
        g.resize(p);
    }

    void add_directed_edge(int a, int b, int preference_a, int preference_b, T cost = 1)
    {
        assert(0 <= a && a < p);
        assert(0 <= b && b < q);
        epos.push_back(std::pair<int, int>(a, (int)g[a].size()));
        g[a].push_back((Edge){a, b, preference_a, preference_b, false, cost, m++});
    }

    void get_stable_matching()
    {
        for (int a = 0; a < p; a++)
        {
            if (!std::is_sorted(g[a].begin(), g[a].end()))
            {
                std::sort(g[a].begin(), g[a].end());
            }
        }
        std::vector<std::pair<int, int>> match(q, std::pair<int, int>(-1, -1));
        std::vector<int> nx(p, 0);
        std::queue<int> que;
        for (int a = 0; a < p; a++)
        {
            que.push(a);
        }
        while (que.size())
        {
            int a = que.front();
            que.pop();
            for (int &i = nx[a]; i < (int)g[a].size(); i++)
            {
                int b = g[a][i].b;
                int x = match[b].first;
                int j = match[b].second;
                if (x >= 0 && g[a][i].preference_b < g[x][j].preference_b)
                {
                    g[x][j].is_matched = false;
                    nx[x]++;
                    que.push(x);
                    x = -1;
                }
                if (x == -1)
                {
                    g[a][i].is_matched = true;
                    match[b] = std::pair<int, int>(a, i);
                    break;
                }
            }
        }
    }

    std::vector<Edge> get_matching_edges()
    {
        std::vector<Edge> res;
        for(int i = 0; i < m; i++)
        {
            Edge e = g[epos[i].first][epos[i].second];
            if(e.is_matched)
            {
                res.push_back(e);
            }
        }
        return res;
    }

    Edge &get_edge(int id)
    {
        assert(id < m);
        return g[epos[id].first][epos[id].second];
    }

    int get_size()
    {
        return p + q;
    }

    int get_left_size()
    {
        return p;
    }

    int get_right_size()
    {
        return q;
    }

    int get_edge_size()
    {
        return m;
    }

private:
    int p, q, m;
    std::vector<std::vector<Edge>> g;
    std::vector<std::pair<int, int>> epos;
};

void solve()
{
    int n, x, y;
    cin >> n >> x >> y;
    int p[200005], d[200005];
    for (int u = 0; u < n; u++)
    {
        p[u] = -1;
    }
    int k[200005];
    int _;
    for (int i = 0; i < x; i++)
    {
        cin >> k[i];
        cin >> _;
        for (int j = 1; j < k[i]; j++)
        {
            int u;
            cin >> u;
            u--;
            p[u] = i;
            d[u] = j;
        }
        cin >> _;
    }
    stable_matching<int> g(y, x);
    int m = 0;
    for (int i = 0; i < y; i++)
    {
        int l;
        cin >> l;
        cin >> _;
        for (int j = 1; j < l; j++)
        {
            int v;
            cin >> v;
            v--;
            if (p[v] >= 0)
            {
                g.add_directed_edge(i, p[v], -j, d[v]);
            }
        }
        cin >> _;
    }
    g.get_stable_matching();
    int ans = 0;
    for (int i = 0; i < x; i++)
    {
        ans += k[i];
    }
    auto M = g.get_matching_edges();
    for (auto &e : M)
    {
        ans -= k[e.b] - e.preference_b;
    }
    cout << ans << endl;
}

int main()
{
    solve();
}
0