結果

問題 No.1078 I love Matrix Construction
ユーザー finefine
提出日時 2020-06-13 02:27:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 2,949 bytes
コンパイル時間 1,990 ms
コンパイル使用メモリ 177,464 KB
実行使用メモリ 50,100 KB
最終ジャッジ日時 2024-06-24 06:17:42
合計ジャッジ時間 6,260 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 22 ms
10,240 KB
testcase_03 AC 71 ms
21,332 KB
testcase_04 AC 98 ms
27,876 KB
testcase_05 AC 84 ms
23,768 KB
testcase_06 AC 22 ms
10,112 KB
testcase_07 AC 9 ms
6,944 KB
testcase_08 AC 81 ms
24,008 KB
testcase_09 AC 4 ms
6,940 KB
testcase_10 AC 221 ms
50,100 KB
testcase_11 AC 109 ms
29,064 KB
testcase_12 AC 175 ms
41,872 KB
testcase_13 AC 196 ms
46,748 KB
testcase_14 AC 133 ms
34,108 KB
testcase_15 AC 190 ms
44,640 KB
testcase_16 AC 7 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 16 ms
8,320 KB
testcase_19 AC 42 ms
14,592 KB
testcase_20 AC 40 ms
14,560 KB
testcase_21 AC 3 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

class StronglyConnectedComponents {
private:
    void dfs(int v, vector<int>& used) {
        used[v] = true;
        for (int i = 0; i < G[v].size(); i++) {
            if (!used[G[v][i]]) dfs(G[v][i], used);
        }
        vs.push_back(v);
    }

    void rdfs(int v, int k, vector<int>& used) {
        used[v] = true;
        cmp[v] = k;
        for (int i = 0; i < rG[v].size(); i++) {
            if (!used[rG[v][i]]) rdfs(rG[v][i], k, used);
        }
    }

public:
    int V;
    vector< vector<int> > G, rG;
    vector<int> vs;
    vector<int> cmp;

    StronglyConnectedComponents(int n) : V(n), G(n), rG(n), cmp(n, -1) {}

    void add_edge(int from, int to) {
        G[from].push_back(to);
        rG[to].push_back(from);
    }

    int run() {
        vector<int> used(V, false);
        vs.clear();
        for (int v = 0; v < V; v++) {
            if (!used[v]) dfs(v, used);
        }
        used.assign(V, false);
        int k = 0;
        for (int i = (int)vs.size() - 1; i >= 0; i--) {
            if (!used[vs[i]]) rdfs(vs[i], k++, used);
        }
        return k;
    }
};

// [Depends]: StronglyConnectedComponents
struct TwoSAT {
    int literal_num;
    StronglyConnectedComponents scc;

    TwoSAT(int V) : literal_num(V), scc(2 * V) {}

    // x, y: index
    // flag_x, flag_y: literal's value
    void add(int x, bool flag_x, int y, bool flag_y) noexcept {
        scc.add_edge(x + literal_num * flag_x, y + literal_num * !flag_y);
        scc.add_edge(y + literal_num * flag_y, x + literal_num * !flag_x);
    }

    bool run() noexcept {
        scc.run();
        return satisfy();
    }

    bool satisfy() noexcept {
        for (int i = 0; i < literal_num; i++) {
            if (scc.cmp[i] == scc.cmp[i + literal_num]) return false;
        }
        return true;
    }

    vector<int> dump() noexcept {
        vector<int> res(literal_num);
        for (int i = 0; i < literal_num; i++) {
            res[i] = scc.cmp[i] > scc.cmp[i + literal_num];
        }
        return res;
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vector<int> s(n), t(n), u(n);
    for (int i = 0; i < n; i++) {
        cin >> s[i];
        --s[i];
    }

    for (int i = 0; i < n; i++) {
        cin >> t[i];
        --t[i];
    }

    TwoSAT sat(n * n);
    for (int i = 0; i < n; i++) {
        cin >> u[i];
        bool f0 = !(u[i] & 1);
        bool f1 = !(u[i] >> 1 & 1);

        for (int j = 0; j < n; j++) {
            sat.add(s[i] * n + j, f0, j * n + t[i], f1);
        }
    }

    if (!sat.run()) {
        cout << -1 << newl;
        return 0;
    }

    vector<int> res = sat.dump();

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << res[i * n + j] << " \n"[j + 1 == n];
        }
    }

    return 0;
}
0