結果

問題 No.1078 I love Matrix Construction
ユーザー finefine
提出日時 2020-06-13 02:00:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 2,322 bytes
コンパイル時間 3,179 ms
コンパイル使用メモリ 175,572 KB
実行使用メモリ 50,012 KB
最終ジャッジ日時 2023-09-06 11:40:21
合計ジャッジ時間 6,181 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 17 ms
10,232 KB
testcase_03 AC 51 ms
21,008 KB
testcase_04 AC 74 ms
27,524 KB
testcase_05 AC 63 ms
23,452 KB
testcase_06 AC 17 ms
9,924 KB
testcase_07 AC 7 ms
5,756 KB
testcase_08 AC 61 ms
23,892 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 184 ms
50,012 KB
testcase_11 AC 80 ms
28,872 KB
testcase_12 AC 144 ms
41,932 KB
testcase_13 AC 167 ms
46,452 KB
testcase_14 AC 102 ms
33,812 KB
testcase_15 AC 152 ms
44,396 KB
testcase_16 AC 7 ms
5,416 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 13 ms
8,316 KB
testcase_19 AC 30 ms
14,512 KB
testcase_20 AC 30 ms
14,268 KB
testcase_21 AC 2 ms
4,376 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;
    }
};

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];
    }

    StronglyConnectedComponents scc(n * n * 2);
    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++) {
            scc.add_edge(s[i] * n + j + n * n * f0, j * n + t[i] + n * n * !f1);
            scc.add_edge(j * n + t[i] + n * n * f1, s[i] * n + j + n * n * !f0);
        }
    }

    scc.run();

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (scc.cmp[i * n + j] == scc.cmp[i * n + j + n * n]) {
                cout << -1 << newl;
                return 0;
            }
        }
    }

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

    return 0;
}
0