結果
問題 | No.241 出席番号(1) |
ユーザー |
![]() |
提出日時 | 2021-04-09 22:48:49 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 1,952 bytes |
コンパイル時間 | 990 ms |
コンパイル使用メモリ | 81,120 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-25 06:29:14 |
合計ジャッジ時間 | 2,355 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#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;}