結果

問題 No.241 出席番号(1)
ユーザー legosukelegosuke
提出日時 2020-03-04 04:50:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,263 bytes
コンパイル時間 2,515 ms
コンパイル使用メモリ 209,768 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 00:56:28
合計ジャッジ時間 3,796 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 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 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

class HopcroftKarp {
    void bfs() {
        const int n = g.size();
        dist.assign(n, -1);
        queue<int> que;
        for (int i = 0; i < n; ++i) if (!used[i]) {
            que.push(i);
            dist[i] = 0;
        }
        while (!que.empty()) {
            int v = que.front();
            que.pop();
            for (int& u : g[v]) {
                int w = match[u];
                if (w >= 0 && dist[w] == -1) {
                    dist[w] = dist[v] + 1;
                    que.emplace(w);
                }
            }
        }
    }

    bool dfs(int v) {
        vv[v] = true;
        for (auto& u : g[v]) {
            int w = match[u];
            if (w < 0 || (!vv[w] && dist[w] == dist[v] + 1 && dfs(w))) {
                match[u] = v;
                used[v] = true;
                return true;
            }
        }
        return false;
    }
    
public:
    vector<vector<int>> g;
    vector<int> dist, match;
    vector<bool> used, vv;

    HopcroftKarp(int n, int m) : g(n), match(m, -1), used(n) {}

    void add_edge(int u, int v) {
        g[u].push_back(v);
    }

    int bipartite_matching() {
        const int n = g.size();
        int res = 0;
        while (true) {
            bfs();
            vv.assign(n, false);
            int flow = 0;
            for (int i = 0; i < n; ++i) {
                if (!used[i] && dfs(i)) ++flow;
            }
            if (!flow) break;
            res += flow;
        }
        return res;
    }

    void print() {
        for (int i = 0; i < match.size(); ++i) {
            if (~match[i]) cout << match[i] << " - " << i << endl;
        }
    }
};

int N;
int A[50];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N;
    HopcroftKarp g(N, N);
    REP (i, N) {
        cin >> A[i];
        REP (j, N) if (j != A[i]) {
            g.add_edge(i, j);
        }
    }
    if (g.bipartite_matching() < N) {
        cout << -1 << endl;
        return 0;
    }
    vector<int> ans(N);
    REP (i, N) ans[g.match[i]] = i;
    REP (i, N) cout << ans[i] << endl;
}
0