結果

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

ソースコード

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

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} iosetup;
struct TwoSatLight {
TwoSatLight(int n) : n(n), graph(n << 1), rev_graph(n << 1), used(n << 1, false), id(n << 1, -1) {}
int negate(int x) { return (n + x) % (n << 1); }
void add_or(int x, int y) {
graph[negate(x)].emplace_back(y);
graph[negate(y)].emplace_back(x);
rev_graph[y].emplace_back(negate(x));
rev_graph[x].emplace_back(negate(y));
}
void add_if(int x, int y) { add_or(negate(x), y); }
void add_nand(int x, int y) { add_or(negate(x), negate(y)); }
void set_true(int x) { add_or(x, x); }
void set_false(int x) { set_true(negate(x)); }
vector<bool> build() {
REP(i, n << 1) {
if (!used[i]) dfs(i);
}
int now = 0;
for (int i = (n << 1) - 1; i >= 0; --i) {
if (id[order[i]] == -1) rev_dfs(order[i], now++);
}
vector<bool> res(n);
REP(i, n) {
if (id[i] == id[negate(i)]) return {};
res[i] = id[negate(i)] < id[i];
}
return res;
}
private:
int n;
vector<vector<int>> graph, rev_graph;
vector<bool> used;
vector<int> id, order;
void dfs(int ver) {
used[ver] = true;
for (int e : graph[ver]) {
if (!used[e]) dfs(e);
}
order.emplace_back(ver);
}
void rev_dfs(int ver, int now) {
id[ver] = now;
for (int e : rev_graph[ver]) {
if (id[e] == -1) rev_dfs(e, now);
}
}
};
int main() {
int n; cin >> n;
vector<int> s(n), t(n), u(n);
REP(i, n) cin >> s[i], --s[i];
REP(i, n) cin >> t[i], --t[i];
REP(i, n) cin >> u[i];
TwoSatLight two_sat(n * n);
REP(i, n) REP(j, n) {
int x = s[i] * n + j, y = j * n + t[i];
if (u[i] == 0) {
two_sat.add_or(x, y);
} else if (u[i] == 1) {
two_sat.add_if(x, y);
} else if (u[i] == 2) {
two_sat.add_if(y, x);
} else if (u[i] == 3) {
two_sat.add_or(two_sat.negate(x), two_sat.negate(y));
}
}
vector<bool> ans = two_sat.build();
if (ans.empty()) {
cout << "-1\n";
return 0;
}
int pos = 0;
REP(i, n) REP(j, n) cout << ans[pos++] << " \n"[j + 1 == n];
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0