結果
問題 | No.241 出席番号(1) |
ユーザー |
![]() |
提出日時 | 2016-07-12 11:31:28 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,695 bytes |
コンパイル時間 | 561 ms |
コンパイル使用メモリ | 62,300 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-30 21:35:56 |
合計ジャッジ時間 | 1,898 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#include <iostream>#include <vector>#include <cstring>using namespace std;const int kMAX_V = 60 * 2;struct BipartiteMatching {int V; // 頂点数vector<int> graph[kMAX_V]; // グラフの隣接リスト表現int match[kMAX_V]; // 各頂点ごとのマッチングの対応bool used[kMAX_V]; // DFSの際に使用BipartiteMatching() { }void Init(int v) {V = v;}// uとvを連結にするvoid AddEdge(int u, int v) {graph[u].push_back(v);graph[v].push_back(u);}bool DFS(int v) {used[v] = true;for (int i = 0; i < graph[v].size(); i++) {int u = graph[v][i], w = match[u];if (w < 0 || (!used[w] && DFS(w))) {match[v] = u;match[u] = v;return true;}}return false;}// 二部マッチングを解くint Solve() {int res = 0;memset(match, -1, sizeof(match));for (int v = 0; v < V; v++) {if (match[v] < 0) {memset(used, sizeof(used), false);if (DFS(v)) {res++;}}}return res;}};int N;BipartiteMatching BM;int main() {cin >> N;BM.Init(N * 2);for (int i = 0; i < N; i++) {int a;cin >> a;for (int j = 0; j < N; j++) {if (j == a) continue;BM.AddEdge(i, N + j);}}if (BM.Solve() == N) {for (int i = 0; i < N; i++) {cout << BM.match[i] - N << endl;}} else {cout << -1 << endl;}}