結果

問題 No.241 出席番号(1)
ユーザー Manuel1024Manuel1024
提出日時 2021-04-09 22:48:49
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,952 bytes
コンパイル時間 1,103 ms
コンパイル使用メモリ 81,484 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-07 12:25:01
合計ジャッジ時間 3,195 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

template<class T>
class MaxFlowGraph{
private:
    struct Edge{
        int to;
        T cap;
        int rev;
    };
    using graph = vector<vector<Edge>>;
    //graph G;
    vector<bool> used;

public:
    graph G;
    MaxFlowGraph(int n){
        G = graph(n);
        used = vector<bool>(n, false);
    }

    void add_edge(int from, int to, T cap){
        G[from].push_back({to, cap, (int)G[to].size()});
        G[to].push_back({from, 0, (int)G[from].size()-1});
    }

    T dfs(int v, int t, T f){
        if(v == t) return f;
        used[v] = true;
        for(int i = 0; i < G[v].size(); i++){
            auto &e = G[v][i];
            if(used[e.to] || e.cap == 0) continue;
            T d = dfs(e.to, t, min(f, e.cap));
            if(d > 0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
        return 0;
    }

    T flow(int s, int t){
        T maxflow = 0;
        while(true){
            fill(used.begin(), used.end(), false);
            T INF = 1001001001;
            T f = dfs(s, t, INF);
            if(f == 0) return maxflow;
            else maxflow += f;
        }
    }
};

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    for(auto &p: a) cin >> p;

    MaxFlowGraph<int> G(n*2+2);
    const int s = n*2;
    const int t = n*2+1;
    for(int i = 0; i < n; i++) G.add_edge(n+i, t, 1);
    for(int i = 0; i < n; i++){
        G.add_edge(s, i, 1);
        for(int j = 0; j < n; j++){
            if(a[i] != j) G.add_edge(i, n+j, 1);
        }
    }

    if(G.flow(s, t) != n){
        cout << -1 << endl;
    }else{
        for(int i = 0; i < n; i++){
            for(auto &p: G.G[i]){
                if(p.cap == 0){
                    cout << p.to-n << endl;
                    break;
                }
            }
        }
    }

    return 0;
}
0