結果
問題 | No.241 出席番号(1) |
ユーザー |
![]() |
提出日時 | 2015-08-19 21:34:23 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 3,616 bytes |
コンパイル時間 | 1,570 ms |
コンパイル使用メモリ | 170,468 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-30 21:35:12 |
合計ジャッジ時間 | 2,863 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#include <bits/stdc++.h>using namespace std;struct Edge {typedef int CostType;const static int cost = 1;int from, to;Edge(int from, int to) : from(from), to(to) {};};template<typename Cost> struct WeightedEdge : public Edge {typedef Cost CostType;Cost cost;WeightedEdge(int from, int to, Cost cost = 0) : Edge(from, to), cost(cost) {}};template<typename Capacity> struct ResidualEdge : public Edge {typedef Capacity CapacityType;Capacity cap;int rev;ResidualEdge(int from, int to, Capacity cap) : Edge(from, to), cap(cap) {}ResidualEdge reverse() const {return ResidualEdge(to, from, 0);}};template<typename Capacity, typename Cost> struct WeightedResidualEdge : public ResidualEdge<Capacity> {Cost cost;WeightedResidualEdge(int from, int to, Capacity cap, Cost cost) : ResidualEdge<Capacity>(from, to, cap), cost(cost) {}WeightedResidualEdge reverse() const {return WeightedResidualEdge(this->to, this->from, 0, -cost);}};template<typename Edge> class Graph {public:typedef Edge EdgeType;virtual int size() const = 0;template<typename... Args> void addEdge(Args...) {}template<typename... Args> void addUndirectedEdge(Args...) {}virtual vector<Edge> getEdges() const = 0;virtual vector<Edge> getEdges(int from) const = 0;virtual vector<Edge> getEdges(int from, int to) const = 0;virtual int getDegree(int v) const = 0;};template<typename Edge> class AdjacencyList : public Graph<Edge> {protected:vector<vector<Edge>> graph;public:AdjacencyList(int n) : graph(n) {}int size() const {return graph.size();}template<typename... Args> void addEdge(Args... args) {Edge edge(args...);graph[edge.from].emplace_back(edge);}template<typename... Args> void addUndirectedEdge(Args... args) {Edge edge(args...);addEdge(edge);swap(edge.from, edge.to);addEdge(edge);}vector<Edge> getEdges() const {vector<Edge> res;for (const auto& edges : graph) {res.insert(res.end(), edges.begin(), edges.end());}return res;}vector<Edge> getEdges(int from) const {return graph[from];}vector<Edge> getEdges(int from, int to) const {vector<Edge> res;for (const auto& edge : graph[from]) {if (edge.to == to) res.emplace_back(edge);}return res;}int getDegree(int v) const {return graph[v].size();}vector<Edge>& operator[](int v) {return graph[v];}};class BipartiteMatching {private:AdjacencyList<Edge> graph;vector<bool> used;bool dfs(int v) {used[v] = true;for (const Edge& u : graph[v]) {int w = match[u.to];if (w < 0 || (!used[w] && dfs(w))) {match[v] = u.to;match[u.to] = v;return true;}}return false;}public:vector<int> match;BipartiteMatching(const AdjacencyList<Edge>& graph) : graph(graph), used(graph.size()), match(graph.size(), -1) {}int solve() {int res = 0;for (int i = 0; i < graph.size(); ++i) {if (match[i] >= 0) continue;fill(used.begin(), used.end(), false);if (dfs(i)) ++res;}return res;}};int main() {int n;cin >> n;vector<int> a(n);for (int& i : a) cin >> i;AdjacencyList<Edge> graph(2 * n);for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {if (a[i] == j) continue;graph.addEdge(i, j + n);}}BipartiteMatching match(graph);if (match.solve() != n) {cout << -1 << endl;} else {for (int i = 0; i < n; ++i) cout << match.match[i] - n << endl;}}