結果
問題 | No.241 出席番号(1) |
ユーザー |
|
提出日時 | 2020-03-04 04:50:44 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 2,263 bytes |
コンパイル時間 | 2,991 ms |
コンパイル使用メモリ | 207,828 KB |
最終ジャッジ日時 | 2025-01-09 04:21:23 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#include <bits/stdc++.h> #define int long long #define endl '\n' #define FOR(i, a, n) for (int i = (a); i < (n); ++i) #define REP(i, n) FOR(i, 0, n) using namespace std; class HopcroftKarp { void bfs() { const int n = g.size(); dist.assign(n, -1); queue<int> que; for (int i = 0; i < n; ++i) if (!used[i]) { que.push(i); dist[i] = 0; } while (!que.empty()) { int v = que.front(); que.pop(); for (int& u : g[v]) { int w = match[u]; if (w >= 0 && dist[w] == -1) { dist[w] = dist[v] + 1; que.emplace(w); } } } } bool dfs(int v) { vv[v] = true; for (auto& u : g[v]) { int w = match[u]; if (w < 0 || (!vv[w] && dist[w] == dist[v] + 1 && dfs(w))) { match[u] = v; used[v] = true; return true; } } return false; } public: vector<vector<int>> g; vector<int> dist, match; vector<bool> used, vv; HopcroftKarp(int n, int m) : g(n), match(m, -1), used(n) {} void add_edge(int u, int v) { g[u].push_back(v); } int bipartite_matching() { const int n = g.size(); int res = 0; while (true) { bfs(); vv.assign(n, false); int flow = 0; for (int i = 0; i < n; ++i) { if (!used[i] && dfs(i)) ++flow; } if (!flow) break; res += flow; } return res; } void print() { for (int i = 0; i < match.size(); ++i) { if (~match[i]) cout << match[i] << " - " << i << endl; } } }; int N; int A[50]; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; HopcroftKarp g(N, N); REP (i, N) { cin >> A[i]; REP (j, N) if (j != A[i]) { g.add_edge(i, j); } } if (g.bipartite_matching() < N) { cout << -1 << endl; return 0; } vector<int> ans(N); REP (i, N) ans[g.match[i]] = i; REP (i, N) cout << ans[i] << endl; }