結果

問題 No.241 出席番号(1)
ユーザー commy
提出日時 2020-11-29 21:41:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,769 bytes
コンパイル時間 1,364 ms
コンパイル使用メモリ 89,508 KB
最終ジャッジ日時 2025-01-16 10:16:17
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <utility>
#include <tuple>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;
// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << '\n'; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on
#include <queue>
template<typename T>
class Dinic {
struct Edge {
int to, rev;
T cap;
Edge(int t, int r, T c)
: to(t), rev(r), cap(c) {}
};
int n;
vector<int> level;
vector<int> itr;
bool bfs(int s, int t) {
queue<int> que;
level.assign(n, -1);
level[s] = 0;
que.emplace(s);
while (que.size()) {
int v = que.front();
que.pop();
for (auto &edge : G[v]) {
if (edge.cap > 0 && level[edge.to] == -1) {
level[edge.to] = level[v] + 1;
que.emplace(edge.to);
}
}
}
return level[t] != -1;
}
T dfs(int v, int t, T flow) {
if (v == t) return flow;
for (int &i = itr[v]; i < static_cast<int>(G[v].size()); i++) {
auto &edge = G[v][i];
if (edge.cap > 0 && level[v] < level[edge.to]) {
T d = dfs(edge.to, t, min(flow, edge.cap));
if (d > 0) {
edge.cap -= d;
G[edge.to][edge.rev].cap += d;
return d;
}
}
}
return 0;
}
public:
vector<vector<Edge>> G;
Dinic(int _n) {
n = _n;
G.resize(n);
}
void add_edge(int s, int t, T cap) {
G[s].emplace_back(t, G[t].size(), cap);
G[t].emplace_back(s, static_cast<int>(G[s].size() - 1), 0);
}
T max_flow(int s, int t, T inf) {
T ans = 0;
while (bfs(s, t)) {
itr.assign(n, 0);
for (T f = 0; (f = dfs(s, t, inf)) > 0; ans += f)
;
}
return ans;
}
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin, n;
int start = 2 * n, goal = start + 1;
Dinic<int> dinic(goal + 1);
rep(i, 0, n) {
int ng;
cin, ng;
dinic.add_edge(start, i, 1);
dinic.add_edge(i + n, goal, 1);
rep(j, 0, n) {
if (ng == j) continue;
dinic.add_edge(i, j + n, 1);
}
}
int sz = dinic.max_flow(start, goal, n);
if (sz == n) {
rep(i, 0, n) {
for (auto edge : dinic.G[i]) {
if (n <= edge.to && edge.to < 2 * n && edge.cap == 0) {
print(edge.to - n);
break;
}
}
}
} else {
print(-1);
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0