結果

問題 No.241 出席番号(1)
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-12 11:31:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,695 bytes
コンパイル時間 520 ms
コンパイル使用メモリ 62,264 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-20 17:54:53
合計ジャッジ時間 1,761 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

const int kMAX_V = 60 * 2;

struct BipartiteMatching {
    int V;  // 頂点数
    vector<int> graph[kMAX_V]; // グラフの隣接リスト表現
    int match[kMAX_V];  // 各頂点ごとのマッチングの対応
    bool used[kMAX_V];  // DFSの際に使用

    BipartiteMatching() { }

    void Init(int v) {
        V = v;
    }

    // uとvを連結にする
    void AddEdge(int u, int v) {
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    bool DFS(int v) {
        used[v] = true;
        for (int i = 0; i < graph[v].size(); i++) {
            int u = graph[v][i], w = match[u];
            if (w < 0 || (!used[w] && DFS(w))) {
                match[v] = u;
                match[u] = v;
                return true;
            }
        }
        return false;
    }

    // 二部マッチングを解く
    int Solve() {
        int res = 0;
        memset(match, -1, sizeof(match));
        for (int v = 0; v < V; v++) {
            if (match[v] < 0) {
                memset(used, sizeof(used), false);
                if (DFS(v)) {
                    res++;
                }
            }
        }
        return res;
    }
};

int N;
BipartiteMatching BM;

int main() {
    cin >> N;

    BM.Init(N * 2);

    for (int i = 0; i < N; i++) {
        int a;
        cin >> a;
        for (int j = 0; j < N; j++) {
            if (j == a) continue;
            BM.AddEdge(i, N + j);
        }
    }

    if (BM.Solve() == N) {
        for (int i = 0; i < N; i++) {
            cout << BM.match[i] - N << endl;
        }
    } else {
        cout << -1 << endl;
    }

}
0