結果

問題 No.241 出席番号(1)
ユーザー k
提出日時 2021-03-10 02:24:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,327 bytes
コンパイル時間 1,981 ms
コンパイル使用メモリ 202,320 KB
最終ジャッジ日時 2025-01-19 13:10:26
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

class bipartite_matching {
  int n;
  vector<vector<int> > g;
  vector<int> match;
  vector<bool> used;

  bool dfs(int v) {
    used[v] = true;
    for (int u: g[v]) {
      int w = match[u];
      if (w < 0 || !used[w] && dfs(w)) {
        match[v] = u;
        match[u] = v;
        return true;
      }
    }
    return false;
  }
  
public:
  bipartite_matching(int n) {
    this->n = n;
    this->g = vector<vector<int> >(n);
    this->match = vector<int>(n, -1);
    this->used = vector<bool>(n);
  }

  void add_edge(int u, int v) {
    g[u].push_back(v);
    g[v].push_back(u);
  }

  int build() {
    int ret = 0;
    for (int v = 0; v < n; v++) {
      if (match[v] < 0) {
        fill(used.begin(), used.end(), false);
        if (dfs(v))
          ++ret;
      }
    }
    return ret;
  }

  int pair(int v) {
    return match[v];
  }
};


int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  bipartite_matching bm(2*n);
  
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    for (int j = 0; j < n; j++) {
      if (j != a) {
        bm.add_edge(i, n+j);
      }
    }
  }

  if (bm.build() != n) {
    cout << -1 << endl;
  } else {
    for (int i = 0; i < n; i++)
      cout << bm.pair(i) - n << endl;
  }
  
  return 0;
}
0