結果
問題 | No.1078 I love Matrix Construction |
ユーザー |
![]() |
提出日時 | 2020-08-26 19:04:40 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 515 ms / 2,000 ms |
コード長 | 4,350 bytes |
コンパイル時間 | 3,747 ms |
コンパイル使用メモリ | 249,932 KB |
最終ジャッジ日時 | 2025-01-13 15:03:58 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<long long, long long>; #define rep(i, n) for(int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() constexpr char ln = '\n'; constexpr long long MOD = 1000000007; //constexpr long long MOD = 998244353; template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; } template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; } inline int popcount(int x) {return __builtin_popcount(x);} inline int popcount(long long x) {return __builtin_popcountll(x);} void print() { cout << "\n"; } template<class T, class... Args> void print(const T &x, const Args &... args) { cout << x << " "; print(args...); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// struct StronglyConnectedComponent { //T:強連結成分をトポロジカルソートしたグラフ C:強連結成分の内訳 vector<vector<int>> G,R,T,C; vector<int> vs,used,blg; StronglyConnectedComponent() {} StronglyConnectedComponent(int N) : G(N), R(N), used(N), blg(N) {} void add_edge(int u,int v) { G[u].emplace_back(v); R[v].emplace_back(u); } void dfs(int v){ used[v] = 1; for(int u:G[v]) { if(!used[u]) dfs(u); } vs.emplace_back(v); } void rdfs(int v, int k) { used[v] = 1; blg[v] = k; C[k].emplace_back(v); for(int u:R[v]) { if(!used[u]) rdfs(u,k); } } int build() { int N = G.size(); for(int v = 0; v < N; v++) { if(!used[v]) dfs(v); } fill(used.begin(),used.end(),0); int k = 0; for(int i = N-1; i >= 0; i--) { if(!used[vs[i]]) { T.emplace_back(); C.emplace_back(); rdfs(vs[i], k++); } } for(int v = 0; v < N; v++) { for(int u:G[v]) { if(blg[v] != blg[u]) T[blg[v]].emplace_back(blg[u]); } } for(int i = 0; i < k; i++) { sort(T[i].begin(),T[i].end()); T[i].erase(unique(T[i].begin(),T[i].end()),T[i].end()); } return k; } int operator[](int k) const{return blg[k];} }; struct TwoSat { int N; StronglyConnectedComponent scc; TwoSat(int N) : N(N), scc(N*2) {} int negate(int v) {return (N+v)%(N*2);} void add_if(int u, int v) { // u -> v <=> !v -> !u scc.add_edge(u,v); scc.add_edge(negate(v),negate(u)); } void add_or(int u, int v) { // u or v <=> !u -> v add_if(negate(u),v); } void add_nand(int u, int v) { // u nand v <=> u -> !v add_if(u,negate(v)); } void set_true(int v) { // v <=> !v -> v scc.add_edge(negate(v),v); } void set_false(int v) { // !v <=> v -> !v scc.add_edge(v,negate(v)); } vector<int> build() { scc.build(); vector<int> ret(N); for (int i = 0; i < N; ++i) { if(scc[i]==scc[N+i]) return {}; ret[i] = scc[i] > scc[N+i]; } return ret; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); 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]; TwoSat TS(N*N); rep(i,N) { rep(j,N) { if (U[i]==0) { TS.add_nand(N*N+S[i]*N+j,N*N+j*N+T[i]); } else if (U[i]==1) { TS.add_nand(S[i]*N+j,N*N+j*N+T[i]); } else if (U[i]==2) { TS.add_nand(N*N+S[i]*N+j,j*N+T[i]); } else { TS.add_nand(S[i]*N+j,j*N+T[i]); } } } auto ans = TS.build(); if (ans.empty()) cout << -1 << ln; else { rep(i,N*N) { cout << ans[i] << " "; if ((i+1)%N==0) cout << ln; } } }