結果

問題 No.1078 I love Matrix Construction
ユーザー knshnb
提出日時 2020-06-12 23:15:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 248 ms / 2,000 ms
コード長 3,720 bytes
コンパイル時間 2,715 ms
コンパイル使用メモリ 204,528 KB
最終ジャッジ日時 2025-01-11 03:02:38
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

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

#include <bits/stdc++.h> // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on
/**
* author: knshnb
* created: Fri Jun 12 22:02:09 JST 2020
**/
/// @docs src/Graph/StronglyConnectedComponents.md
struct StronglyConnectedComponents {
int n, size; // sizebuild()
std::vector<std::vector<int>> edge, redge;
std::vector<int> belong_to;
StronglyConnectedComponents(int n_) : n(n_), edge(n_), redge(n_) {}
void add_edge(int u, int v) {
assert(0 <= u && u < n && 0 <= v && v < n);
edge[u].push_back(v);
redge[v].push_back(u);
}
void build() {
std::vector<int> ord; // post-order
// dfspost-order
std::vector<bool> visited(n, false);
auto dfs1 = [&](auto f, int v) -> void {
if (visited[v]) return;
visited[v] = true;
for (int s : edge[v]) {
f(f, s);
}
ord.push_back(v);
};
for (int i = 0; i < n; i++) dfs1(dfs1, i);
// post-orderdfs
belong_to.assign(n, -1);
int cur_group = 0;
auto dfs2 = [&](auto f, int v) -> void {
if (belong_to[v] != -1) return;
belong_to[v] = cur_group;
for (int s : redge[v]) {
f(f, s);
}
};
std::reverse(ord.begin(), ord.end());
for (int i : ord) {
if (belong_to[i] == -1) {
dfs2(dfs2, i);
cur_group++;
}
}
size = cur_group;
}
bool has_loop() {
build();
return size < n;
}
};
struct TwoSat {
int n;
StronglyConnectedComponents scc; // [0, n): x, [n, 2n): not x
TwoSat(int n_) : n(n_), scc(2 * n_) {}
// xindex
int rev(int x) { return x < n ? x + n : x - n; }
// (x \vee y)
void add_condition(int x, bool not_x, int y, bool not_y) {
if (not_x) x = rev(x);
if (not_y) y = rev(y);
scc.add_edge(rev(x), y);
scc.add_edge(rev(y), x);
}
bool run() {
scc.build();
for (int x = 0; x < n; x++) {
if (scc.belong_to[x] == scc.belong_to[rev(x)]) return false;
}
return true;
}
bool is_true(int x) {
assert(0 <= x && x < n);
return scc.belong_to[x] > scc.belong_to[rev(x)];
}
};
template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}
signed main() {
Int n;
std::cin >> n;
std::vector<Int> s(n), t(n), u(n);
REP(i, n) std::cin >> s[i], s[i]--;
REP(i, n) std::cin >> t[i], t[i]--;
REP(i, n) std::cin >> u[i];
TwoSat sat(n * n);
auto enc = [&](Int i, Int j) { return n * i + j; };
REP(i, n) {
REP(j, n) {
Int x = enc(s[i], j);
Int y = enc(j, t[i]);
sat.add_condition(x, u[i] & 1, y, u[i] >> 1);
}
}
if (!sat.run()) {
std::cout << -1 << std::endl;
return 0;
}
REP(i, n) {
REP(j, n) {
std::cout << sat.is_true(enc(i, j)) << " ";
}
std::cout << "\n";
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0