結果

問題 No.241 出席番号(1)
ユーザー huan han
提出日時 2025-08-21 19:05:15
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,488 bytes
コンパイル時間 862 ms
コンパイル使用メモリ 76,244 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-08-21 19:05:18
合計ジャッジ時間 2,897 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int N, A[50];

int V;
vector<int> G[100];
int match[100];
bool used[100];

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

bool dfs(int v){
        used[v] = true;
        for(int u : G[v]){
                int w = match[u];
                if(w < 0 || (!used[w] && dfs(w))){
                        match[v] = u;
                        match[u] = v;
                        return true;
                }
        }
        return false;
}

int bipartite_matching(){
        int res = 0;
        memset(match, -1, sizeof(match));
        for(int v=0;v<V;v++){
                if(match[v] < 0){
                        memset(used, false, sizeof(used));
                        if(dfs(v)){
                                ++res;
                        }
                }
        }
        return res;
}

int main(){
        cin >> N;
        for(int i=0;i<N;i++){
                cin >> A[i];
        }

        V = N * 2;
        for(int i=0;i<N;i++){
                for(int x=0;x<N;x++){
                        if(x != A[i]){
                                add_edge(i, N + x);
                        }
                }                
        }

        if(bipartite_matching() < N){
                cout << -1 << endl;
                return 0;
        }

        for(int i=0;i<N;i++){
                cout << match[i] - N << endl;
        }
        return 0;
}
0