結果

問題 No.1078 I love Matrix Construction
ユーザー knshnbknshnb
提出日時 2020-06-12 22:42:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 343 ms / 2,000 ms
コード長 3,320 bytes
コンパイル時間 2,630 ms
コンパイル使用メモリ 215,688 KB
実行使用メモリ 48,580 KB
最終ジャッジ日時 2024-06-24 05:27:13
合計ジャッジ時間 8,716 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 29 ms
9,796 KB
testcase_03 AC 101 ms
20,344 KB
testcase_04 AC 150 ms
26,836 KB
testcase_05 AC 124 ms
22,740 KB
testcase_06 AC 25 ms
9,728 KB
testcase_07 AC 10 ms
6,940 KB
testcase_08 AC 124 ms
23,212 KB
testcase_09 AC 5 ms
6,944 KB
testcase_10 AC 343 ms
48,580 KB
testcase_11 AC 161 ms
28,164 KB
testcase_12 AC 252 ms
40,264 KB
testcase_13 AC 268 ms
45,132 KB
testcase_14 AC 194 ms
32,120 KB
testcase_15 AC 264 ms
42,940 KB
testcase_16 AC 8 ms
6,940 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 18 ms
8,192 KB
testcase_19 AC 56 ms
14,160 KB
testcase_20 AC 57 ms
14,040 KB
testcase_21 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>  // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Fri Jun 12 22:02:09 JST 2020
 **/

/// @docs src/Graph/StronglyConnectedComponents.md
struct StronglyConnectedComponents {
    int n, size;  // sizeはbuild()後に強連結成分の数を格納
    std::vector<std::vector<int>> edge, redge;
    std::vector<int> belong_to;
    StronglyConnectedComponents(int n_) : n(n_), edge(n_), redge(n_) {}
    void add_edge(int u, int v) {
        assert(0 <= u && u < n && 0 <= v && v < n);
        edge[u].push_back(v);
        redge[v].push_back(u);
    }
    void build() {
        std::vector<int> ord;  // post-order
        // 正方向の辺でdfs、post-orderをふる
        std::vector<bool> visited(n, false);
        auto dfs1 = [&](auto f, int v) -> void {
            if (visited[v]) return;
            visited[v] = true;
            for (int s : edge[v]) {
                f(f, s);
            }
            ord.push_back(v);
        };
        for (int i = 0; i < n; i++) dfs1(dfs1, i);

        // post-order逆順にdfs、到達可能な同値類に分ける
        belong_to.assign(n, -1);
        int cur_group = 0;
        auto dfs2 = [&](auto f, int v) -> void {
            if (belong_to[v] != -1) return;
            belong_to[v] = cur_group;
            for (int s : redge[v]) {
                f(f, s);
            }
        };
        std::reverse(ord.begin(), ord.end());
        for (int i : ord) {
            if (belong_to[i] == -1) {
                dfs2(dfs2, i);
                cur_group++;
            }
        }
        size = cur_group;
    }
    bool has_loop() {
        build();
        return size < n;
    }
};

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
    return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

signed main() {
    Int n;
    std::cin >> n;
    std::vector<Int> s(n), t(n), u(n);
    REP(i, n) std::cin >> s[i], s[i]--;
    REP(i, n) std::cin >> t[i], t[i]--;
    REP(i, n) std::cin >> u[i];
    StronglyConnectedComponents g(2 * n * n);
    auto enc = [&](Int i, Int j) { return n * i + j; };
    auto rev = [&](Int x) { return x < n * n ? x + n * n : x - n * n; };
    REP(i, n) {
        REP(j, n) {
            Int x = enc(s[i], j);
            if (u[i] & 1) x = rev(x);
            Int y = enc(j, t[i]);
            if (u[i] >> 1) y = rev(y);
            g.add_edge(rev(x), y);
            g.add_edge(rev(y), x);
        }
    }
    g.build();
    auto ans = make_vec<Int>(n, n, 0);
    REP(i, n) REP(j, n) {
        Int x = enc(i, j);
        if (g.belong_to[x] == g.belong_to[rev(x)]) {
            std::cout << -1 << std::endl;
            return 0;
        }
        ans[i][j] = g.belong_to[x] > g.belong_to[rev(x)];
    }
    REP(i, n) {
        REP(j, n) { std::cout << ans[i][j] << " "; }
        std::cout << "\n";
    }
}
0