結果

問題 No.1078 I love Matrix Construction
ユーザー minatominato
提出日時 2020-08-26 19:04:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 386 ms / 2,000 ms
コード長 4,350 bytes
コンパイル時間 3,070 ms
コンパイル使用メモリ 245,364 KB
実行使用メモリ 93,536 KB
最終ジャッジ日時 2024-04-25 03:39:27
合計ジャッジ時間 8,571 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 34 ms
16,852 KB
testcase_03 AC 109 ms
37,900 KB
testcase_04 AC 167 ms
51,156 KB
testcase_05 AC 145 ms
43,396 KB
testcase_06 AC 33 ms
16,004 KB
testcase_07 AC 13 ms
8,148 KB
testcase_08 AC 141 ms
42,980 KB
testcase_09 AC 6 ms
5,376 KB
testcase_10 AC 386 ms
93,536 KB
testcase_11 AC 169 ms
53,924 KB
testcase_12 AC 324 ms
78,320 KB
testcase_13 AC 349 ms
87,244 KB
testcase_14 AC 226 ms
61,296 KB
testcase_15 AC 332 ms
83,380 KB
testcase_16 AC 10 ms
7,268 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 23 ms
12,984 KB
testcase_19 AC 59 ms
25,408 KB
testcase_20 AC 58 ms
24,872 KB
testcase_21 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
        }
    }
}
0