結果
問題 | No.1078 I love Matrix Construction |
ユーザー |
|
提出日時 | 2020-06-13 01:39:09 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 219 ms / 2,000 ms |
コード長 | 2,868 bytes |
コンパイル時間 | 1,821 ms |
コンパイル使用メモリ | 179,068 KB |
実行使用メモリ | 48,372 KB |
最終ジャッジ日時 | 2024-06-24 06:16:07 |
合計ジャッジ時間 | 6,596 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
#include <bits/stdc++.h>using namespace std;typedef long long ll;#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)#define ALL(v) (v).begin(),(v).end()#define CLR(t,v) memset(t,(v),sizeof(t))template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}ll nextLong() { ll x; scanf("%lld", &x); return x;}struct TwoSAT {const int N;vector<vector<int>> g, r;TwoSAT(int N): N(N) {g = vector<vector<int>>(2*N);r = vector<vector<int>>(2*N);}// (a == a_val || b == b_val)void add_cond(int a, bool a_val, int b, bool b_val) {int x = 2 * a + (a_val ? 1 : 0);int y = 2 * b + (b_val ? 1 : 0);g[x^1].push_back(y);g[y^1].push_back(x);r[y].push_back(x^1);r[x].push_back(y^1);}// !(a == a_val && b == b_val)void add_forbid(int a, bool a_val, int b, bool b_val) {add_cond(a, !a_val, b, !b_val);}vector<int> sccid;int sccnum;void dfs(int p, vector<int>& order) {if (sccid[p] != 0) return;sccid[p] = 1;for (int i : g[p]) dfs(i, order);order.push_back(p);}void rdfs(int p, int id) {if (sccid[p] != -1) return;sccid[p] = id;for (int i : r[p]) rdfs(i, id);}void scc() {sccnum = 0;sccid = vector<int>(2 * N);vector<int> order;REP(i, 2*N) dfs(i, order);fill(ALL(sccid), -1);sccnum = 0;for (int i = (int)order.size() - 1; i >= 0; i--) if (sccid[order[i]] == -1) {rdfs(order[i], sccnum);sccnum++;}}// 割り当てを一つ返す。存在しない場合は長さ0を返す。vector<int> solve() {vector<int> res(N);REP(i, N) {if (sccid[2*i] != -1 && sccid[2*i] == sccid[2*i+1]) return {};res[i] = sccid[2*i+1] > sccid[2*i] ? 1 : 0;}return res;}};const int MAX_N = 505;int A[MAX_N][MAX_N];int N;int nodeId(int r, int c) {return (r * N + c);}int main2() {N = nextLong();vector<int> S(N), T(N), U(N);REP(i, N) S[i] = nextLong()-1;REP(i, N) T[i] = nextLong()-1;REP(i, N) U[i] = nextLong();TwoSAT twoSAT(N*N);REP(i, N) REP(j, N) {twoSAT.add_forbid(nodeId(S[i], j), U[i] & 1,nodeId(j, T[i]), U[i] >> 1);}twoSAT.scc();auto res = twoSAT.solve();if (res.size() == 0) {cout << -1 << endl;} else {REP(i, N*N) {int r = i / N;int c = i % N;A[r][c] = res[i];}REP(r, N) {REP(c, N) {if (c) cout << ' ';cout << A[r][c];}cout << '\n';}}return 0;}int main() {#ifdef LOCALfor (;!cin.eof();cin>>ws)#endifmain2();return 0;}