結果

問題 No.241 出席番号(1)
ユーザー pekempeypekempey
提出日時 2015-07-10 22:43:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,173 bytes
コンパイル時間 1,440 ms
コンパイル使用メモリ 158,400 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-20 17:43:20
合計ジャッジ時間 2,848 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;

int N, A[50];

class Dinic {
    public:
        struct edge { int to, cap, rev; };
        vector<vector<edge> > G;
        Dinic() {}

        Dinic(int V) {
            resize(V);
        }

        void resize(int V) {
            G.resize(V);
            level.resize(V);
            iter.resize(V);
        }

        void add(int u, int v, int cap) {
            G[u].push_back((edge){v, cap, (int)G[v].size()});
            G[v].push_back((edge){u, 0, (int)G[u].size() - 1});
        }

        int calc(int s, int t) {
            int res = 0;
            for (;;) {
                bfs(s);
                if (level[t] < 0) return res;
                fill(iter.begin(), iter.end(), 0);
                int f;
                while ((f = dfs(s, t, inf)) > 0) {
                    res += f;
                }
            }
        }

    private: 
        vector<int> level;
        vector<int> iter;

        void bfs(int s) {
            fill(level.begin(), level.end(), -1);
            queue<int> q;
            q.push(s);

            level[s] = 0;

            while (!q.empty()) {
                int v = q.front(); q.pop();

                for (int i = 0; i < G[v].size(); i++) {
                    edge &e = G[v][i];
                    if (e.cap == 0) continue;
                    if (level[e.to] >= 0) continue;

                    level[e.to] = level[v] + 1;
                    q.push(e.to);
                }
            }
        }

        int dfs(int v, int t, int f) {
            if (v == t) return f;
            for (int &i = iter[v]; i < G[v].size(); i++) {
                edge &e = G[v][i];
                if (e.cap == 0) continue;
                if (level[v] >= level[e.to]) continue;

                int flow = dfs(e.to, t, min(f, e.cap));

                if (flow == 0) continue;

                e.cap -= flow;
                G[e.to][e.rev].cap += flow;
                return flow;
            }
            return 0;
        }
};

class BipartiteMatching {
public:
    Dinic dinic;
    BipartiteMatching(int U, int V) : U(U), V(V) {
        dinic.resize(U + V + 2);
        rep (i, U) dinic.add(U + V, i, 1);
        rep (j, V) dinic.add(j + U, U + V + 1, 1);
    }

    void add(int u, int v) {
        dinic.add(u, U + v, 1);
    }

    int calc() {
        return dinic.calc(U + V, U + V + 1);
    }

private:
    int U, V;
};

int main() {
    cin >> N;
    rep (i, N) cin >> A[i];

    BipartiteMatching m(N, N);
    
    rep (i, N) {
        rep (j, N) if (j != A[i]) {
            m.add(i, j);
        }
    }

    m.calc();

    vector<int> ans;
    bool ok = true;

    rep (i, N) {
        for (auto e : m.dinic.G[i]) {
            if (e.cap == 0 && e.to - N < N) {
                ans.push_back(e.to - N);
            }
        }
    }

    if (ans.size() == N) {
        for (auto x : ans) {
            cout << x << endl;
        }
    } else {
        cout << -1 << endl;
    }

    return 0;
}
0