結果

問題 No.241 出席番号(1)
ユーザー Grun1396
提出日時 2018-04-05 22:24:42
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,870 bytes
コンパイル時間 970 ms
コンパイル使用メモリ 72,680 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-30 21:43:03
合計ジャッジ時間 2,123 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cctype>
#include <functional>
#include <map>
#include <string.h>
#include <cstdio>
using namespace std;
typedef long long ll;

#define reps(i,s,n) for(int (i) = (s); (i) < (n); (i)++)
#define rep(i,n) reps(i,0,n)
const int INF = 1e9;


struct edge { int to, cap, rev;};
const int MAX_V = 1e4;
vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from,int to,int cap){
//    printf("%d,%d,%d\n",from,to,cap);
    G[from].push_back({to,cap,(int)G[to].size()});
    G[to].push_back({from,0,(int)G[from].size()-1});
}

int dfs(int v,int t,int f){
    if(v == t) return f;
    used[v] = true;
    rep(i,G[v].size()){
        edge &e = G[v][i];

        if(!used[e.to] && e.cap > 0){
            int 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;
}

int max_flow(int s,int t){
    int flow = 0;
    for(;;){
        memset(used,0,sizeof(used));
        int f = dfs(s,t,INF);
        if(f==0)return flow;
        flow += f;
    }
}

int n;
vector<int> A;
int main(){

    cin >> n;
    
    int s = 2*n+1, t = 2*n + 2;
    A.resize(n);
    rep(i,n){
        cin >> A[i];
    }
    rep(i,n){
        add_edge(s,i,1);//s -> i
        add_edge(i+n,t,1);//i+n -> t
    }
    rep(i,n){
        rep(j,n){
            if(j != A[i]) add_edge(i,j+n,1);
        }
    }
int flow=max_flow(s,t);
//cout << flow<< endl;
    if(flow == n){
        rep(i,n){
            rep(j,G[i].size()){
                if(G[i][j].cap == 0 && G[i][j].to < 2*n+1){
                    
                    cout << G[i][j].to -n << endl;
                    break;
                }
            }
        }
    }else{
        cout << -1 << endl;
    }

    return 0;
}
0