結果

問題 No.1078 I love Matrix Construction
ユーザー MisterMister
提出日時 2020-08-18 12:24:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 353 ms / 2,000 ms
コード長 4,126 bytes
コンパイル時間 1,151 ms
コンパイル使用メモリ 92,956 KB
実行使用メモリ 95,548 KB
最終ジャッジ日時 2024-04-20 07:30:08
合計ジャッジ時間 6,751 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 39 ms
18,236 KB
testcase_03 AC 117 ms
39,272 KB
testcase_04 AC 166 ms
52,132 KB
testcase_05 AC 135 ms
43,744 KB
testcase_06 AC 35 ms
16,308 KB
testcase_07 AC 13 ms
8,312 KB
testcase_08 AC 144 ms
43,760 KB
testcase_09 AC 7 ms
5,376 KB
testcase_10 AC 353 ms
95,548 KB
testcase_11 AC 174 ms
54,580 KB
testcase_12 AC 277 ms
79,968 KB
testcase_13 AC 346 ms
89,556 KB
testcase_14 AC 218 ms
67,748 KB
testcase_15 AC 311 ms
84,960 KB
testcase_16 AC 12 ms
7,296 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 24 ms
13,120 KB
testcase_19 AC 61 ms
25,820 KB
testcase_20 AC 67 ms
25,260 KB
testcase_21 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

template <class Cost = int>
struct StronglyConnectedComponents {
    Graph<Cost> graph, rgraph;
    std::vector<bool> visited;
    std::vector<int> stk;

    // id[v] = 頂点vはgroups[id[v]]に属する
    std::vector<int> id;
    std::vector<std::vector<int>> groups;

    explicit StronglyConnectedComponents(const Graph<Cost>& g)
        : graph(g), visited(graph.size(), false), id(graph.size(), -1) {
        revinit();

        for (int v = 0; v < (int)graph.size(); ++v) dfs(v);

        while (!stk.empty()) {
            int v = stk.back();
            stk.pop_back();
            if (id[v] < 0) {
                groups.emplace_back();
                rdfs(v);
            }
        }
    }

    void revinit() {
        rgraph = Graph<Cost>(graph.size());
        for (int v = 0; v < (int)graph.size(); ++v) {
            for (const auto& e : graph[v]) {
                rgraph[e.dst].emplace_back(e.dst, v, e.cost);
            }
        }
    }

    void dfs(int v) {
        if (visited[v]) return;
        visited[v] = true;
        for (const auto& e : graph[v]) dfs(e.dst);
        stk.push_back(v);
    }

    void rdfs(int v) {
        if (id[v] >= 0) return;
        id[v] = groups.size() - 1;
        groups.back().push_back(v);
        for (const auto& e : rgraph[v]) rdfs(e.dst);
    }
};

struct TwoSat {
    int vnum;
    Graph<> graph;

    explicit TwoSat(int n) : vnum(n), graph(n * 2) {}

    // t=1 <=> true
    int enc(int x, bool t) {
        return x + (t ? vnum : 0);
    }

    // [tx]x V [ty]y
    void span(int x, bool tx, int y, bool ty) {
        graph[enc(x, !tx)].emplace_back(enc(x, !tx), enc(y, ty));
        graph[enc(y, !ty)].emplace_back(enc(y, !ty), enc(x, tx));
    }

    // if unsatisfiable, return an empty vector
    std::vector<bool> exec() {
        StronglyConnectedComponents scc(graph);

        std::vector<bool> assign(vnum);
        for (int x = 0; x < vnum; ++x) {
            int fid = scc.id[enc(x, false)],
                tid = scc.id[enc(x, true)];

            if (fid == tid) {
                assign.clear();
                break;
            } else {
                assign[x] = (fid < tid);
            }
        }
        return assign;
    }
};

void solve() {
    int n;
    std::cin >> n;

    std::vector<int> ss(n), ts(n), us(n);
    for (auto& s : ss) {
        std::cin >> s;
        --s;
    }
    for (auto& t : ts) {
        std::cin >> t;
        --t;
    }
    for (auto& u : us) std::cin >> u;

    TwoSat sat(n * n);
    auto enc = [&](int x, int y) { return x * n + y; };

    for (int i = 0; i < n; ++i) {
        int p = us[i] & 1,
            q = us[i] >> 1;
        for (int j = 0; j < n; ++j) {
            sat.span(enc(ss[i], j), 1 - p, enc(j, ts[i]), 1 - q);
        }
    }

    auto ans = sat.exec();
    if (ans.empty()) {
        std::cout << -1 << "\n";
    } else {
        for (int x = 0; x < n; ++x) {
            for (int y = 0; y < n; ++y) {
                std::cout << ans[enc(x, y)] << " ";
            }
            std::cout << "\n";
        }
    }
}

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

    solve();

    return 0;
}
0